程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了spring-security java config:如何配置多个AuthenticationManager实例大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决spring-security java config:如何配置多个AuthenticationManager实例?

开发过程中遇到spring-security java config:如何配置多个AuthenticationManager实例的问题如何解决?下面主要结合日常开发的经验,给出你关于spring-security java config:如何配置多个AuthenticationManager实例的解决方法建议,希望对你解决spring-security java config:如何配置多个AuthenticationManager实例有所启发或帮助;

您可以具有多个http配置元素,每个元素都有自己的AuthenticationManager。它可能看起来像这样

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    private AuthenticationManager authenticationManager1() {
        // defines first AuthenticationManager
        return authenticationManager;
    }

    @Bean
    private AuthenticationManager authenticationManager2() {
        // defines second AuthenticationManager
        return authenticationManager;
    }

    @Configuration
    @Order(1)
    public static class Uri1APIConfigurationAdapter extends WebSecurityConfigurerAdapter {

        @autowired
        @QualifIEr(authenticationManager1)
        private authManager1;

        @OverrIDe
        protected AuthenticationManager authenticationManager() {
            return authManager1;
        }

        protected voID configure(httpSecurity http) throws Exception {
            http
                .antMatcher("/URI1/**")
                ...
        }
    }

    @Configuration
    @Order(2)
    public static class Uri2APIConfigurationAdapter extends WebSecurityConfigurerAdapter {

        @autowired
        @QualifIEr(authenticationManager2)
        private authManager2;

        @OverrIDe
        protected AuthenticationManager authenticationManager() {
            return authManager2;
        }

        protected voID configure(httpSecurity http) throws Exception {
            http
                .antMatcher("/URI2/**")
                ...
        }
    }
}

解决方法

我用:

  • spring boot: 1.1.7
  • spring-security: 4.0.0.M2
  • spring-fmk: 4.1.1.RELEASE

一切都使用Java Config配置(包括spring-security)

我正在使用一个Web服务器项目,在该项目中,身份验证:基本base64Gibberish标头用于验证用户。

问题是,取决于URI的AuthenticationManager不同(因为我需要2个不同的)UserDetailsservice

  • / URI1 / ** => authManager1
  • / URI2 / ** => authManager2

我试过的多个扩展名WebSecurityConfigurerAdapter

@Override
@Bean( name = "authManager1" )
public AuthenticationManager authenticationManagerBean() throws Exception



@Override
@Bean( name = "authManager2" )
public AuthenticationManager authenticationManagerBean() throws Exception

我总是得到:

org.springframework.beans.factory.beanCreationException: Error creaTing bean with name 'springSecurityFilterChain' 
defined in class path resource [org/springframework/security/config/Annotation/web/configuration/WebSecurityConfiguration.class]: Instantiation of bean failed; 
nested exception is org.springframework.beans.factory.beanDefinitionStoreException: 
Factory method [public javax.servlet.Filter org.springframework.security.config.Annotation.web.configuration.WebSecurityConfiguration.springSecurityFilterChain() throws java.lang.Exception] 
threw exception; nested exception is java.lang.IllegalArgumentexception: 
ExpecTing to only find a single bean for type interface org.springframework.security.authentication.AuthenticationManager,but found [authManager1,authManager2]

由于我有多个安全筛选器链,如何“讲” spring-security在不同的安全筛选器链中注入不同的AuthenticationManager?

在此先感谢P。

大佬总结

以上是大佬教程为你收集整理的spring-security java config:如何配置多个AuthenticationManager实例全部内容,希望文章能够帮你解决spring-security java config:如何配置多个AuthenticationManager实例所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。