当使用 spring security 登录失败时,它会抛出异常并在我的 JSP 中显示如下:
<c:if test="${not empty error}">
<div class="errorblock">
Your login attempt was not successful, try again.<br /> Caused :
${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}
</div>
</c:if>
现在我正在从 JSP 更改为 Thymeleaf 并尝试做同样的事情,但是当登录失败时,sessionScope
变量似乎为空。
<div th:if="${error}" class="errorblock" th:text="'testing ' + ${sessionScope}">
</div>
这会打印出 testing null
。当我将 View 解析器改回 JSP 时,它运行良好。我想我做错了什么。有什么建议吗?
最佳答案
在 Thymeleaf 中,您不使用 sessionScope
token 名称访问 session 变量。您改用 session
。所以你需要做类似的事情:
<div class="error"
th:if="${param.login_error}"
th:with="errorMsg=${session["SPRING_SECURITY_LAST_EXCEPTION"].message}">
Your login attempt was not successful, try again.<br />
Reason: <span th:text="${errorMsg}">Wrong input!</span>
</div>
免责声明,根据 StackOverflow 规则:我是 Thymeleaf 的作者。
关于java - 使用 Thymeleaf 时,Spring security sessionscope 似乎为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14042106/