嗨,我是 Spring security 的新手,我正在使用提到的示例 here 作者:Mkyong,我正在使用本教程中提供的基于注释的示例,但每当我在登录表单中输入值并单击提交按钮时,它总是会出现异常,如下所示。
type Status report
message Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN'.
description Access to the specified resource has been forbidden.
我尝试用谷歌搜索,但没有找到任何合适的答案来解决这个问题。安全配置类如下。
SecurityConfig.java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
@Qualifier("authenticationProvider")
AuthenticationProvider authenticationProvider;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/admin/**")
.access("hasRole('ROLE_USER')").and().formLogin()
.loginPage("/login").failureUrl("/login?error")
.usernameParameter("username")
.passwordParameter("password")
.and().logout().logoutSuccessUrl("/login?logout").and().csrf();
}
}
和
login.jsp 表单如下
<form name='loginForm'
action="<c:url value='/login'/>" method='POST' enctype="multipart/form-data">
<table>
<tr>
<td>User:</td>
<td><input type='text' name='username'></td>
</tr>
<tr>
<td>Password:</td>
<td><input type='password' name='password' /></td>
</tr>
<tr>
<td colspan='2'><input name="submit" type="submit"
value="submit" /></td>
</tr>
</table>
<input type="hidden" name="${_csrf.parameterName}"
value="${_csrf.token}" />
</form>
请帮助我解决这个问题。
最佳答案
请将以下代码添加到您的安全配置中,看看是否有任何差异
.and()
.csrf().disable();
关于java - 错误 HTTP 状态 403 - 在请求参数 'null' 或 header '_csrf' 上发现无效的 CSRF token 'X-CSRF-TOKEN',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36786757/