【问题标题】:${_csrf.parameterName} and ${_csrf.token} return null${_csrf.parameterName} 和 ${_csrf.token} 返回 null
【发布时间】:2015-09-08 02:36:03
【问题描述】:

对我来说,没有生成 CSRF 令牌....我搜索了很多链接仍然没有找到解决方案

收到此错误

在请求参数“_csrf”上发现无效的 CSRF 令牌“null” 或标题“X-CSRF-TOKEN”。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <c:url value="/j_spring_security_check" var="postUrl"/>
    <form action="${postUrl}" method="post" >
        <c:if test="${param.error != null}">     
        <p>Invalid username and password.</p>
        </c:if>
        <c:if test="${param.logout != null}">  
        <p>You have been logged out.</p>
        </c:if>
        <p>
            <label for="username">Username</label>
             <input type="text"
                id="username" name="username" /> 
        </p>
        <p>
            <label for="password">Password</label> <input type="password"
                id="password" name="password" /> 
        </p>


        <input type="text" 
        name="${_csrf.parameterName}"
            value="${_csrf.token}" />

        <button type="submit" class="btn">Log in</button>
    </form>
</body>
</html>

在 pom.xml 中

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-core</artifactId>
    <version>4.0.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
    <version>4.0.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-web</artifactId>
    <version>4.0.1.RELEASE</version>
</dependency>

【问题讨论】:

  • 你有configured it吗?
  • 如果您使用 spring-form &lt;form:form&gt;,它将为您添加 csrf 令牌(假设您已正确设置)
  • 这里有同样的问题。这是一个 csrf 令牌,他应该在哪里给出路径,因为你建议
    tag.@NeilMcGuigan

标签: spring maven spring-security


【解决方案1】:

对我来说,我遇到了同样的错误,问题是我在 web.xml 中缺少以下配置,

<!-- Spring Security Filter -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

【讨论】:

  • @Downvoter 我不知道为什么有人会否决对我有用的解决方案的正当理由,尽管这可能不是 OP 提出的问题的确切解决方案.没关系,但请说明您投票反对的原因,哈哈。
  • 因为它不能仅仅使用它。必须在安全配置 xml 中添加 配置和其他内容。我同意,但应该添加评论。我有一张纸条要回到这个问题上。我会发布 xml,但我现在手头没有。
【解决方案2】:

在我的情况下,只需在 web.xml 中启用 CSRF 标签,它就可以正常工作。

【讨论】:

    猜你喜欢
    • 2012-01-26
    • 2021-04-11
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多