加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Spring启动csrf过滤器

发布时间:2020-12-15 04:26:17 所属栏目:Java 来源:网络整理
导读:我试图为某些特定的api调用启用csrf过滤器,而其他人则不需要csrf过滤器.我所做的是 @Overrideprotected void configure(HttpSecurity http) throws Exception { http.csrf().disable().authorizeRequests().antMatchers("/public/**").permitAll(); http.exc
我试图为某些特定的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令牌”

我正在使用Spring启动和Spring Security.

谢谢你的帮助.

解决方法

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();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读