【发布时间】:2020-03-22 07:47:27
【问题描述】:
我正在尝试为我的项目设置多个 WebsecurityConfigurerAdapter,其中 Spring Boot 执行器 API 使用基本身份验证进行保护,所有其他端点使用 JWtAuthentication 进行身份验证。我只是不能让它一起工作,只有低阶的配置才能工作。我正在使用 Spring Boot 2.1.5.RELEASE
使用 JWT Authenticator 进行安全配置一
@Order(1)
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private static final String[] AUTH_WHITELIST = {
"/docs/**",
"/csrf/**",
"/webjars/**",
"/**swagger**/**",
"/swagger-resources",
"/swagger-resources/**",
"/v2/api-docs"
};
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers(AUTH_WHITELIST).permitAll()
.antMatchers("/abc/**", "/abc/pdf/**").hasAuthority("ABC")
.antMatchers("/ddd/**").hasAuthority("DDD")
.and()
.csrf().disable()
.oauth2ResourceServer().jwt().jwtAuthenticationConverter(new GrantedAuthoritiesExtractor());
}
}
带有用户名/密码的基本身份验证配置
@Order(2)
@Configuration
public class ActuatorSecurityConfig extends WebSecurityConfigurerAdapter {
/* @Bean
public UserDetailsService userDetailsService(final PasswordEncoder encoder) {
final InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
manager.createUser(
User
.withUsername("user1")
.password(encoder.encode("password"))
.roles("ADMIN")
.build()
);
return manager;
}
@Bean PasswordEncoder encoder(){
return new BCryptPasswordEncoder();
}*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/actuator/**").hasRole("ADMIN")
.and()
.httpBasic();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user1").password("password").authorities("ADMIN");
}
}
我一直试图让它工作很多天,但不能让它们一起工作。如果我交换订单,则只有基本身份验证有效,JWT 身份验证管理器无效。
我经历了很多 SOF 问题,比如
[Spring boot security - multiple WebSecurityConfigurerAdapter
[Issue with having multiple WebSecurityConfigurerAdapter in spring-boot
[https://github.com/spring-projects/spring-security/issues/5593][1]
[https://www.baeldung.com/spring-security-multiple-entry-points][1]
似乎没有任何效果,这是 Spring 中的一个已知问题吗?
【问题讨论】:
-
您需要使用 AuthenticationManagerBuilder 注入相同的身份验证管理器。见stackoverflow.com/questions/40258583/…
标签: java spring spring-boot spring-security