【问题标题】:Multiple WebSecurityConfigurerAdapter in spring boot for multiple patternsSpring Boot中的多个WebSecurityConfigurerAdapter用于多种模式
【发布时间】: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 中的一个已知问题吗?

【问题讨论】:

标签: java spring spring-boot spring-security


【解决方案1】:

要使用多个WebsecurityConfigurerAdapter,您需要使用RequestMatcher 将它们限制为特定的URL 模式。

在您的情况下,您可以为ActuatorSecurityConfig 设置更高的优先级并将其限制为仅执行器端点:

@Order(-1)
@Configuration
public class ActuatorSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .requestMatchers().antMatchers("/actuator/**")
                .and()
                .authorizeRequests().anyRequest().hasRole("ADMIN")
                .and()
                .httpBasic();
    }
}

【讨论】:

  • 感谢@Anar Sultanov 解决了这个问题。通过 POSTMAN 进行测试时,我还必须禁用 SESSIONID。如果启用了 SESSIONID,则如果一个请求成功通过身份验证,则所有后续请求都会成功,无论是否传递了错误的凭据
  • 救命稻草,我想请你喝杯啤酒。
  • 我使用了这个解决方案,但我不得不直接在HttpSecurity 对象上使用http.antMatcher("foo")。这将创建一个额外的FilterChainProxy,这意味着要应用一组新的过滤器,它只匹配某些请求。
  • @DanielF 感谢您的评论,但我不认为您“必须使用”它。虽然antMatcher(String) 还允许将HttpSecurity 配置为仅在匹配提供的ant 模式时调用,但requestMatchers() 更适合更高级的配置。
猜你喜欢
  • 2014-11-24
  • 2017-04-06
  • 2019-03-07
  • 2021-04-21
  • 2020-06-09
  • 1970-01-01
  • 2018-04-19
  • 2022-11-27
相关资源
最近更新 更多