Menus per Role With Thymeleaf Security Tags: sec:authorize, sec:authentication and an Edit Button for the Author
One header file, three menus: a visitor sees Register and Login, Emma sees her email and Logout, Chaand also gets Admin. Then each post knows its author through @ManyToOne, and the Edit button shows only for the author or an admin.
What you will learn
- Add thymeleaf-extras-springsecurity6 and the sec namespace
- Show menu links by role with sec:authorize and print the user with sec:authentication
- Link a Post to its author with @ManyToOne
- Show an Edit button only to the author or an admin, and see why that is not a lock
Before you start
- Roles and Authorities at Login in Spring Security
Until now everybody saw the same menu: Register, Login, my account and Admin. A normal user who clicked Admin landed on a 403. Today the menu changes with the person looking at it: one header file, three different menus.
Think of a theatre usher
One building, many doors, and an usher at the front. The usher is Thymeleaf with the security tags; the guest's ticket is the logged-in user. No ticket (a visitor): Register and Login. A user ticket (Emma): her own email and Logout. An admin ticket (Chaand): the Admin door as well. Spring Security already knows who is asking and keeps it in an Authentication object (a name and some authorities). Thymeleaf walks through the template and asks each sec:authorize a yes-or-no question.
One dependency
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity6</artifactId>
</dependency>
It adds the sec attributes to Thymeleaf. The 6 is the dialect's own name; it works with the Spring Security the blog already uses.
layout.html: the menu asks
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
...
<a sec:authorize="hasRole('ADMIN')" th:href="@{/admin}">Admin</a>
<a sec:authorize="isAnonymous()" th:href="@{/register}">Register</a>
<a sec:authorize="isAnonymous()" th:href="@{/login}">Login</a>
<a sec:authorize="isAuthenticated()" th:href="@{/account}" sec:authentication="name">[email protected]</a>
<form sec:authorize="isAuthenticated()" th:action="@{/logout}" method="post">
...
</form>
Only an admin gets the Admin link. Register and Login are for visitors only. For a logged-in user, sec:authentication="name" replaces the link text with their email. Logout is a small form that posts to /logout, so Thymeleaf adds the CSRF token.
A post knows its author
@ManyToOne
private Account author;
Many posts, one author. In the database this becomes an author id column pointing at a row of the account table. The Post constructor now takes the author, and SeedData gives post 1 to Emma, post 2 to Noah and post 3 to Chaand.
post.html: who may edit?
<span class="author-name" th:text="${post.author.name}">Emma Clark</span>
...
th:if="${#authentication.name == post.author.email or #authorization.expression('hasRole(''ADMIN'')')}"
th:href="@{/posts/{id}/edit(id=${post.id})}">Edit post</a>
The page prints the author's name. The Edit link is behind a th:if: the logged-in email equals the author's email, or the user is an admin (the same hasRole check, written as an expression). Emma sees Edit on her own post and not on Noah's; Chaand sees it on Noah's because he's an admin; a visitor never sees it.
A hidden button is not a lock
Hiding the button only changes the page. The address /posts/1/edit still matches the open /posts/** rule, so a visitor who types it gets in, and so does Noah on Emma's post. Closing those doors on the server is the next step: rules per role and ownership.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
A Registration Form With Bean Validation in Spring Boot: @Valid, BindingResult and th:errors
A RegisterForm record with three annotations checks the name, email and password at the door. Bad data goes back to the form with a message under each field; good data becomes an Account row. And the password is still stored as plain text, which the next step fixes.
Spring Security 7 Form Login, BCrypt and a UserDetailsService on the Account Table
A guard at the gate and a hash in the table. A SecurityFilterChain lists the open pages and sends everything else to /login, a BCrypt PasswordEncoder bean hashes every password, and a UserDetailsService answers 'who is this email?' from the ACCOUNT table.
Roles and Authorities at Login in Spring Security: From a ROLE Column to hasRole("ADMIN")
Until now every login got .roles("USER") typed into the code. A Role enum on Account, one changed line in the UserDetailsService and one hasRole rule let Chaand open /admin while Emma gets a 403 and a visitor is sent to log in.
Fixing the Access Bugs: a Rule per Role and an Ownership Check in Spring Security
The Edit button was hidden but /posts/1/edit still opened for a visitor and for Noah. A hasAnyRole rule above /posts/** and a getForEdit check in PostService (owner or admin, else AccessDeniedException) turn each request into a 302, a 403 or a 200.
