How can I set authority variables using Java Spring Security Expressions in Thymeleaf?

63 Views Asked by At

I'm working with Thymeleaf templates and Java Spring Security. I want to dynamically set the authority used in the hasAuthority('Admin') expression within my Thymeleaf templates. Instead of a hardcoded value, I'd like to use a variable to make the authority dynamic.

The following working fragment thymeleaf Code snippets

<li   th:fragment="nav-link(fieldName,label)" class="nav-item active" 
      sec:authorize="hasAuthority( 'Admin')">
   <a class="nav-link" th:href="@{'/' + ${fieldName}}">[[${label}]]</a>
</li>

Requirement: Make the authority value in hasAuthority('Admin') dynamic and variable.

Any insights or suggestions to help me overcome these challenges would be greatly appreciated. Thank you!

<li th:fragment="nav-link(fieldName,label,auth)" class="nav-item active"  
    sec:authorize="hasAuthority( ${auth})">
    <a class="nav-link" th:href="@{'/' + ${fieldName}}">[[${label}]]</a>
</li>
0

There are 0 best solutions below