Quick revision
The short answer
We add the Thymeleaf Spring Security extras dependency, thymeleaf-extras-springsecurity6 for Spring Security 6 (use the artifact matching your Spring Security generation), declare the sec namespace, and write <li sec:authorize="hasRole('ADMIN')"> around the menu and <span sec:authentication="name"> for the user's name. Hiding the link is only usability, though: anyone can type /admin into the browser or call it with curl, so the URLs and service methods must also be protected by request or method security.
What to remember
In this answer, we added thymeleaf-extras-springsecurity6, declared the sec namespace, showed the admin menu with sec:authorize="hasRole('ADMIN')" and printed the name with sec:authentication="name", and saw the JSP taglib equivalent. Most importantly, we saw that hiding a link is only usability: the URLs and service methods must enforce the rule themselves, and complex rules should live in one authorization bean whose result the template only displays.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We add the Thymeleaf Spring Security extras dependency, thymeleaf-extras-springsecurity6 for Spring Security 6 (use the artifact matching your Spring Security generation), declare the sec namespace, and write <li sec:authorize="hasRole('ADMIN')"> around the menu and <span sec:authentication="name"> for the user's name. Hiding the link is only usability, though: anyone can type /admin into the browser or call it with curl, so the URLs and service methods must also be protected by request or method security.
Why this question is asked
Server-rendered pages often need to adapt to the user: show an admin menu to admins, show a name in the header, hide buttons a user cannot use. Spring Security provides template integration for this, and it is pleasant to use. Interviewers ask this question for the second half, though: they want to hear that hiding something in the page is not security. In this answer, we will add the dependency, write the template, look at the JSP equivalent, see why hiding is not enough and how to protect the real endpoints, and finish with how to avoid duplicating complex rules in templates.
1Adding the Dependency
Thymeleaf does not know about Spring Security by itself. A separate extras module adds the sec: attributes and the #authentication and #authorization expression objects. Its artifact name includes the Spring Security generation it supports; for Spring Security 6 it is thymeleaf-extras-springsecurity6, and Spring Boot manages its version, so we omit the version number. For Spring Security 7, check which artifact your Boot version manages.
Spring Boot detects the module and registers its dialect with the template engine automatically. Without the dependency, sec:authorize attributes are left in the output as unknown attributes and everything is shown to everyone, which is an easy mistake to miss in development, where we usually log in as an admin.
2Writing the Template
We declare the sec namespace on the html element so editors recognise the attributes, then use sec:authorize for conditional content and sec:authentication to print a property of the current Authentication.
sec:authorize takes the same expressions as Spring Security elsewhere, so hasRole('ADMIN') looks for the authority ROLE_ADMIN. When the expression is false, Thymeleaf removes the element and everything inside it from the output. sec:authentication="name" calls getName() on the Authentication; other properties, such as principal.email for a custom principal, work the same way. The placeholder text user is only shown when the template is opened as a static file.
3The JSP Equivalent
Older applications using JSP get the same features from the spring-security-taglibs module.
The idea and the expressions are identical. Only the syntax differs: a tag around the content instead of an attribute on it.
4Why Hiding the Link Is Not Enough
The template decides what HTML the browser receives. It has no influence on what requests the browser, or any other HTTP client, can send. A user who knows or guesses /admin/users can type it into the address bar, find it in the JavaScript of another page, or call it with curl. If the controller has no protection, the request succeeds.
- Template: sec:authorizehides the menu, usability only
- Filter chain: /admin/** hasRole(ADMIN)refuses the request
- Method security: @PreAuthorizeprotects the operation from any caller
3 layers, from the top down. Step through them one at a time.
Only the lower two layers stop a user who types the URL.
So the rule is: the template is for usability, so people do not see options they cannot use; the filter chain and method security are for security. Every element hidden by sec:authorize must correspond to a URL or method that enforces the same rule on the server. The good news is that both use the same expression language, so they are easy to keep aligned for simple role checks.
5Avoiding Duplicated Rules in Templates
Role checks are short, but real rules are often longer: a refund button visible to finance staff, for orders under 1,000 euros, within 30 days. Writing that as a SpEL string in the template duplicates the server's logic, and the two copies drift. Then the button shows but the server refuses, or worse, the server's rule is loosened to match a template someone tested.
Now there is one rule and the template simply displays its result. The same pattern works for lists, where each row gets a flag, and it keeps templates readable for designers who should not need to understand authorization expressions.
6Follow-up questions
Does sec:authorize work with method security expressions such as hasPermission?
The dialect evaluates web security expressions, and support for hasPermission depends on the expression handler configured for web requests. For object-level rules, computing a flag in the controller with the same bean the service uses is clearer and avoids that configuration.
Can sec:authorize check whether a URL is allowed instead of an expression?
Yes, sec:authorize-url="/admin" asks the filter chain's rules whether the current user may access that URL, which keeps the template aligned with the URL rules automatically. It does not know about method security rules, though.
Is it a problem to show the user's name from sec:authentication?
Thymeleaf escapes the output of sec:authentication, so a name containing HTML is displayed as text, not executed. Using th:utext with user data would remove that protection, so we avoid it.
7Conclusion
In this answer, we added thymeleaf-extras-springsecurity6, declared the sec namespace, showed the admin menu with sec:authorize="hasRole('ADMIN')" and printed the name with sec:authentication="name", and saw the JSP taglib equivalent. Most importantly, we saw that hiding a link is only usability: the URLs and service methods must enforce the rule themselves, and complex rules should live in one authorization bean whose result the template only displays.
Open with
I would add the Thymeleaf Spring Security extras module, thymeleaf-extras-springsecurity6 for Spring Security 6, or whichever artifact matches the Spring Security generation, and let Spring Boot manage the version and register the dialect.
Then, if they want more
In the template I declare the sec namespace, put sec:authorize="hasRole('ADMIN')" on the list item of the admin menu, and use a span with sec:authentication="name" to show the user's name. If the element's expression is false, Thymeleaf removes it from the HTML. In JSP, the same thing is the Spring Security taglib with sec:authorize access="hasRole('ADMIN')" and sec:authentication property="name".
The important point is that hiding the link is only usability. Anyone can type /admin into the address bar or call it with curl, so the filter chain must protect /admin/** with hasRole ADMIN, and sensitive service methods should have method security too, because templates do not stop requests.
For anything more complex than a role check, I would not repeat the rule in a SpEL string in the template. I would put it in one authorization bean, use it in @PreAuthorize on the service, compute a flag like canRefund in the controller with the same bean, and let the template just read that flag.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Security 6, Spring Security 7
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Security reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "In a Thymeleaf page in a Spring Boot application, show an Admin menu only to users with...": https://scrivio-interview-library.netlify.app/spring-boot/authorization/1de7f3c9f5/