多个 WebSecurityConfigurer在弹簧引导中为多种模式提供适配器
2022-09-01 14:39:46
我正在尝试为我的项目设置多个WebsecurityConfigurerAdapter,其中弹簧启动执行器API使用基本身份验证进行保护,所有其他端点都使用JWtAuthentication进行身份验证。我只是无法让它一起工作,只有具有较低顺序的配置才能工作。我正在使用Spring Boot 2.1.5.RELEASE
带有 JWT 身份验证器的安全配置 One
@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 - 多个 WebSecurityConfigurerAdapter
[在 Spring-boot 中具有多个 WebSecurityConfigurerAdapter 的问题
[https://github.com/spring 项目/弹簧安全/问题/5593][1]
[https://www.baeldung.com/spring 安全多入口点][1]
似乎没有任何效果,这是春季的已知问题吗?