Sam D.
Sam D.

Reputation: 283

How to inject my custom ProviderManager into AuthenticationManagerBuilder

In my Spring Boot application, I've come out my custom MyProviderManager where I'd like to control the logic inside method authenticate

    public Authentication authenticate(Authentication authentication) {
        // instead of iterating in the AuthenticationProvider list one by one
        // I'd rather choose the right AuthenticationProvider based on the currently requested URL path
        RequestDetails requestDetails = authentication.getDetails();
        if ("/ad/sso".equals(requestDetails.getPath())) {
            return adAuthenticationProvider.authenticate(authentication);
        } else if ("/saml/sso".equals(requestDetails.getPath())) {
            return samlAuthenticationProvider.authenticate(authentication);
        } else if ("/oidc/sso".equals(requestDetails.getPath())) {
            return oidcAuthenticationProvider.authenticate(authentication);
        } else  {
            return ldapAuthenticationProvider.authenticate(authentication);
        }
        return null;
    }

However, I'm now having it hard to inject my custom MyProviderManager with AuthenticationManagerBuilder so that the method performBuild() in AuthenticationManagerBuilder will return MyProviderManager instead of the default one from Spring Security

I had even tried to come out my custom MyAuthenticationManagerBuilder exends AuthenticationManagerBuilder and overridden performBuild() method, but I faced the same issue of how to inject my custom AuthenticationManagerBuilder to Spring Boot

It is really appreciate if someone could shed the light on the issues here or have better alternative ideas tackling my special requirements

Upvotes: 1

Views: 1017

Answers (1)

Rob Winch
Rob Winch

Reputation: 21720

If you have a custom AuthenticationManager implementation, you do not use an AuthenticationManagerBuilder anymore (that is what would build an AuthenticationManager but you already have one). Instead expose AuthenticationManager as a bean and do not use AuthenticationManagerBuilder.

@Bean
CustomAuthenticationManager customAuthenticationManager() {
    return new CustomAuthenticationManager();
}

Upvotes: 2

Related Questions