首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Spring安全忽略url不适用于we安全忽略方法。

Spring安全忽略url不适用于we安全忽略方法。
EN

Stack Overflow用户
提问于 2020-12-24 11:41:00
回答 3查看 6.1K关注 0票数 3

我们面临着SpringSecurity忽略一个方法的问题。我们试图跳过一些urls (acutator/health)和资源的身份验证。身份验证是在外部进行的,我们有一个自定义过滤器来提取授权原则。

我们覆盖配置的方法,如下所示:

代码语言:javascript
运行
复制
public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/resources/**", "/actuator/health");
}
protected void configure(HttpSecurity http) throws Exception {
         http.addFilter(cutstomFilter).authorizeRequests()
        .antMatchers("/add","/update","/upload").hasAuthority("ADMIN").anyRequest().authenticated()
        .and().logout().logoutSuccessUrl("/logoutUser").and()
        .exceptionHandling().accessDeniedPage("/accessDenied").and().csrf().disable();
    }

在给定的实现中,我们的customFilter被要求提供资源和健康url。这是由于原则的改变而导致的重新认证。

我们尝试添加这段代码,但是customFilter也会被调用为health。

代码语言:javascript
运行
复制
http.authorizeRequests().antMatchers("/actuator/health").permitAll() 

注意:检查了@Rob的答案,但不明白如果我们将这些url放在忽略列表中,为什么需要一个自定义的文件处理程序。https://stackoverflow.com/a/19985323/2138633

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-12-24 12:13:19

更新:请参阅@dur中的评论,它可能会解决问题,而不会有重大变化。

代码语言:javascript
运行
复制
To make it clear, your first security configuration is correct. Your problem 
is that your filter is used as a servlet filter not only as a security chain 
filter. Spring Boot does this autmatically, if you expose your filter.

https://stackoverflow.com/a/39314867/14072498

OP提到的是执行器的端点。让我们看看doc:https://spring.io/guides/topicals/spring-security-architecture

医生说:

代码语言:javascript
运行
复制
If you want your application security rules to apply to the actuator 
endpoints, you can add a filter chain that is ordered earlier than the 
actuator one and that has a request matcher that includes all actuator 
endpoints.

Doc建议将配置划分为多个WebSecurityConfigurerAdapter实现。

在下面的示例配置中,您应该将您所指的自定义筛选器应用于MainAppConfigurerAdapter

“多弹簧启动安全配置”示例:https://medium.com/@igor.bonny/multiple-spring-boot-security-configuration-c876f1b6061e

为了跳过其他端点的身份验证,请添加

代码语言:javascript
运行
复制
.and()
.authorizeRequests().anyRequest().permitAll();

以结束应用程序链,如下所示。

若要验证安全设置,请为所有端点添加集成测试。

代码语言:javascript
运行
复制
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration {

  @Configuration
  @Order(ManagementServerProperties.BASIC_AUTH_ORDER - 1)
  public class ActuatorConfigurerAdapter extends WebSecurityConfigurerAdapter {
    @Override
      protected void configure(HttpSecurity http) {
          http.antMatcher("/actuator/**")
          ...
      }
  }

  @Configuration
  @Order(SecurityProperties.DEFAULT_FILTER_ORDER)
  public class MainAppConfigurerAdapter extends WebSecurityConfigurerAdapter {
      @Override
      protected void configure(HttpSecurity http) {
          http.antMatcher("/api/**")
          ...
      }
  }
}
票数 5
EN

Stack Overflow用户

发布于 2020-12-24 13:54:34

遵循https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-endpoints-security,您应该:

  • 添加到属性文件中

management.endpoints.web.exposure.include=*

  • 更新您的安全配置如下

@Configuration(proxyBeanMethods = false)公共类ActuatorSecurity扩展WebSecurityConfigurerAdapter {@覆盖受保护的无效配置(HttpSecurity http)抛出异常{HttpSecurity -> requests.anyRequest().permitAll();}

票数 0
EN

Stack Overflow用户

发布于 2020-12-24 14:14:27

一种方法是在安全配置和自定义筛选器中使用模式,从身份验证中提取主体。你可以这样做:

  1. 声明忽略模式:

静态String[] IGNORE_PATTERNS =新的String[] {“**/*..js”,“**/*..css”,String[]

  1. 声明允许所有模式:

静态String[] PERMIT_ALL_PATTERNS =新String[] { "/login“、"/logout”、"/health"};

  1. WebSecurity中使用被忽略的模式:

web.ignoring().antMatchers(IGNORE_PATTERNS);

  1. 使用许可证-HttpSecurity中的所有模式:

http.authorizeRequests().antMatchers(PERMIT_ALL_PATTERNS).permitAll().anyRequest().authenticated().and() .

  1. 在过滤器中声明RequestMatcher

列表匹配器=新的ArrayList<>();for (字符串模式: IGNORE_PATTERNS) {matchers.add(新的AntPathRequestMatcher(模式));} for (字符串模式: PERMIT_ALL_PATTERNS) {matchers.add(新的模式);} RequestMatcher ignoreRequestMatcher =新的OrRequestMatcher(匹配器);

  1. 在过滤器的doFilter方法中使用请求匹配器:

如果(ignoreRequestMatcher.matches((HttpServletRequest)请求){ /*跳过此过滤器*/ chain.doFilter(请求,响应);返回;}在*/下面的过滤器代码的/*其余部分

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65437818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档