【问题标题】:HTTP Status 403 - Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN'HTTP 状态 403 - 在请求参数“_csrf”或标头“X-CSRF-TOKEN”上发现无效的 CSRF 令牌“null”
【发布时间】:2015-09-03 04:33:15
【问题描述】:

我收到了错误 HTTP 状态 403 - 在请求参数“_csrf”或标头“X-CSRF-TOKEN”上发现无效的 CSRF 令牌“null” 当我点击登录按钮时。 HTML 文件如下所示。任何帮助将不胜感激。

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Login page</title>
<style>
.error {
    color: red;
}
</style>
</head>
<body>
<h1>Login page</h1>

<p>
<c:if test="${error == true}">
    <b class="error">Invalid login or password.</b>
</c:if>
</p>

<form method="post" action="<c:url value='j_spring_security_check'/>" >
<table>
<tbody>
<tr>
<td>Login:</td>
<td><input type="text" name="j_username" id="j_username"size="30" maxlength="40"  /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="j_password" id="j_password" size="30" maxlength="32" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Login" /></td>
</tr>
</tbody>
</table>
</form> 

<p>
<a href="${pageContext.request.contextPath}/index.html">Home page</a><br/>
</p>
</body>
</html>

【问题讨论】:

    标签: token csrf status


    【解决方案1】:

    您正在使用 spring security 并发布用户名和密码表单,但在发布表单时忘记包含 _csrf 令牌。 对于 Spring 安全文档:

    13.4.3 包含 CSRF 令牌

    表单提交

    最后一步是确保您将 CSRF 令牌包含在所有 PATCH、POST、PUT 和 DELETE 方法。这可以使用 _csrf request 属性获取当前的 CsrfToken。做的一个例子 这与 JSP 如下所示:

    <c:url var="logoutUrl" value="/logout"/> 
    <form action="${logoutUrl}"method="post">
        <input type="submit" value="Log out" />
        <input type="hidden" name="${_csrf.parameterName}"value="${_csrf.token}"/>
    </form> 
    

    查看完整文档here

    【讨论】:

      猜你喜欢
      • 2014-02-03
      • 2015-11-16
      • 2016-09-11
      • 2018-03-16
      • 2015-11-05
      • 2019-02-03
      • 2017-07-15
      • 2021-03-18
      • 2016-03-29
      相关资源
      最近更新 更多