liveBook · Manning

GrantedAuthority

As explained in Spring Security in Action, Second Edition

GrantedAuthority is a Spring Security interface representing an authority granted to an authenticated principal.

GrantedAuthority is a Spring Security interface representing a privilege an authenticated user may exercise. Each user has one or more authorities, which later support authorization decisions. section 3.1 section 3.2.2

Define an authority by giving its privilege a name:

  • Implement getAuthority() to return the authority name as a String. section 3.2.2
  • Use a lambda expression for a small implementation, such as () -> "READ". section 3.2.2
  • Use SimpleGrantedAuthority to create an immutable authority instance from a name. section 3.2.2
  • Return the authorities through UserDetails.getAuthorities() so they become part of the authenticated user's details. section 7.1

A user with a READ authority

public class DummyUser implements UserDetails { @Override public Collection<? extends GrantedAuthority> getAuthorities() { return List.of(() -> "READ"); } } section 3.2.3

public interface GrantedAuthority extends Serializable {
   String getAuthority();
}

The contract contains one abstract method and extends Serializable. section 3.2.2

How Spring Security in Action, Second Edition covers it

How deep it goes
Defines one method and demonstrates lambda and SimpleGrantedAuthority implementations, but defers broader authorization configuration.
Examples
Uses READ, read, write, and delete privileges, plus lambda, SimpleGrantedAuthority, and DummyUser implementations.
What it assumes
Assumes basic Spring Security user definitions and introduces authorities alongside the UserDetails contract.
What you can do after
Apply it via UserDetails and authorization rules to distinguish permissible user actions.
How it connects
Connects UserDetails, UserDetailsService, authentication, authorization, roles, and endpoint access rules.

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.