Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How do you implement custom authentication providers in Spring Security?

Java
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 22, 2024
UpdatedNov 16, 2026
6 min read

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.

Table of Contents

  • What an AuthenticationProvider does
  • Writing the custom provider
  • Registering the provider
  • Providing the UserDetailsService
  • Testing the provider
  • Frequently Asked Questions
    • Should I extend DaoAuthenticationProvider instead?
    • What happens when I register several providers?
    • How do I support a custom token such as an API key?
    • Why is my provider never called?
  • Summary

What an AuthenticationProvider does

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:

ResultHow to signal it
Credentials validReturn a fully authenticated Authentication with authorities
Credentials invalidThrow an AuthenticationException such as BadCredentialsException
Cannot handle this tokenReturn 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.

Writing the custom provider

This provider checks a password through a UserDetailsService, and adds a rule that disabled tenants cannot log in:

java

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.
  • Use the factory method UsernamePasswordAuthenticationToken.authenticated(...), available since Spring Security 5.7. It makes the "authenticated" state explicit.
  • Pass null as the credentials of the returned token so the password is not kept in memory.
  • Always compare with a PasswordEncoder, never with equals on plain text.

Registering the provider

java

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:

java

Providing the UserDetailsService

The provider above needs a UserDetailsService. A minimal in-memory version for tests:

java

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.

Testing the provider

Use spring-security-test with MockMvc:

java

You can also unit test the provider by calling authenticate with a UsernamePasswordAuthenticationToken.unauthenticated(user, password) and asserting the exception or result.

Frequently Asked Questions

Should I extend 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.

What happens when I register several providers?

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.

How do I support a custom token such as an API key?

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.

Why is my provider never called?

The usual reasons are that it is not registered on the chain, or supports returns false for the token type your filter creates.

Summary

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.

Share