UserDetailsService is the Spring Security interface that loads a user's account data by username. It has one method, loadUserByUsername(String username), which returns a UserDetails object with the password hash, authorities and account status. Spring Security calls it during login and compares the result with the credentials the user submitted.
You implement it when your users live in your own database and you want form login or HTTP Basic to authenticate against them.
The method must return a non-null UserDetails or throw UsernameNotFoundException. The returned object tells Spring Security:
UserDetails method | Meaning |
|---|---|
getUsername() | The identity of the user |
getPassword() | The stored password hash, never the plain text |
getAuthorities() | Roles and permissions as GrantedAuthority values |
isEnabled(), isAccountNonLocked(), isAccountNonExpired(), isCredentialsNonExpired() | Account state checks that can block login |
UserDetailsService only loads data. It does not check the password. That comparison happens elsewhere in the authentication flow.
For username and password login, DaoAuthenticationProvider does the work:
Authentication request.DaoAuthenticationProvider calls your loadUserByUsername.UserDetails.PasswordEncoder.Authentication with the authorities.When a UserDetailsService bean and a PasswordEncoder bean exist, Spring Boot's security configuration uses them without further wiring.
Start with a user entity and repository:
Then the service:
roles(...) adds the ROLE_ prefix for you. If your stored values are already prefixed or are fine-grained permissions, use authorities(...) instead.
Spring Security 6 and later uses a SecurityFilterChain bean, and WebSecurityConfigurerAdapter was removed:
The delegating encoder stores hashes with an algorithm prefix such as {bcrypt}, so you can upgrade the algorithm later without invalidating old hashes. Hash passwords with the same encoder when users register.
You do not always need your own class. Spring Security ships:
InMemoryUserDetailsManager for demos and tests.JdbcUserDetailsManager for the standard users and authorities tables.Write your own when your schema differs, or when you need extra data such as a tenant ID in the returned principal. In that case return your own class that implements UserDetails.
UserDetails is used outside the persistence context.No. It returns the stored hash, and DaoAuthenticationProvider compares it with the submitted password using the PasswordEncoder.
Throw UsernameNotFoundException. Never return null.
Yes. The method parameter is just the identifier the user typed, so query by email in your implementation.
If it is the only UserDetailsService bean, Spring Boot picks it up automatically.
UserDetailsService loads user data by username, and Spring Security does the authentication around it. Implement loadUserByUsername, return a UserDetails with a password hash and authorities, expose a PasswordEncoder bean, and configure rules with a SecurityFilterChain.