"ThymeleafSecurity" is the common name for the Thymeleaf Extras Spring Security integration, published as thymeleaf-extras-springsecurity6 for Spring Security 6 and Spring Boot 3. It adds the sec: attributes and the #authentication and #authorization expression objects to Thymeleaf templates, so a view can show or hide content depending on whether the user is logged in and what roles they hold.
It only controls what the page renders. Hiding a button does not protect the endpoint behind it, so every restricted action must still be secured on the server.
With Spring Boot 3 and later, add the Thymeleaf starter, Spring Security and the extras module. Boot manages the version of the extras, so you do not set one.
Spring Boot 2.x uses the older artifact thymeleaf-extras-springsecurity5. Pick the artifact that matches your Spring Security major version, otherwise the dialect will not load correctly. In templates, declare the namespace on the root element:
The sec:authorize attribute renders its element only when a Spring Security expression is true. It accepts the same expressions as @PreAuthorize.
Note that hasRole('ADMIN') checks for the authority ROLE_ADMIN, because Spring Security adds the ROLE_ prefix. hasAuthority('ADMIN') checks the literal string.
Use sec:authentication to print a property of the Authentication object, or use the #authentication expression object inside th:text.
The text inside the element, "user" here, is the placeholder used when you open the file as a static prototype, and it is replaced at render time. principal.username works when the principal is a UserDetails object; with OAuth 2 login the principal is an OidcUser or OAuth2User and has different properties such as fullName or attributes.
When you use th:action, Thymeleaf with Spring Security automatically inserts the CSRF hidden field into forms that send non-GET requests. Logout in Spring Security 6 should be a POST request, so use a form instead of a plain link:
If you write action="/logout" without th:, the token is not added and the request is rejected with 403 Forbidden.
Template checks are for user experience. Enforce access in the security configuration or on methods:
Now the /admin link is hidden from non-admins by the template, and the server also rejects direct requests with 403.
sec: attributes are ignored. The extras dependency is missing, or its version does not match your Spring Security major version.#authentication is null on a public page. Use isAuthenticated() checks first, because an anonymous user has an AnonymousAuthenticationToken.ROLE_ prefix.UserDetails fields are missing. Expose them as getters and read them through principal.fieldName.No. It is only a Thymeleaf dialect that exposes Spring Security's authentication and authorization state to templates. Authentication itself is done by Spring Security.
Use thymeleaf-extras-springsecurity6. The springsecurity5 artifact is for Spring Security 5 and Spring Boot 2.
No. It only hides markup. Protect URLs with authorizeHttpRequests and services with @PreAuthorize.
Yes. The Authentication object is available the same way, but the principal type differs, so read attributes such as the user's name from the OAuth2User.
The Thymeleaf Spring Security extras connect login state and authorities to your templates through sec:authorize, sec:authentication and the #authentication object. Use the springsecurity6 artifact on current Spring Boot versions, post logout through a form, and always back template checks with real server-side rules.