๐Ÿ”ด The Error You're Seeing

Confirm this matches your console output. If it does, you're in the right place.

ERROR LOGorg.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)

Solution 1โœ“ Most common cause

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(); }
Solution 2

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; }
Solution 3

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(); }
Solution 4

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) { ... } }
Solution 5

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

Spring Boot 2.x

WebSecurityConfigurerAdapter auto-configured the AuthenticationManager.

Spring Boot 3.x

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.

Course Search
Search across all chapters & stages
๐Ÿ“–

Search the course

Type any topic โ€” branching, stash, rebase, hooks โ€” and jump straight to that chapter.

merge branchesgit stashundo commitrebase