当前位置: 首页 > 知识库问答 >
问题:

用于多种模式的spring boot中的多个WebSecurity配置适配器

松旻
2023-03-14

我正在尝试为我的项目设置多个WebsecurityConfigrerAdapter,其中Spring启动执行器API使用基本身份验证进行保护,所有其他endpoint使用JWtAuthentication进行身份验证。我只是无法使它一起工作,只有较低顺序的配置可以工作。我使用Spring Boot 2.1.5。释放

带有JWT身份验证程序的安全配置1

@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身份验证管理器。

我问过很多问题,比如

[Spring Boot安全-多个WebSecurityConfigrerAdapter

[Spring启动中有多个WebSecurityConfigrerAdapter的问题

[https://github.com/spring-projects/spring-security/issues/5593][1]

[https://www.baeldung.com/spring-security-multiple-entry-points][1]

似乎什么都不起作用,这是Spring的已知问题吗?

共有1个答案

邹毅
2023-03-14

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

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

@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();
    }
}
 类似资料:
  • 我目前想实现这样的东西: 但是在中,我们将重点关注authProvider()方法和configure() 随着最近的消息,已被弃用。经过研究,我发现了一件事: 所以我也不得不这么做 这解决了另一个问题。但现在,我发现了这个错误。 这也是我的用户服务 你们介意帮我解决这个问题!谢谢:) 我还想提一下,我还没有在网上找到答案

  • 我正在尝试在我的应用程序中设置Spring Security,它有3个组件: REST API(在v1路径下) 我想这样设置安全性: 用JWT令牌保护的REST API 使用HTTP basic保护管理员 无担保文件(公共资源) 我尝试在的单独实现中为这3个部分配置身份验证,结果如下所示: 对于REST API: 对于Spring管理员: 对于公共文件: 我的主要应用程序类如下所示: 当我运行应用

  • 我在SpringBoot应用程序中实现了多语言,在完成以下配置后,我得到的属性文件无法加载 文件夹名称 src/main/resources/i18n/messages 属性文件名 messages_us.properties 在应用程序主类 内部控制器 标题 接受-语言:我们 我明白了 [36mo.s.c.s.ResourceBundleMessageSource[0;39m[2m:[0;39m

  • 我的代码中有多个WebSecurity配置适配器: 当我从ajax请求访问我的站点时,我得到:从源http://localhost:8082访问http://localhost:8080/user/authenticate的XMLHttpRequest已被CORS策略阻止:对飞行前请求的响应未通过权限改造检查:请求的资源上不存在访问控制允许源标头。 我想我的WebSecurity配置适配器中有一些

  • 我使用的Spring安全与oAuth2,但我有一个问题,我没有找到任何答案,在许多项目的例子,你有2次配置(HttpSecurity超文本传输协议)。 例如在https://github.com/spring-projects/spring-security-oauth/blob/master/samples/oauth2/sparklr/src/main/java/org/springframe

  • 我正在使用依赖项从我的Spring boot微服务中读取配置图,并且它的工作正常。 修改配置映射后,我正在使用刷新endpoint 它按预期工作,应用程序加载configmap更改。 问题上面的工作正常,如果我只有我的应用程序的1个pod,但当我使用更多的1个pod只有1个pod选择更改不是全部。 在下面的示例中,只有我pod选择更改 minkube部署 配置映射。yml公司 bootstrap.