[英]Spring boot csrf filter
我试图为某些特定的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();
尝试将http.csrf().disable()
更改为http.antMatcher("/public/**").csrf().disable()
和http.antMatcher("/rest/**").csrf().disable()
。 您可能必须将这两行放在他们自己的WebSecurityConfigurerAdapter
。
这将告诉Spring-Security创建多个HTTP安全过滤器链(类似于在XML模型中具有多个<http/>
块)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.