2

我一直在試圖實現Spring Security幾天,並一直在努力與csrf tokensSpring Security生成空的CSRF令牌 - 無法找到屬性或字段'parameterName'null

我已調試到CsrfRequestDataValueProcessor,發現下面一行返回null。

CsrfToken token = (CsrfToken) request.getAttribute(CsrfToken.class.getName()); 

使用Thymeleaf我有以下形式:

<form th:action="@{/j_spring_security_check}" method="post"> 
    <label for="j_username">Username</label>: 
    <input type="text" id="j_username" name="j_username" /> <br /> 
    <label for="j_password">Password</label>: 
    <input type="password" id="j_password" name="j_password" /> <br /> 
    <input type="submit" value="Log in" /> 
    <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" /> 
</form> 

它不會呈現,並拋出以下異常:

請求處理失敗;嵌套異常是org.thymeleaf.exceptions.TemplateProcessingException:評估具有根本原因的SpringEL表達式:「_csrf.parameterName」(loginsample:19)]的異常org.springframework.expression.spel.SpelEvaluationException:EL1007E:(pos 0):屬性或字段'parameterName'無法找到null

爲什麼這可能是?

+0

你使用彈簧安全4? – smoggers

+0

你如何生成csrf標記? – kTT

回答

0

您是使用Java還是XML配置?如果XML,請確保您啓用了CSRF令牌:

<http> ... <csrf /> </http>

您可能還需要導入JSP標籤庫: 導入安全標籤:

<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> 

然後在頭部添加此以及:

<sec:csrfMetaTags /> 

您可能需要導入taglib依賴項。

相關問題