The*_*ool 0 spring-mvc spring-security thymeleaf spring-boot bootstrap-4
我正在开发一个基于 Spring boot 的 Web 应用程序,主页包含一个带有登录和注册链接的 boostrap 导航栏。
用户登录后(通过单击主页中的登录链接),他们将再次重定向到主页(除非他们在 Spring Security 启动 A&A 之前访问任何其他链接)。在重定向时,我想隐藏导航栏中的登录和注册链接,并在其位置显示注销链接。因为我想在应用程序的所有页面中插入导航栏。
我想知道当要求重定向到同一页面时执行此操作的最佳/标准方法是什么。
我能想到的一种解决方案是从 spring security 检查并获取 user-principal 对象,将其传递给 thymeleaf 模板,并检查 thymeleaf 模板的请求属性中是否存在 userprincipal 对象,这意味着用户已登录然后我可以隐藏(不渲染)登录和注册链接以及显示(渲染)注销链接。如果没有,则显示登录和注册链接并隐藏注销链接。我想知道,这是正确的方法吗?这对我来说就像是一种黑客攻击,因此想知道是否有任何标准方法可以做到这一点。
您可以使用 Thymeleaf Spring Security 扩展:
\n\n <dependency>\n <groupId>org.thymeleaf.extras</groupId>\n <artifactId>thymeleaf-extras-springsecurity5</artifactId>\n </dependency>\nRun Code Online (Sandbox Code Playgroud)\n\n添加命名空间
\n\n<html xmlns:th="http://www.thymeleaf.org" \n xmlns:sec="http://www.thymeleaf.org/extras/spring-security">\nRun Code Online (Sandbox Code Playgroud)\n\n然后你就可以使用它:
\n\n <li sec:authorize="!isAuthenticated()" class="nav-item">\n <a class="btn btn-outline-light" th:href="@{/login}">Login</a>\n </li>\n <li sec:authorize="isAuthenticated()" class="nav-item dropdown">\n <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown"\n aria-haspopup="true" aria-expanded="false" sec:authentication="name">\n </a>\n <div class="dropdown-menu" aria-labelledby="navbarDropdown">\n <a class="pl-3" th:href="@{/password}">Passwort \xc3\xa4ndern</a>\n <div class="dropdown-divider"></div>\n <a class="pl-3" th:href="@{/logout}">Logout</a>\n </div>\n </li>\nRun Code Online (Sandbox Code Playgroud)\n\n请在此处找到完整文档:
\n\nhttps://github.com/thymeleaf/thymeleaf-extras-springsecurity
\n| 归档时间: |
|
| 查看次数: |
1718 次 |
| 最近记录: |