๐ด The Error You're Seeing
Confirm this matches your console output. If it does, you're in the right place.
org.springframework.security.authentication.ProviderNotFoundException: No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:202)
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:183)โก Quick Fix Works 80% of the time
Expose an AuthenticationManager bean and ensure a DaoAuthenticationProvider is configured.
@Bean
public AuthenticationManager authManager(HttpSecurity http, UserDetailsService uds, PasswordEncoder encoder) throws Exception {
AuthenticationManagerBuilder builder = http.getSharedObject(AuthenticationManagerBuilder.class);
builder.userDetailsService(uds).passwordEncoder(encoder);
return builder.build();
}๐ง Why this Happens
Tap to expand the deep technical explanation
You created a custom login endpoint (e.g., `/api/login`) and injected an `AuthenticationManager` to verify the credentials. However, Spring Security's ProviderManager iterated through its registered `AuthenticationProvider`s and couldn't find one that supports `UsernamePasswordAuthenticationToken`. This happens if you built a custom SecurityFilterChain but forgot to register a `DaoAuthenticationProvider`.
The HITEC City Parking Spot Analogy:
Imagine showing up at an airport check-in desk with a train ticket. The desk attendant (ProviderManager) looks at all the systems they have available (airlines) and says, 'I don't have any system that can process a train ticket.'
๐ How to Reproduce Confirm this is your error
Create a custom SecurityFilterChain. Do not configure any form login or AuthenticationProvider. Inject `AuthenticationManager` into a controller and call `authManager.authenticate(new UsernamePasswordAuthenticationToken(...))`.
๐ ๏ธ Solutions (5 Ways to Fix)
Expose AuthenticationManager via HttpSecurity
๐ Use this when building a custom login controller in Spring Security 6.
In Spring Security 6, you must explicitly build the AuthenticationManager from the HttpSecurity shared object to ensure default providers are wired up.
@Bean
public AuthenticationManager authManager(HttpSecurity http, UserDetailsService uds, PasswordEncoder encoder) throws Exception {
AuthenticationManagerBuilder builder = http.getSharedObject(AuthenticationManagerBuilder.class);
builder.userDetailsService(uds).passwordEncoder(encoder);
return builder.build();
}Define a DaoAuthenticationProvider Bean
๐ Use this if you prefer explicit bean configuration over HttpSecurity builders.
Manually create the provider that handles username/password authentication and expose it as a bean.
@Bean
public AuthenticationProvider authProvider(UserDetailsService uds, PasswordEncoder encoder) {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setUserDetailsService(uds);
provider.setPasswordEncoder(encoder);
return provider;
}Use Spring Boot Default Form Login
๐ Use this if you don't need a custom JSON login API.
If you configure `http.formLogin()`, Spring Boot auto-configures the DaoAuthenticationProvider for you.
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(auth -> auth.anyRequest().authenticated())
.formLogin(form -> form.permitAll());
return http.build();
}Ensure UserDetailsService bean exists
๐ Use this if you configured the provider but it still fails.
The DaoAuthenticationProvider needs a UserDetailsService to fetch users from the DB. If it's missing, the provider cannot be initialized.
@Service
public class CustomUserDetailsService implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String username) { ... }
}Check for profile mismatches
๐ Use this if your config works locally but fails in prod.
Ensure your SecurityConfig class is not annotated with `@Profile("dev")` if you are running in production, as the bean won't be created.
// Remove @Profile("dev") if this config is needed globally
@Configuration
public class SecurityConfig { ... }๐ Version Notes
WebSecurityConfigurerAdapter auto-configured the AuthenticationManager.
Adapter removed. AuthenticationManager must be explicitly exposed as a Bean.
๐ก๏ธ How to Prevent This Next Time
When building custom login endpoints, always map out the Authentication flow: Controller -> AuthenticationManager -> AuthenticationProvider -> UserDetailsService.