如何在thymelaf中组合sec:authorize和th:if



如何在thymelaf中组合sec:authorize和th:if?

<div class="form-group" sec:authorize="hasRole('ADMIN')" th:if="${myObjct.name!=null}"></div>

基本上,我需要将sec:authorize="hasRole('ADMIN')"和th:if="${myObjct.name!=null}"组合起来,而上面的代码无法工作,因为我希望如何在thymelaf中组合这两个条件?

只需尝试:

<div class="form-group" th:if="${#authorization.expression('hasRole(''ADMIN'')')}"></div>

src:https://github.com/thymeleaf/thymeleaf-extras-springsecurity

我能够用下面的代码实现这一点。

 <sec:authorize access="hasRole('ADMIN')" var="isAdmin"></sec:authorize>
 <div class="form-group" th:if="${'ADMIN'.equals(isAdmin) and myObjct.name!=null}"></div>

此代码可以在这里工作:

<div class="form-group" sec:authorize="isAuthenticated()" th:if="${myObjct.name!=null} and ${#authorization.expression('hasRole(''ADMIN'')')}">

最新更新