To keep your Mac secure when you step away from it you can configure your computer to require a username and password when it wakes from sleep, or after the screensaver activates. This prevents unauthorized people from messing with your data when you've stepped away for a few minutes, or if they try to wake your computer from sleep mode.
Choose Apple menu > System Preferences, click Security & Privacy, then click General.
Select “Require password… after sleep or screen saver begins.”
Click the pop-up menu and choose the time that passes before a password is required.