Switch port security can be configured to restrict access to ports by limiting the number of MAC addresses and enforcing violations. The steps are:
1. Set the interface mode to access and enable port security.
2. Set the maximum secure MAC addresses and violation mode which can be protect, restrict, or shutdown.
3. Configure the rate limit for invalid source MAC addresses and specify allowed secure MAC addresses.