UserDetailsService is the Spring Security interface that loads a user's account data by username. Its single method, loadUserByUsername, returns a UserDetails object holding the username, the encoded password, the granted authorities and account status flags. Spring Security's DaoAuthenticationProvider calls it during username and password authentication and then checks the submitted password against the stored hash.
If you define a UserDetailsService bean, Spring Boot uses it automatically, which makes it the standard way to plug your own user store (database, LDAP gateway, remote service) into form login or HTTP Basic.
The interface lives in org.springframework.security.core.userdetails and has one method:
It does not check the password itself. Its job is only to find the user and return what Spring Security needs to decide:
getUsername() and getPassword() (the stored, encoded password)getAuthorities() (roles and permissions)isEnabled(), isAccountNonLocked(), isAccountNonExpired(), isCredentialsNonExpired()If the user does not exist, throw UsernameNotFoundException. By default DaoAuthenticationProvider hides this and reports a generic bad-credentials failure, so attackers cannot tell whether a username exists.
AuthenticationFilter creates an UsernamePasswordAuthenticationToken and passes it to the AuthenticationManager.ProviderManager delegates to DaoAuthenticationProvider.UserDetailsService.loadUserByUsername.PasswordEncoder and checks the account status flags.Authentication with the user's authorities is stored in the SecurityContext.The PasswordEncoder is mandatory. Passwords in storage must be hashed, for example with BCryptPasswordEncoder or the delegating encoder from PasswordEncoderFactories.
This example uses Spring Boot 3 with Spring Security 6 and Spring Data JPA:
User is Spring Security's built-in UserDetails implementation, so you do not have to write your own unless you need extra fields such as a tenant id. Roles checked with hasRole("ADMIN") must be stored with the ROLE_ prefix as authorities, or you can use .roles("ADMIN") on the builder, which adds the prefix for you.
Since Spring Security 5.7 the WebSecurityConfigurerAdapter is deprecated, and it is removed in 6.0. You configure a SecurityFilterChain bean and a PasswordEncoder bean instead:
With one UserDetailsService bean and one PasswordEncoder bean in the context, Spring Boot builds the DaoAuthenticationProvider for you. You do not call auth.userDetailsService(...) as older tutorials show.
Spring Security ships ready-made implementations:
| Implementation | Use |
|---|---|
InMemoryUserDetailsManager | Demos, tests, a few fixed accounts |
JdbcUserDetailsManager | Default JDBC schema with users and authorities tables |
| Custom class (above) | Your own tables, LDAP, or a remote user service |
For tests, @WithMockUser creates a fake authenticated user without calling your service, and @WithUserDetails("alice") loads the real user through your UserDetailsService.
No. It only loads the user. DaoAuthenticationProvider compares the submitted password with the stored one through the PasswordEncoder.
Not for validating a token that already contains the identity. You can still use it in a JWT filter to load the user's authorities, but resource servers typically build authorities from token claims.
The stored password has no encoding prefix and you use the delegating encoder. Store hashes created by your encoder, for example {bcrypt}..., or use BCryptPasswordEncoder directly.
That is your choice, but be consistent with how usernames are stored and enforce uniqueness in the database to avoid duplicate accounts.
UserDetailsService is the lookup hook Spring Security uses to fetch a user by username during authentication. Implement it to load users and authorities from your data source, return encoded passwords, throw UsernameNotFoundException when no user exists, and register a PasswordEncoder bean. In Spring Security 6, expose a SecurityFilterChain bean and let Boot wire the provider.