作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我用:
AuthenticationManager
是不同的(因为我需要 2 个不同的
UserDetailsService
。
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]
最佳答案
您可以有多个 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/
我是一名优秀的程序员,十分优秀!