Performance: @PreAuthorize annotation
This affects server-side request authorization speed and response time, impacting how quickly secured endpoints respond.
Jump into concepts and practice - no test required
@PreAuthorize("hasAnyRole('ADMIN', 'USER', 'MANAGER', 'SUPERVISOR')")
public ResponseEntity<?> getData() { ... }@PreAuthorize("hasRole('ADMIN') or hasRole('USER') or hasRole('MANAGER') or hasRole('SUPERVISOR')")
public ResponseEntity<?> getData() { ... }| Pattern | Authorization Checks | CPU Usage | Response Latency | Verdict |
|---|---|---|---|---|
| Multiple OR conditions in @PreAuthorize | Multiple per request | High | Increased | [X] Bad |
| Using hasAnyRole in @PreAuthorize | Single per request | Low | Reduced | [OK] Good |
@PreAuthorize annotation in Spring Boot?@PreAuthorize@PreAuthorize.@PreAuthorize controls access before method runs [OK]@PreAuthorize with logging or exception handling@PreAuthorize?hasRole('ADMIN') checks if the user has the 'ADMIN' role.hasAuthority('USER') checks for a different role, permitAll() allows everyone, and denyAll() denies everyone.hasRole('ROLE_NAME') to restrict by role [OK]hasRole with hasAuthoritypermitAll() when restriction is needed@PreAuthorize("hasRole('ADMIN')")
public String adminOnly() {
return "Welcome Admin";
}@PreAuthorize blocks method [OK]@PreAuthorize:@PreAuthorize("hasRole(ADMIN)")
public void secureMethod() { }hasRole expressionhasRole('ADMIN').@PreAuthorize is correct annotation, so no other errors.hasRole() [OK]@PreAuthorize with @PostAuthorize@PreAuthorize to allow access only if the user has either 'ADMIN' role or 'MANAGER' authority?@PreAuthorizeor to allow access if either condition is true.or correctly; @PreAuthorize("hasRole('ADMIN') and hasAuthority('MANAGER')") requires both roles which is stricter; @PreAuthorize("hasRole('ADMIN')") @PreAuthorize("hasAuthority('MANAGER')") is invalid to use two annotations; @PreAuthorize("permitAll()") allows everyone.@PreAuthorize [OK]@PreAuthorize annotationspermitAll() which allows everyone