Search code examples
htmlspring-bootspring-securitythymeleafspring-thymeleaf

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


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.


Solution

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

    Thanks @ChetanAhirrao