Home > Software engineering >  Using Enum in sec:authorize="hasRole(...)" (Spring Boot and Thymeleaf)
Using Enum in sec:authorize="hasRole(...)" (Spring Boot and Thymeleaf)

Time:10-09

I try to replace

<div sec:authorize="hasRole('ADMIN')"></div>

by

<div sec:authorize="hasRole(${T(com.mypackage.Role).ADMIN.getText()})"></div>

but it does not work. Then I tried

<div th:with="role=${T(com.mypackage.Role).ADMIN.getText()}" sec:authorize="hasRole(${role})"></div>

and with preprocessing

<div th:with="role=${T(com.mypackage.Role).ADMIN.getText()}" sec:authorize="hasRole(__${role}__)"></div>

but is still not working.

CodePudding user response:

Something like this should work:

<div th:if="${#authorization.expression('hasRole('''   T(com.mypackage.Role).ADMIN.getText()   ''')')}">
  
</div>

(I don't think the sec: attributess interpret Thymeleaf expressions same as other tags... at least I couldn't find any examples of it.)

CodePudding user response:

<div sec:authorize="hasRole(T(com.mypackage.Role).ADMIN)"></div>

Thanks @ChetanAhirrao

  • Related