UserDetailsService is the Spring Security interface that loads a user by username. It has one method, loadUserByUsername, which returns a UserDetails object. DaoAuthenticationProvider calls it during username and password login to get the stored password hash and authorities.
Implementing it, and exposing it as a bean, is how you make Spring Security authenticate against your own database.
The contract is a single method.
It must return a fully populated UserDetails, or throw UsernameNotFoundException when no such user exists. It does not check the password itself. The authentication provider compares the submitted password with the returned hash using a PasswordEncoder.
This example loads users through a Spring Data JPA repository and maps them to the built-in User class.
Keep the error message generic. Spring Security hides whether the username or the password was wrong by default, and revealing it helps attackers enumerate accounts.
In Spring Security 6 (Spring Boot 3 and later) you define a SecurityFilterChain and a PasswordEncoder bean. If exactly one UserDetailsService bean exists, Spring Security picks it up automatically.
The old pattern of extending WebSecurityConfigurerAdapter was removed in Spring Security 6, so do not use it in new code.
| Class | Use |
|---|---|
InMemoryUserDetailsManager | Demos and tests with a few fixed users |
JdbcUserDetailsManager | JDBC storage using the standard schema, with create and update support |
UserDetailsManager | Interface adding create, update, delete and change-password operations |
For anything beyond a prototype, write your own implementation against your real user model.
UserDetailsService is specific to username and password authentication. For JWT bearer tokens, OAuth 2 login and other schemes, authentication goes through different providers, though you may still map the token to local authorities. If you need to update a stored password when the encoding becomes outdated, implement UserDetailsPasswordService as well.
null instead of throwing UsernameNotFoundException.{bcrypt} style prefix required by the delegating encoder.UserDetailsService beans without configuring an AuthenticationManager explicitly.No. It only loads the user. The DaoAuthenticationProvider compares passwords with the PasswordEncoder.
Throw UsernameNotFoundException. By default Spring Security converts it into a generic bad credentials error for the client.
Yes. The method parameter is just the identifier the login form submits, so look the user up by email.
In most Spring Boot applications no. A single UserDetailsService bean is detected automatically.
UserDetailsService is the single hook that tells Spring Security how to find users. Implement loadUserByUsername, return a UserDetails with an encoded password and authorities, expose it as a bean, and pair it with a PasswordEncoder.