liveBook · Manning

UserDetails

As explained in Spring Security in Action, Second Edition

UserDetails is a Spring Security representation of a user's identity, password, and granted authorities.

UserDetails is the Spring Security contract for representing a user during authentication and authorization. An application class implements it so the framework can understand that user. section 3.2 section 3.2.1

Implement the contract through these responsibilities:

  • Return the username and password used during authentication. section 3.2.1
  • Return one or more GrantedAuthority instances representing permitted actions or roles. section 3.2.1 section 7.1
  • Report whether the account, credentials, and user account are expired, locked, or enabled. section 3.2.1
  • Return true for the four status methods when the application does not implement expiration, locking, or disabling rules. section 3.2.1

UserDetails supplies the user object that UserDetailsService retrieves by username. After authentication, its authorities support authorization decisions, while UserDetailsManager adds user-management operations around the retrieval contract. section 3.1 section 3.4

A practical user implementation

A SimpleUser stores username and password in constructor fields, returns them from the matching methods, supplies authorities through getAuthorities(), and implements the four status methods. A minimal DummyUser can represent bill with password 12345 and READ authority, but a parameterized class supports multiple users. section 3.2.3

Listing 3.1 The UserDetails interface
public interface UserDetails extends Serializable {
  String getUsername();                                        #1
  String getPassword();
  Collection<? extends GrantedAuthority> getAuthorities();     #2
  boolean isAccountNonExpired();                               #3
  boolean isAccountNonLocked();
  boolean isCredentialsNonExpired();
  boolean isEnabled();
}

Inspect the complete UserDetails interface, including authentication values, authorities, and account-status methods. section 3.2.1

How Spring Security in Action, Second Edition covers it

How deep it goes
Explains every interface method and demonstrates minimal and parameterized implementations, but defers password encoding details.
Examples
Uses DummyUser, SimpleUser, an in-memory user service, a john user, and authenticated cURL requests to test endpoint access.
What it assumes
Assumes basic Java and Spring knowledge while teaching the UserDetails contract and user representation from the beginning.
What you can do after
Build it from scratch: implement UserDetails, provide authorities and status rules, register users, and test authenticated endpoint access.
How it connects
Connects UserDetails with UserDetailsService, UserDetailsManager, GrantedAuthority, PasswordEncoder, AuthenticationProvider, and authorization.

Also explained in

Compare how 2 books explain UserDetails →

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.