Performance: @Secured annotation
This annotation affects server-side request handling speed and security checks, impacting response time and user interaction delay.
Jump into concepts and practice - no test required
@Secured("ROLE_ADMIN")
public void process() {
// method logic
}@Secured({"ROLE_ADMIN", "ROLE_USER"})
public void process() {
// method logic
}| Pattern | Security Checks | Request Delay | User Interaction Delay | Verdict |
|---|---|---|---|---|
| Multiple roles in @Secured | Multiple role checks | Increased by ~1-3ms | Slightly higher INP | [!] OK |
| Single role in @Secured | Single role check | Minimal delay | Better INP | [OK] Good |
What is the main purpose of the @Secured annotation in Spring Boot?
@Secured@Secured annotation is used to limit method access to users with specific roles.@Secured controls method access by roles [OK]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() { }ROLE_, so ROLE_ADMIN is correct.ADMIN without prefix is invalid; ROLE-ADMIN uses wrong separator; ROLE_ADMINISTRATOR is a different role.ROLE_ prefix [OK]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";
}ROLE_USER or ROLE_ADMIN.ROLE_GUEST, which is not allowed, so access is denied.Identify the error in this usage of @Secured:
@Secured("ROLE_ADMIN")
public void adminTask() { }@Secured expects an array of roles, so roles must be inside curly braces {}.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() { }@Secured({"ROLE_ADMIN"}) to restrict access to admins.@Secured({"ROLE_USER", "ROLE_MANAGER"}) to allow either role.