To implement a custom authentication provider in Spring Security, create a class that implements AuthenticationProvider with two methods: authenticate(Authentication), which validates the credentials and returns an authenticated token, and supports(Class<?>), which says which token type it handles. Then register it on the HttpSecurity bean with http.authenticationProvider(...).
The example uses Spring Security 6.x with Spring Boot 3.x. It no longer uses WebSecurityConfigurerAdapter, which was removed, so configuration is done with a SecurityFilterChain bean.
When a request carries credentials, Spring Security wraps them in an Authentication object, for example UsernamePasswordAuthenticationToken, and hands it to the AuthenticationManager. The default manager, ProviderManager, asks each registered provider in order whether it supports the token type, and calls authenticate on the first one that does.
A provider must do one of three things:
| Result | How to signal it |
|---|---|
| Credentials valid | Return a fully authenticated Authentication with authorities |
| Credentials invalid | Throw an AuthenticationException such as BadCredentialsException |
| Cannot handle this token | Return null from authenticate, or false from supports |
Write a custom provider when authentication goes beyond username and password stored in a database, for example an external identity API, a one-time code, an API key or an extra account check. For plain database login, the built-in DaoAuthenticationProvider with a UserDetailsService is enough.
This provider checks a password through a UserDetailsService, and adds a rule that disabled tenants cannot log in:
Points that matter:
loadUserByUsername throws UsernameNotFoundException for unknown users. ProviderManager hides that behind a generic bad-credentials error by default, which avoids leaking which usernames exist.UsernamePasswordAuthenticationToken.authenticated(...), available since Spring Security 5.7. It makes the "authenticated" state explicit.null as the credentials of the returned token so the password is not kept in memory.PasswordEncoder, never with equals on plain text.Calling http.authenticationProvider(provider) is explicit and works with several providers. Spring Security also picks up a single AuthenticationProvider bean automatically when you define no AuthenticationManager bean, but being explicit avoids surprises.
If you need to call the manager yourself, for example in a login controller, expose it:
The provider above needs a UserDetailsService. A minimal in-memory version for tests:
In production, load users from a database through a JPA repository and keep password hashes with a strong algorithm such as bcrypt or Argon2, which the delegating encoder supports.
Use spring-security-test with MockMvc:
You can also unit test the provider by calling authenticate with a UsernamePasswordAuthenticationToken.unauthenticated(user, password) and asserting the exception or result.
DaoAuthenticationProvider instead?If you only need to tweak password checking or add pre and post checks, extending or configuring DaoAuthenticationProvider saves work. Implement AuthenticationProvider directly when the credentials come from somewhere else.
ProviderManager tries them in order and uses the first that supports the token and does not return null. If one throws an AuthenticationException, it records it and may try the next, depending on the exception type.
Create your own Authentication subclass, add a filter that builds it from the request, and return true from supports for that class in your provider.
The usual reasons are that it is not registered on the chain, or supports returns false for the token type your filter creates.
A custom AuthenticationProvider validates credentials in authenticate, declares its token type in supports, and is registered with http.authenticationProvider. Use a PasswordEncoder, return an explicit authenticated token, throw AuthenticationException subclasses on failure, and test both paths.