3

私はSpring Securityを数日間実装しようとしており、csrf tokens.

デバッグした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: Exception Evaluation SpringEL expression: "_csrf.parameterName" (loginsample:19)] で根本原因 org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Property or field 'parameterName' が null で見つかりません

これはなぜでしょうか?

4

1 に答える 1

1

Java または XML 構成を使用していますか? XML の場合は、csrf トークンを有効にしてください。

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

JSP taglib をインポートする必要がある場合もあります。セキュリティ タグをインポートします。

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

次に、これも頭に追加します。

<sec:csrfMetaTags /> 

taglib 依存関係をインポートする必要がある場合があります。

于 2015-06-16T02:29:52.480 に答える