在 Thymeleaf 中创建基于 SpringSecurity 用户名的 URL [英] Create a URL based on SpringSecurity username in Thymeleaf
问题描述
使用 Thymeleaf SpringSecurityDialect 我可以像这样显示登录用户的用户名:
Using the Thymeleaf SpringSecurityDialect I am able to display the username of the logged in user like this:
Logged in user<span sec:authentication="name">Username</span>
我想更进一步,使用该登录用户名生成 URL.
I'd like to go one step further and use that loggedin username to generate a URL.
<a href="view.html" th:href="@{'/view/'+${user.username}}" />
到目前为止,我一直通过将其直接添加到模型中来做到这一点,但是我想知道是否可以仅使用 SecurityDialect 和标签直接做到这一点.
I have been doing this so far by adding this directly to the model, however I was wondering if it was possible to do this directly just using the SecurityDialect and tags.
例如在 JSP 中,将它添加到 pagecontext var 是微不足道的.
For instance in JSP it would be trivial to add this to a pagecontext var.
<sec:authentication var="user" property="principal" />
但是,我想知道是否可以选择在 thymeleaf 中执行此类操作.
However, I I'd like to know if there's an option for me to do something like this in thymeleaf.
推荐答案
您是否尝试过使用 ${#authentication.name} 代替 ${user.username}?它应该工作...
Have you tried using ${#authentication.name} in stead of ${user.username}? It should work...
<a href="view.html" th:href="@{'/view/'+${#authentication.name}}" >TEST</a>
这篇关于在 Thymeleaf 中创建基于 SpringSecurity 用户名的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!