Spring Security exposing AuthenticationManager without WebSecurityConfigurerAdapter
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Since WebSecurityConfigurerAdapter was deprecated and removed from the usual configuration style, exposing an AuthenticationManager in Spring Security is now done with beans instead of inheritance. The exact approach depends on whether you want the framework-managed AuthenticationManager or a custom one built from specific authentication providers.
The important shift is conceptual: configure security explicitly with SecurityFilterChain, provider beans, and, when needed, an AuthenticationManager bean derived from AuthenticationConfiguration.
The Simple Way: Ask Spring for It
If Spring Security already knows how to build the authentication manager from your configured providers, the usual approach is:
This is the most common answer when you need to inject AuthenticationManager into a login service or custom authentication endpoint.
Pair It with SecurityFilterChain
Modern Spring Security configuration usually looks like this:
This replaces the old adapter-based override style.
Expose Providers and UserDetails Explicitly
If you want Spring to assemble the manager correctly, provide the pieces as beans.
With these in place, AuthenticationConfiguration can usually build the right manager for you.
When You Need a Custom AuthenticationManager
If you want full control, create one from providers yourself.
This is useful when you need a very specific provider chain or nonstandard authentication flow.
Common Use Case: Login Service
A typical reason to expose the bean is manual authentication in a REST login endpoint.
That still works in the modern component-based style; you just obtain the manager through bean configuration rather than overriding adapter methods.
Testing and Boot Integration
In Spring Boot applications, this bean-based style also makes tests easier to reason about because each security component is explicit. You can replace the UserDetailsService, PasswordEncoder, or even the AuthenticationManager itself in test configuration without subclassing a global adapter.
That is one of the practical benefits of the newer model: fewer magic overrides and more ordinary Spring bean wiring.
Common Pitfalls
The biggest mistake is trying to keep using WebSecurityConfigurerAdapter patterns mentally even after moving to the bean-based approach.
Another common issue is exposing AuthenticationManager from AuthenticationConfiguration without actually registering the providers, UserDetailsService, or password encoder that Spring needs.
People also create a custom AuthenticationManager unnecessarily when the framework can already build one from the configured authentication components.
Finally, do not forget that SecurityFilterChain and authentication-manager exposure solve different concerns. One configures the web filter behavior; the other gives you programmatic access to authentication.
Summary
- Modern Spring Security exposes
AuthenticationManagerthrough beans, notWebSecurityConfigurerAdapter. - The simplest pattern is
configuration.getAuthenticationManager(). - Use
SecurityFilterChainfor HTTP security configuration. - Provide
UserDetailsService, providers, and password encoders as beans. - Build a custom
ProviderManageronly when you need explicit control. - Treat filter-chain configuration and authentication-manager exposure as separate responsibilities.
Related reading
- Spring Security HTTP Basic for RESTFul and FormLogin Cookies for web - Annotations
- Spring Security in Spring Boot 3
- Spring Security LDAP and Remember Me
- Spring Security mapping OAuth2 claims with roles to secure Resource Server endpoints
- Spring security method cannot decide pattern is MVC or not Spring Boot application exception
- Spring Security, Method Security annotation Secured is not working java config
- Spring Test returning 401 for unsecured URLs
- SpringBoot 401 UnAuthorized even with out security

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.