To create a custom user details service in Spring Security, implement the UserDetailsService interface, load the user from your own store in loadUserByUsername, and return a UserDetails object with the password hash and authorities. Then expose the service as a bean and register a PasswordEncoder. Spring Security 6 (Spring Boot 3 and later) picks both up automatically for form login and HTTP Basic authentication.
The interface has a single method, so the work is in mapping your own user model (a JPA entity, a REST API or an LDAP-like source) to the UserDetails contract that the authentication manager understands.
When a user submits credentials, the DaoAuthenticationProvider calls your service to load the stored user by username. It then compares the submitted password with the stored hash using the configured PasswordEncoder. If the match succeeds, the authorities from UserDetails become the granted authorities of the authenticated principal.
Throw UsernameNotFoundException when the user does not exist. Spring Security converts it into a generic bad-credentials failure by default, so the response does not reveal which usernames exist.
With Spring Boot 3.x or 4.x you only need the starters. spring-security-core comes in transitively, so you do not declare it yourself.
The Gradle equivalent is implementation 'org.springframework.boot:spring-boot-starter-security' plus the other two starters.
Create a JPA entity and a Spring Data repository. Store only a password hash, never the plain password. Boot 3 and later use the jakarta.persistence package.
In a larger system you would model roles as a separate table, but the mapping to authorities works the same way.
Annotate the class with @Service and build a UserDetails from the entity. The User builder in org.springframework.security.core.userdetails is the simplest way to do it.
The disabled(true) flag makes Spring Security reject the login with a DisabledException. You can also set accountLocked, accountExpired and credentialsExpired the same way. If you need extra fields such as a tenant ID, implement UserDetails yourself in a small class and return that instead.
Since Spring Security 5.7 you no longer extend WebSecurityConfigurerAdapter; it was removed in 6.0. Declare a SecurityFilterChain bean and a PasswordEncoder bean instead.
When exactly one UserDetailsService bean exists, Spring Boot wires it into the default AuthenticationManager for you. The delegating encoder stores hashes with a prefix such as {bcrypt}, which lets you migrate to a stronger algorithm later without invalidating existing passwords.
Hash passwords when you create users, and use the encoder in your registration code.
| Mistake | Effect | Fix |
|---|---|---|
No PasswordEncoder bean, or a plain-text password in the database | "There is no PasswordEncoder mapped for the id null" | Store hashes produced by the encoder bean |
Roles stored without the ROLE_ prefix but checked with hasRole("ADMIN") | Access denied for valid users | Store ROLE_ADMIN, or use hasAuthority("ADMIN") |
Returning null instead of throwing | NullPointerException or an internal error | Always throw UsernameNotFoundException |
Two UserDetailsService beans | Boot cannot auto-configure the default one | Keep one, or build your own AuthenticationManager |
No. It was deprecated in Spring Security 5.7 and removed in 6.0. Use a SecurityFilterChain bean and the lambda DSL as shown above.
Yes. loadUserByUsername can call any source, as long as it returns a UserDetails with a password hash the encoder understands. Cache the result carefully and keep timeouts short, because it runs on every login.
For resource servers that validate bearer tokens, you usually map claims to authorities with a JwtAuthenticationConverter and do not need UserDetailsService. It remains the right hook for username and password logins.
Implement UserDetailsPasswordService on the same class. Spring Security calls it after a successful login when the encoder reports that the stored hash should be upgraded.
A custom user details service is one class: look the user up, translate it to UserDetails, and throw UsernameNotFoundException when it is missing. Add a PasswordEncoder bean and a SecurityFilterChain, store only hashes, and mind the ROLE_ prefix convention. In Spring Boot 3 and later, a single UserDetailsService bean is picked up automatically.