UserDetails is the Spring Security interface that represents a user as the framework sees it: a username, an encoded password, a collection of granted authorities, and four account status flags. Authentication providers load a UserDetails object, compare the password, and use its authorities for authorization.
You implement it, or use the built-in User class, to connect Spring Security to your own user store.
Spring Security does not know about your user table. UserDetails is the adapter that gives it the minimum it needs.
| Method | Meaning |
|---|---|
getUsername() | Unique login identifier |
getPassword() | Stored password hash, never plain text |
getAuthorities() | Collection<? extends GrantedAuthority> such as ROLE_ADMIN |
isAccountNonExpired() | false blocks login for an expired account |
isAccountNonLocked() | false blocks a locked account |
isCredentialsNonExpired() | false forces a password change |
isEnabled() | false blocks a disabled account |
Since Spring Security 5.x the last four methods are default methods returning true, so you override only those you need.
The flow for a username and password login is:
Authentication with the submitted credentials.DaoAuthenticationProvider calls UserDetailsService.loadUserByUsername, which returns a UserDetails.getPassword() using the configured PasswordEncoder.Authentication whose principal is the UserDetails and whose authorities come from getAuthorities().Later, the principal is available in controllers with @AuthenticationPrincipal.
For simple cases you do not need your own class. org.springframework.security.core.userdetails.User implements the interface.
Do not use User.withDefaultPasswordEncoder() outside demos; it is deprecated and marked unsafe for production.
A common design is a separate class that wraps your JPA entity, so the entity stays free of security types.
The extra getId() method lets controllers read the user id from the principal without another database query.
A GrantedAuthority is just a string. By convention, a role is an authority prefixed with ROLE_. The hasRole("ADMIN") check looks for ROLE_ADMIN, while hasAuthority("ROLE_ADMIN") or hasAuthority("report:read") compares the exact string. Use roles for broad groups and authorities for fine-grained permissions.
BCryptPasswordEncoder or DelegatingPasswordEncoder, never the raw password.Authentication after login by default, but your own UserDetails object may still hold the hash in memory, so avoid logging it.Serializable or keep it small.No. The User class works for simple cases. Implement your own when you need extra fields such as an id or tenant.
It works, but a separate wrapper keeps persistence and security concerns apart and avoids exposing the password hash by accident.
A role is an authority with the ROLE_ prefix. hasRole("X") adds the prefix for you, hasAuthority("X") does not.
Through a UserDetailsService, which loads it by username during authentication.
UserDetails is the bridge between your user data and Spring Security. Provide the username, a hashed password, authorities and account flags, and the framework handles the rest of authentication and authorization.