java - 使用 Thymeleaf 时,Spring security sessionscope 似乎为空

标签 java spring jsp spring-security thymeleaf

当使用 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/

相关文章:

java - 验证受密码保护的 ZIP 文件

java 编译与运行时计算

java - 使用 RMI 通过网络发送非常大的对象

java - 以特定格式获取当前日期

java - Spring JDBC。插入具有一对多关系的记录

java - 数据库全文检索

java - 如何在java jsf中点击按钮显示数据库中的数据

jsp - Gradle插件Jasper jspc可以预编译jsps

java - 带有 Hibernate 的 Spring - 线程 "main"java.lang.NullPointerException 中的异常

java - :iterator with collection of Object array