AuthenticationProvider
About
The AuthenticationProvider
is a core component in Spring Security that performs actual authentication logic. It is responsible for validating user credentials and returning an Authentication
object if authentication is successful.
Unlike AuthenticationManager
, which acts as a delegator, the AuthenticationProvider
actually implements authentication logic. Multiple AuthenticationProvider
instances can be registered to support different authentication mechanisms (e.g., username/password, JWT, OAuth2, LDAP, etc.).
Responsibilities of AuthenticationProvider
Processes authentication requests received from
AuthenticationManager
.Verifies credentials using
UserDetailsService
, an external system, or a database.Returns a valid Authentication object on success or throws an exception on failure.
Supports multiple authentication mechanisms via different implementations.
Customizable to support custom authentication logic.
Authentication Flow in Spring Security
1. User submits credentials
A user submits a login request with username and password.
2. AuthenticationManager delegates to AuthenticationProvider
Spring Security's AuthenticationManager
calls one of its registered AuthenticationProvider
instances.
3. AuthenticationProvider verifies credentials
Extracts credentials from
Authentication
object.Checks username against the database.
Verifies the password using
PasswordEncoder
.
4. AuthenticationProvider returns Authentication object
If the credentials are valid, it returns an Authentication
object that contains the authenticated user's details.
5. SecurityContext stores Authentication object
If authentication is successful, the result is stored in SecurityContextHolder
:
Built-in AuthenticationProvider Implementations
Spring Security provides multiple built-in AuthenticationProvider
implementations:
AuthenticationProvider
Description
DaoAuthenticationProvider
Standard provider for username/password authentication using UserDetailsService
.
LdapAuthenticationProvider
Authentication against an LDAP directory.
JwtAuthenticationProvider
Custom provider for JWT-based authentication.
OAuth2AuthenticationProvider
Handles OAuth2 authentication.
CasAuthenticationProvider
Authentication via Central Authentication Service (CAS).
SamlAuthenticationProvider
Authentication via SAML 2.0.
ActiveDirectoryLdapAuthenticationProvider
Microsoft Active Directory authentication.
1. DaoAuthenticationProvider (Username/Password Authentication)
The most commonly used authentication provider in Spring Security. It retrieves user details from UserDetailsService
and verifies the password using PasswordEncoder
.
Default DaoAuthenticationProvider Implementation
Spring Boot automatically configures a DaoAuthenticationProvider
when UserDetailsService
and PasswordEncoder
are available.
2. Custom Authentication Provider (Example: Hardcoded Credentials)
If we need a custom authentication logic, implement AuthenticationProvider
manually.
This provider authenticates only if the username is "admin" and password is "password".
If credentials are incorrect, it throws
BadCredentialsException
.It registers itself as an
AuthenticationProvider
by being annotated with@Component
.
3. Custom Authentication Provider for JWT Authentication
If using JWT authentication, a custom AuthenticationProvider
can validate JWT tokens instead of username/password.
Extracts the username from the JWT token.
Validates the token using
JwtUtil
.Returns an authenticated
Authentication
object if the token is valid.
Spring Boot 2 vs Spring Boot 3 AuthenticationProvider Configuration
Spring Boot 2 Configuration (Extends WebSecurityConfigurerAdapter)
Uses
WebSecurityConfigurerAdapter
(Deprecated in Spring Security 5.7+).Configures
AuthenticationProvider
insideconfigure()
method.
Spring Boot 3 Configuration (Uses Lambda DSL & Beans)
In Spring Security, the DSL (Domain-Specific Language) is the fluent API style configuration used to define security rules in a more readable and declarative way.
WebSecurityConfigurerAdapter
is removed.AuthenticationManager
is explicitly defined using@Bean
.Uses
SecurityFilterChain
for security configurations.
Last updated
Was this helpful?