【问题标题】:How to inject my custom ProviderManager into AuthenticationManagerBuilder如何将我的自定义 ProviderManager 注入 AuthenticationManagerBuilder
【发布时间】:2020-06-05 21:42:24
【问题描述】:

在我的 Spring Boot 应用程序中,我已经自定义了 MyProviderManager,我想在其中控制方法内部的逻辑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;
    }

但是,我现在很难用 AuthenticationManagerBuilder 注入我的自定义 MyProviderManager,以便 AuthenticationManagerBuilderperformBuild() /em> 将返回 MyProviderManager 而不是 Spring Security 的默认值

我什至尝试过将我的自定义 MyAuthenticationManagerBuilder 扩展为 AuthenticationManagerBuilder 并重写 performBuild() 方法,但我遇到了如何将我的自定义 AuthenticationManagerBuilder 注入 Spring 的相同问题开机

如果有人能阐明这里的问题或有更好的替代想法来解决我的特殊要求,我将不胜感激

【问题讨论】:

标签: spring spring-security


【解决方案1】:

如果您有自定义的 AuthenticationManager 实现,则不再使用 AuthenticationManagerBuilder(这将构建 AuthenticationManager 但您已经有了)。而是将 AuthenticationManager 公开为 bean,不要使用 AuthenticationManagerBuilder。

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

【讨论】:

  • 这是否表明我应该不覆盖扩展WebSecurityConfigurerAdapter 的MySecurityConfig 中的protected void configure(AuthenticationManagerBuilder auth) 和protected void configure(AuthenticationManagerBuilder auth)?相反,CustomAuthenticationManager 应该自己实例化所有必要的AuthenticationProvider 列表,对吧?
猜你喜欢
  • 2019-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多