Java – when using thymeleaf, the spring security sessionscope appears to be null

When the login fails with spring security, it will throw an exception and display it in my JSP, as shown below:

<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>

Now I'm changing from JSP to thymeleaf and trying to do the same, but when the login fails, the sessionscope variable appears to be null

<div th:if="${error}" class="errorblock" th:text="'testing ' + ${sessionScope}">

</div>

This prints out the test null It works well when I change the view parser back to JSP I think I did something wrong Any suggestions?

Solution

In thymeleaf, you do not use the sessionscope token name to access session variables You can use conversation instead So you need to do something similar:

<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>

Disclaimer, according to stackoverflow rule: I am the author of thymeleaf

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>