gpt4 book ai didi

Spring 安全 java 配置 : How to configure Multiple AuthenticationManager instances

转载 作者:行者123 更新时间:2023-12-04 03:37:25 24 4
gpt4 key购买 nike

我用:

  • Spring Boot :1.1.7
  • Spring 安全:4.0.0.M2
  • spring-fmk: 4.1.1.RELEASE

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

    我正在开发一个 Web 服务器项目,其中 Authentication: Basic base64Gibberish header 用于对用户进行身份验证。

    问题在于,根据 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 在不同的安全过滤器链中注入(inject)不同的 AuthenticationManager ?

    提前致谢
    P。

    最佳答案

    您可以有多个 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 安全 java 配置 : How to configure Multiple AuthenticationManager instances,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26305942/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com