UserDetailsService is the lookup hook of Spring Security's username and password authentication. Given a username, it returns a UserDetails with the stored password hash, authorities and account flags, or it throws UsernameNotFoundException. Spring Security then verifies the password and builds the authenticated principal.
This guide focuses on how the interface fits into the authentication machinery and on the implementations that ship with Spring Security. For a database-backed custom implementation, see the related article on the purpose of the interface.
Spring Security does not know where your users are stored: a database, LDAP, a REST service or a file. UserDetailsService is the single seam that hides that storage. The framework asks one question, "who is this user?", and your code answers with a UserDetails.
Because the interface is small, you can swap storage without touching the rest of the security configuration.
For form login and HTTP Basic, the flow runs through ProviderManager and DaoAuthenticationProvider:
UsernamePasswordAuthenticationToken.ProviderManager hands it to DaoAuthenticationProvider.userDetailsService.loadUserByUsername(username).getPassword() using the PasswordEncoder.UserDetails as the principal and its authorities.SecurityContext, and in a session for session-based apps.If the user is unknown or the password is wrong, the provider throws a BadCredentialsException by default, so the response does not reveal which part was wrong.
UserDetails is the data holder:
roles("ADMIN") becomes the authority ROLE_ADMIN, which is what hasRole("ADMIN") checks. A fine-grained permission such as report:read is added with authorities(...) and checked with hasAuthority("report:read").
| Class | Storage | Use it for |
|---|---|---|
InMemoryUserDetailsManager | Memory | Tests, demos, prototypes |
JdbcUserDetailsManager | SQL tables in the default Spring Security schema | Simple apps that accept that schema |
Your own UserDetailsService | Anything | Custom schemas, JPA, remote services |
Both managers also implement UserDetailsManager, which adds create, update, delete and change-password operations.
A quick in-memory setup:
Use this for local experiments only. Do not ship hard-coded credentials.
A single UserDetailsService bean is enough. Spring Boot's authentication setup finds it and a PasswordEncoder bean and wires DaoAuthenticationProvider for you:
If you need several providers or a custom one, build an AuthenticationManager explicitly. Extra customisation points are UserDetailsPasswordService, which lets the provider upgrade a password hash when the user logs in, and a custom AuthenticationProvider when username and password are not the credentials.
Stateless APIs that validate a JWT or an opaque OAuth2 token in a resource server derive the identity from the token, not from a user lookup. Login with an external identity provider through OAuth2 login also builds the principal from the provider's response. You still often use UserDetailsService for the account that issues tokens or for local users.
You can test your implementation as plain Java:
In MVC tests, @WithMockUser creates an authenticated principal without calling your service at all, so use real login requests in at least one integration test.
UserDetailsService is the loader, and UserDetails is the data it returns for one user.
Only for username and password authentication that reads users from your store. Token-based setups may not need it.
In DaoAuthenticationProvider, with the PasswordEncoder bean. The service only supplies the stored hash.
No. It was removed in Spring Security 6. Declare SecurityFilterChain and UserDetailsService beans.
UserDetailsService connects Spring Security to your user store through one method. DaoAuthenticationProvider calls it, applies the account checks, and verifies the password with the PasswordEncoder. Use the in-memory or JDBC managers for simple cases, or write your own implementation when your data lives elsewhere.