Does anyone know the freemarker tags to check spring security roles and username in freemarker file?
I found from couple of resources on the web that the following code would print the logged in username. But it is not printing the username, instead it is printing just "logged in as"
<security:authorize access="isAuthenticated()">
logged in as <security:authentication property="principal.username" />
</security:authorize>
Also checking the roles in Freemarker file is also not working. Has anyone done it before?
The following should work:
step 1: Include Spring security tag library on top of freemarker file <#assign security=JspTaglibs["http://www.springframework.org/security/tags"] />
step 2: To check the role name
<@security.authorize ifAnyGranted="ROLE_USER">
Your role is "ROLE_USER" <br/>
</@security.authorize>
Step 3: To check logged in user's loginname
<@security.authorize access="isAuthenticated()">
logged in as <@security.authentication property="principal.username" />
</@security.authorize>
<@security.authorize access="! isAuthenticated()">
Not logged in
</@security.authorize>
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With