In Spring Security, access() delegates endpoint authorization to an authorization decision function or manager. It supports custom AuthorizationManager implementations and SpEL expressions when simpler authority or role checks are insufficient. section 7.1.1
Choose the authorization mechanism according to how complex the rule needs to be:
- Prefer hasAuthority() for one authority and hasAnyAuthority() for several alternatives; these rules remain comparatively easy to read. section 7.1.1
- Use access() with WebExpressionAuthorizationManager when a rule requires SpEL or a custom authorization decision. section 7.1.1
- Apply the same fallback approach to roles: prefer hasRole() or hasAnyRole(), and use access() when those methods do not express the requirement. section 7.1.2
A compound endpoint rule
A security filter chain can require read access while rejecting users with delete authority: http.authorizeHttpRequests(c -> c.anyRequest().access("hasAuthority('read') and !hasAuthority('delete')")); A user with only read authority succeeds, while a user with read, write, and delete authorities receives HTTP 403 Forbidden. section 7.1.1
Roles remain GrantedAuthority values internally and use the ROLE_ prefix when declared. Expressions normally name the role without that prefix, such as access("hasRole('ADMIN')"). section 7.1.2
Figure 7.4 Authorities define the permissible operations users can execute within the application. These operations inform the creation of authorization protocols, restricting certain requests to endpoints to users with designated authorities. For instance, Jane is limited to reading and writing at the endpoint, whereas John has the capacity to read, write, delete, and modify at that endpoint.
Authorities define permissible endpoint operations, while roles provide coarser-grained purpose-based access; access() can express rules over either model. section 7.1.1 section 7.1.2
