Spring 使用 Thymeleaf 启动应用程序。使用常量检查 hasAuthority
Spring boot Application with Thymeleaf. Using Constants to check hasAuthority
在我的 class 中定义常量并在 UI 中使用来验证用户是否有权限,向用户显示菜单否则隐藏它。参考下面的代码我实现的方式。
<li sec:authorize="hasAuthority('${T(com.sample.application.security.Privilege).ADMIN}')" class="nav-item" th:classappend="${template} == 'Home' ? 'active':''">
但是,它并没有像我预期的那样工作。我期待 thymeleaf 将 ${T(com.sample.application.security.Privilege).ADMIN} 转换为 ADMIN 并将其验证为 hasAuthority('ADMIN') 但这不起作用。有没有其他方法可以在 thymeleaf 中进行此验证。实现这一目标的最佳方法是什么?
更新:也尝试将常量分配给 thymeleaf 局部变量。也没用。
<ul class="navbar-nav" th:with="admin=${T(com.sample.application.security.Privilege).ADMINISTRATOR}, groupAdmin=${T(com.sample.application.security.Privilege).APPLICATION_GROUP_ADMIN}, basicUser=${T(com.sample.application.security.Privilege).APPLICATION_BASIC_USER}" >
<li sec:authorize="hasAuthority(${basicUser}) OR hasAuthority(${admin})" class="nav-item" th:classappend="${template} == 'home' ? 'active':''">
<a class="nav-link" href="/myApplication/User">Customer Home</a>
</li>......</ul>
尝试了几种方法后。以下解决方案没有任何问题。
<ul class="navbar-nav">
<li sec:authorize="${hasAuthority(T(com.sample.application.security.Privilege).ADMINISTRATOR) OR hasAuthority(T(com.sample.application.security.Privilege).APPLICATION_GROUP_ADMIN)}" class="nav-item" th:classappend="${template} == 'home' ? 'active':''">
<a class="nav-link" href="/myApplication/User">Customer Home</a>
</li>
在我的 class 中定义常量并在 UI 中使用来验证用户是否有权限,向用户显示菜单否则隐藏它。参考下面的代码我实现的方式。
<li sec:authorize="hasAuthority('${T(com.sample.application.security.Privilege).ADMIN}')" class="nav-item" th:classappend="${template} == 'Home' ? 'active':''">
但是,它并没有像我预期的那样工作。我期待 thymeleaf 将 ${T(com.sample.application.security.Privilege).ADMIN} 转换为 ADMIN 并将其验证为 hasAuthority('ADMIN') 但这不起作用。有没有其他方法可以在 thymeleaf 中进行此验证。实现这一目标的最佳方法是什么?
更新:也尝试将常量分配给 thymeleaf 局部变量。也没用。
<ul class="navbar-nav" th:with="admin=${T(com.sample.application.security.Privilege).ADMINISTRATOR}, groupAdmin=${T(com.sample.application.security.Privilege).APPLICATION_GROUP_ADMIN}, basicUser=${T(com.sample.application.security.Privilege).APPLICATION_BASIC_USER}" >
<li sec:authorize="hasAuthority(${basicUser}) OR hasAuthority(${admin})" class="nav-item" th:classappend="${template} == 'home' ? 'active':''">
<a class="nav-link" href="/myApplication/User">Customer Home</a>
</li>......</ul>
尝试了几种方法后。以下解决方案没有任何问题。
<ul class="navbar-nav">
<li sec:authorize="${hasAuthority(T(com.sample.application.security.Privilege).ADMINISTRATOR) OR hasAuthority(T(com.sample.application.security.Privilege).APPLICATION_GROUP_ADMIN)}" class="nav-item" th:classappend="${template} == 'home' ? 'active':''">
<a class="nav-link" href="/myApplication/User">Customer Home</a>
</li>