带有thymeleaf的spring引导应用程序使用常量检查权限

w51jfk4q  于 2021-09-30  发布在  Java
关注(0)|答案(1)|浏览(269)

在我的类中定义常量,并在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>
cgh8pdjw

cgh8pdjw1#

在尝试了几种方法之后。下面的解决方案没有任何问题。

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

相关问题