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

spring-security java config: How to configure Multiple AuthenticationManager instances

我用:

  • 弹簧靴:1.1.7
  • 弹簧安全性:4.0.0.M2
  • spring-fmk:4.1.1。发布

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

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

问题在于,根据URI,AuthenticationManager是不同的(因为我需要2个不同的UserDetailsService

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

我尝试了WebSecurityConfigurerAdapter的多个扩展

1
2
3
@Override
@Bean( name ="authManager1" )
public AuthenticationManager authenticationManagerBean() throws Exception
1
2
3
@Override
@Bean( name ="authManager2" )
public AuthenticationManager authenticationManagerBean() throws Exception

无济于事

我总是得到:

1
2
3
4
5
6
7
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.


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@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/**")
                ...
        }
    }
}