【问题标题】:Spring boot csrf filterSpring Boot csrf 过滤器
【发布时间】:2015-05-22 21:10:56
【问题描述】:

我试图为某些特定的 api 调用启用 csrf 过滤器,而其他人则不需要 csrf 过滤器。我所做的是

@Override

protected void configure(HttpSecurity http) throws Exception {

    http.csrf().disable().authorizeRequests().antMatchers("/public/**").permitAll();
    http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint);
    http.csrf().disable().addFilterBefore(new StatelessCSRFFilter(), CsrfFilter.class).authorizeRequests().antMatchers("/rest/**").permitAll();
}

问题是当我调用 localhost:8080/public/hello 时

显示错误

"message": "CSRF-token 缺失或不匹配"

我正在使用 Spring Boot 和 Spring Security。

感谢您的帮助。

【问题讨论】:

    标签: java spring spring-mvc spring-security spring-boot


    【解决方案1】:
    http.antMatcher("/public/**").authorizeRequests().anyRequest().permitAll();
    http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint);
    http.antMatcher("/rest/**").addFilterBefore(new StatelessCSRFFilter(), CsrfFilter.class).csrf().disable();
    

    或者你可以这样做。我认为两者都可以工作。

    http.antMatcher("/public/**").csrf().disable();
    http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint);
    http.antMatcher("/rest/**").addFilterBefore(new StatelessCSRFFilter(), CsrfFilter.class).csrf().disable();
    

    【讨论】:

      【解决方案2】:

      尝试将http.csrf().disable() 更改为http.antMatcher("/public/**").csrf().disable()http.antMatcher("/rest/**").csrf().disable()。您可能必须将这两行分别放在自己的WebSecurityConfigurerAdapter 中。

      这将告诉 Spring-Security 创建多个 HTTP 安全过滤器链(类似于在 XML 模型中有多个 <http/> 块)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-17
        • 2017-06-09
        • 2021-08-28
        • 1970-01-01
        • 2021-11-04
        • 2020-10-30
        • 1970-01-01
        • 2016-08-16
        相关资源
        最近更新 更多