liveBook · Manning

Role

As explained in Spring Security in Action, Second Edition

A role is a named grouping of permissions or authorities used to represent an actor's access level.

A role is a coarse-grained label that groups permissions for an authenticated user. Spring Security evaluates roles during authorization, after authentication has placed the user's details in the security context. section 7.0 section 7.1.2

Use roles when users repeatedly receive the same collection of actions:

  • Represent fine-grained actions such as read, write, update, or delete as authorities. section 7.1
  • Group those actions under labels such as READER, MANAGER, or ADMIN. section 7.1.2
  • Store the role through the user's authorities; internally, roles use the GrantedAuthority contract and normally carry the ROLE_ prefix. section 7.1.2
  • Authorize an endpoint with hasRole("ADMIN"), allowing only users assigned the ADMIN role. section 7.1.2

Restrict an endpoint to administrators

Assign john the ADMIN role and jane the MANAGER role with roles("ADMIN") and roles("MANAGER"). Configure authorizeHttpRequests with anyRequest().hasRole("ADMIN"). A request authenticated as john succeeds, while jane receives HTTP 403 Forbidden. section 7.1.2

Figure 7.5 Roles are coarse grained. Each user with a specific role can only do the actions granted by that role. When applying this philosophy in authorization, a request is allowed based on the purpose of the user in the system. Only users who have a specific role can call a certain endpoint.

Visualize a role as a coarse-grained badge: each assigned role permits only the actions granted by that role and can control access to an endpoint. section 7.1.2

How Spring Security in Action, Second Edition covers it

How deep it goes
Explains role grouping, GrantedAuthority representation, ROLE_ naming, Java configuration, and a successful-versus-forbidden request test.
Examples
Uses READER, MANAGER, and ADMIN roles, plus john and jane requests demonstrating successful access and HTTP 403 Forbidden.
What it assumes
Assumes basic authentication and UserDetails knowledge while connecting authenticated users with their GrantedAuthority collection.
What you can do after
Apply it via Spring Security by assigning roles and configuring hasRole rules for protected endpoints.
How it connects
Connects roles with authorization, authentication, authorities, GrantedAuthority, UserDetails, UserDetailsService, and endpoint security.

Also explained in

Compare how 11 books explain role →

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.