Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use thymleaf with spring security, using Keycloak inside the .html files?

I'm using keycloak for authentication and authorization with Spring Boot and Thymeleaf.

I'm trying to secure some areas, for example if the user has the role ROLE_TEC, I have to hide some part of the view, so I'm doing the following:

<li class="nav-item dropdown" sec:authorize="hasRole('ROLE_TEC')">
//some html
</li>

The user that I'm using has that role, but I can't see that piece of page. Why?

Do I have to add any configuration about thymeleaf for keycloak or Spring security?

EDIT

If I use: <span sec:authentication="authorities"></span>

I obtain this:

[KeycloakRole{role='TEC'}, KeycloakRole{role='USER'}]
like image 515
PaolaG Avatar asked Nov 29 '25 16:11

PaolaG


1 Answers

I think this is the solution:

<li class="nav-item dropdown" sec:authorize="hasAuthority('TEC')">
like image 103
Teo Avatar answered Dec 02 '25 07:12

Teo