【问题标题】:Thymeleaf sec:authorize can you combine authorization for role OR different property?Thymeleaf sec:authorize 你可以结合角色或不同属性的授权吗?
【发布时间】:2019-03-30 09:46:44
【问题描述】:

我正在使用 Thymeleaf、Spring-boot 和 Java。我有导航栏的 li 项目,并且想为其中一个 li 项目设置访问权限,以便如果 loggedUser.client.policyType == 特定访问权限,或者他们具有管理员角色,则链接显示以便他们可以访问页。 我试过 th:authorize, sec:authorize, th:if.... 都不行。知道如何让它工作吗?

<li th:authorize="${loggedUser.client.policyType == 'Access – NO AMS'} or hasRole('ROLE_ADMIN')" th:classappend="${currentPage == 'directBind'} ? 'active'"><a th:href="@{/directBind}"><img class="left"  height="60%" width="auto" src="/images/mail1.png"/> Direct Bind</a></li>

【问题讨论】:

    标签: java spring-boot authorization thymeleaf


    【解决方案1】:

    您是否尝试过以下方法?

    <li th:if="${#authorization.expression('hasRole(''ROLE_ADMIN'') or ${loggedUser.client.policyType == 'Access – NO AMS'}')}">
       <a th:href="@{/directBind}"><img class="left"  height="60%" width="auto" src="/images/mail1.png"/> Direct Bind</a>
    </li >
    

    您需要在.pom 上添加以下依赖项才能使用#authorization。可以使用以下代码添加依赖。

    <dependency>
       <groupId>org.thymeleaf.extras</groupId>
       <artifactId>thymeleaf-extras-springsecurity4</artifactId>
    </dependency>
    

    【讨论】:

    • 嗨 Alain,如果登录用户的策略类型是 Access-No AMS 并且用户是管理员,则此代码可以显示链接。我需要它是或。
    • 哦抱歉误会了。我知道的唯一方法是使用 Thymeleaf Spring Security Extras。不过,您需要将其添加到您的 Maven 中。您可以在我编辑的答案中看到如何。
    猜你喜欢
    • 2021-10-20
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    相关资源
    最近更新 更多