【发布时间】:2020-03-25 01:34:57
【问题描述】:
我在使用 csrf 时遇到问题,即使它在 spring 配置中被禁用。 我的日志输出如下:
Invalid CSRF token found for http://localhost:8080/exercise/
我有这个弹簧配置
protected void configure(HttpSecurity http)throws Exception {
http.csrf().disable();
http
.httpBasic()
.and()
.authorizeRequests()
.antMatchers(HttpMethod.POST, "/user/add").hasRole("ADMIN")
.antMatchers(HttpMethod.POST).hasAnyRole("ADMIN", "TRAINER")
.antMatchers(HttpMethod.DELETE).hasAnyRole("ADMIN", "TRAINER")
.antMatchers(HttpMethod.GET).permitAll()
.antMatchers(HttpMethod.PUT).permitAll()
.antMatchers(HttpMethod.HEAD).permitAll()
.antMatchers("/register/").authenticated()
.and()
.exceptionHandling()
.and()
.formLogin()
.permitAll()
.and()
.logout()
.permitAll();
}
好像我获得了身份验证和授权,因为日志输出:
Obtained a valid SecurityContext from SPRING_SECURITY_CONTEXT: 'org.springframework.security.core.context.SecurityContextImpl@1ddb0b4b: Authentication: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@1ddb0b4b: Principal: com.brevisfit.api.model.user.User[ iduser=1 ]; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null; Granted Authorities: ROLE_ADMIN, ROLE_TRAINER'
我对 POST\DELETE\PUT 请求有疑问,因为它们会抛出 403 响应,我知道它来自 CsrfFilter.class
if (!csrfToken.getToken().equals(actualToken)) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Invalid CSRF token found for "
+ UrlUtils.buildFullRequestUrl(request));
}
对于请求,我使用的是邮递员。
【问题讨论】:
-
你把这个
configure方法放在哪里了?是否在扩展WebSecurityConfigurerAdapter的配置类中? -
您找到了吗,出了什么问题?或者你是怎么解决的?
标签: java spring spring-boot csrf