liveBook · Manning

Access

As explained in Spring Security in Action, Second Edition

access() is an authorization configuration method that delegates request authorization to a supplied authorization decision function or manager.

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

How Spring Security in Action, Second Edition covers it

How deep it goes
Explains AuthorizationManager delegation, SpEL expressions, role handling, and tested HTTP outcomes, but defers custom manager implementation details.
Examples
Uses Spring Java security configurations, in-memory users, curl requests, compound SpEL expressions, and HTTP 403 responses.
What it assumes
Assumes basic Spring Security configuration and user authorities, while demonstrating role prefixes and access expressions explicitly.
What you can do after
Apply it via Spring Security to express compound authority or role rules for protected endpoints.
How it connects
Connects endpoint authorization with AuthorizationManager, SpEL, authorities, roles, GrantedAuthority, hasAuthority(), and hasRole().

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Security in Action, Second Edition. The section links point to the parts of the book it draws on.