The @Secured annotation helps protect parts of your app by allowing only certain users to access them.
@Secured annotation in Spring Boot
Start learning this pattern below
Jump into concepts and practice - no test required
@Secured({"ROLE_NAME"})
public void methodName() {
// method code
}Use role names with the prefix ROLE_ by convention.
You can specify multiple roles inside the braces as an array.
ROLE_ADMIN role.@Secured({"ROLE_ADMIN"})
public void adminOnly() {
// admin code
}ROLE_USER or ROLE_ADMIN.@Secured({"ROLE_USER", "ROLE_ADMIN"})
public void userOrAdmin() {
// code for users or admins
}This service has two methods. One is only for admins, the other for users or admins.
import org.springframework.security.access.annotation.Secured; import org.springframework.stereotype.Service; @Service public class DocumentService { @Secured({"ROLE_ADMIN"}) public String getAdminDocument() { return "Secret Admin Document"; } @Secured({"ROLE_USER", "ROLE_ADMIN"}) public String getUserDocument() { return "User Document"; } }
You must enable method security in your Spring Boot app with @EnableMethodSecurity.
If a user does not have the required role, Spring Security will block access and throw an exception.
Roles should be granted to users in your security configuration or user database.
@Secured restricts method access by user roles.
Use role names with ROLE_ prefix inside curly braces.
Works well for simple role-based security in Spring Boot apps.
Practice
What is the main purpose of the @Secured annotation in Spring Boot?
Solution
Step 1: Understand the role of
The@Secured@Securedannotation is used to limit method access to users with specific roles.Step 2: Compare with other options
Other options relate to different Spring features like database or HTTP handling, not security roles.Final Answer:
To restrict access to methods based on user roles -> Option AQuick Check:
@Securedcontrols method access by roles [OK]
- Confusing @Secured with @RequestMapping
- Thinking @Secured configures database
- Assuming @Secured manages app properties
Which of the following is the correct way to use @Secured to allow only users with role ADMIN to access a method?
@Secured({"?"})
public void adminMethod() { }Solution
Step 1: Recall role naming convention
Spring Security requires roles to be prefixed withROLE_, soROLE_ADMINis correct.Step 2: Check other options
ADMINwithout prefix is invalid;ROLE-ADMINuses wrong separator;ROLE_ADMINISTRATORis a different role.Final Answer:
ROLE_ADMIN -> Option AQuick Check:
Roles needROLE_prefix [OK]
- Omitting ROLE_ prefix
- Using dash (-) instead of underscore (_)
- Using wrong role names
Given this method secured with @Secured({"ROLE_USER", "ROLE_ADMIN"}), what happens if a user with role ROLE_GUEST calls it?
@Secured({"ROLE_USER", "ROLE_ADMIN"})
public String getData() {
return "Secret Data";
}Solution
Step 1: Understand role checking with @Secured
The annotation allows only users with rolesROLE_USERorROLE_ADMIN.Step 2: Check user role
User hasROLE_GUEST, which is not allowed, so access is denied.Final Answer:
Access denied error is thrown -> Option DQuick Check:
User role mismatch causes denial [OK]
- Assuming method runs for any role
- Thinking method returns null on denial
- Confusing role names
Identify the error in this usage of @Secured:
@Secured("ROLE_ADMIN")
public void adminTask() { }Solution
Step 1: Check @Secured syntax
@Securedexpects an array of roles, so roles must be inside curly braces{}.Step 2: Analyze given code
Here, roles are given as a single string without braces, causing syntax error.Final Answer:
Missing curly braces around roles array -> Option BQuick Check:
@Secured requires roles in braces [OK]
- Omitting braces for single role
- Removing ROLE_ prefix
- Confusing @Secured with @RolesAllowed
You want to secure two methods: one accessible only by ROLE_ADMIN, and another accessible by either ROLE_USER or ROLE_MANAGER. Which is the correct way to annotate these methods?
Method 1:
@Secured({"?"})
public void adminOnly() { }
Method 2:
@Secured({"?"})
public void userOrManager() { }Solution
Step 1: Secure Method 1 for ROLE_ADMIN only
Use@Secured({"ROLE_ADMIN"})to restrict access to admins.Step 2: Secure Method 2 for ROLE_USER or ROLE_MANAGER
Use@Secured({"ROLE_USER", "ROLE_MANAGER"})to allow either role.Final Answer:
{"ROLE_ADMIN"} and {"ROLE_USER", "ROLE_MANAGER"} -> Option CQuick Check:
Use arrays with ROLE_ prefix for multiple roles [OK]
- Omitting ROLE_ prefix
- Using pipe '|' inside role strings
- Mixing roles in one annotation incorrectly
