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
