Thymeleaf: check if the user is authenticated with Spring Security
In Thymeleaf, using the Spring Security integration module, the following expression returns true
if the current user (i.e. the user viewing the page) is authenticated, false
otherwise:
${#authorization.expression('isAuthenticated()')}
Usage example
Change a text:
<span th:text="${#authorization.expression('isAuthenticated()')} ?
'the user is logged in' :
'the user is NOT logged in'">
</span>
Add a class on html element:
<span th:classappend="${#authorization.expression('isAuthenticated()')} ? 'active'"
</span>
References
https://github.com/thymeleaf/thymeleaf-extras-springsecurity
http://www.thymeleaf.org/doc/articles/springsecurity.html
-
rohtakdev
-
Andrea
-
rohtakdev
-
-
-
dima
-
Andrea
-
-
Soumyajyoti Das