Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching authentication approaches at runtime with Spring Security?

Typically, when you declare different "<authentication-provider>" for your application (webapp in my case), Spring Security takes care of invoking providers one after another, incase of failure. So, say I have DatabaseAuthenticationProvider and LDAPAuthenticationProvider with DatabaseAuthenticationProvider declared first in the config file, at runtime, DatabaseAuthenticationProvider is invoked first and if authentication fails, LDAPAuthentication is tried. This is cool - However, what I need is a runtime switch.

I would like to have an option of chosing between these two approaches (database based authentication / ldap based authentication) and somehow swith the implementation based on thsi global setting.

How do I do it? Is it even possible with Spring-Security?

like image 961
Jay Avatar asked Feb 17 '10 09:02

Jay


People also ask

How does authentication work in Spring Security?

There are multiple filters in spring security out of which one is the Authentication Filter, which initiates the process of authentication. Once the request passes through the authentication filter, the credentials of the user are stored in the Authentication object.


1 Answers

How about writing a delegating AuthenticationProvider that knows how to access your runtime switch and the actual instances of Database/LDAP AuthenticationProvider.

I'm thinking of something like:

public class SwitchingAuthenticationProvider implements AuthenticationProvider
{
    private List<AuthenticationProvider> delegateList;
    private int selectedProvider;

    @Override
    public Authentication authenticate(Authentication authentication)
        throws AuthenticationException
    {
        AuthenticationProvider delegateTo = delegateList.get(selectedProvider);
        return delegateTo.authenticate(authentication);
    }

    ....
}
like image 90
Matt Avatar answered Oct 06 '22 01:10

Matt