HTTP Status 403 - An invalid CSRF token "null" was found in the request parameter "_csrf" or the header "X-CSRF-TOKEN"

I receive an error message HTTP Status 403 - An invalid CSRF token "null" was found in the request parameter "_csrf" or the header "X-CSRF-TOKEN" when I press the login button. The HTML file is as follows. Any help would be appreciated.

 
<%@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>
+4
source share
1 answer

You use spring security and publish the username and password form, but when placing the form you forgot to enable the token _csrf. For spring security documentation:

13.4.3 Enable CSRF Token

Form submissions

- , CSRF PATCH, POST, PUT DELETE. _csrf 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> 

+5

All Articles