This type of annotation is the most powerful, supporting complex authorization judgments using Spring EL expressions.
Built-in Expressions
The base class for expression root objects is org.springframework.security.access.expression.SecurityExpressionRoot.
Expression
Description
hasRole([role])
Checks if the user has the specified role. If the parameter role does not start with “ROLE_”, the prefix is added by default. The default prefix can be customized by modifying the defaultRolePrefix property in DefaultWebSecurityExpressionHandler.
hasAnyRole([role1,role2])
Checks if the user has any of the specified roles. Default prefix is the same as above.
hasAuthority([authority])
Checks if the user has the specified authority.
hasAnyAuthority([authority1,authority2])
Checks if the user has any of the specified authorities.
principal
Represents the current user’s principal object.
authentication
Gets the current Authentication object from SecurityContext.
permitAll
Always returns true. Allows anyone to access.
denyAll
Always returns false. Denies access to everyone.
isAnonymous()
Checks if the current principal is an anonymous user.
isRememberMe()
Checks if the current principal is a “remember-me” user
isAuthenticated()
Checks if the current principal is not an anonymous user.
isFullyAuthenticated()
Returns true if the current principal is neither an anonymous nor a “remember-me” user.
hasPermission(Object target, Object permission)
Returns true if the user has the specified permission for the given target. Requires custom implementation.
When using @PreFilter or @PostFilter annotations, the built-in name filterObject can be used to represent individual objects in method parameters or return value collections.
@PreFilter - Parameter Filtering
Filters method parameters. When there are multiple collection-type parameters, use the filterTarget attribute to specify the target collection.
/** * Check if the current user is an admin * @return true: yes; false: no */ @Override publicbooleanisAdmin() { return"admin".equals( ((User) SecurityContextHolder.getContext().getAuthentication()).getUsername() ); } }
2. Use in Method
1 2 3 4 5 6 7 8 9 10
/** * Query data by ID * @param id ID * @return Result */ @GetMapping(path = "/{id}") @PreAuthorize("@myService.isAdmin()") public ResponseEntity<Object> queryById(@PathVariable("id") Long id) { // ... }
hasPermission() Built-in Expression
Working Principle
The hasPermission() built-in expression delegates to an instance of org.springframework.security.access.PermissionEvaluator for processing.
@Bean public PermissionEvaluator permissionEvaluator() { returnnewMyPermissionEvaluator(); } }
3. Usage Example
1 2 3 4 5 6 7 8 9 10
/** * Query data by ID * @param id ID * @return Result */ @GetMapping(path = "/{id}") @PreAuthorize("hasPermission(#id, 'myType', '')") public ResponseEntity<Object> queryById(@PathVariable("id") Long id) { // ... }
Comparison of Three Annotation Types
Feature
JSR-250 (@RolesAllowed)
@Secured
Spring EL (@PreAuthorize)
Standardization
Java EE Standard
Spring Proprietary
Spring Proprietary
Expression Capability
Role-only checks
Role-only checks
Arbitrary Spring EL expressions
Authorization Judgment
Simple role checking
Simple role checking
Complex business logic
Flexibility
Low
Medium
High
Recommended Scenario
Simple role control
Simple role control
Complex business permissions
Usage Recommendations
Simple role control: Use @RolesAllowed or @Secured
Complex authorization control: Use @PreAuthorize with custom PermissionEvaluator
Authorization expressions: Fully utilize built-in expressions like hasRole(), hasAnyRole(), hasAuthority()
Bean references: For complex business permission checks, use Spring Bean method references