Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking Spring security roles and logged username in Freemarker template

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?

like image 353
bfab Avatar asked Feb 05 '15 15:02

bfab


1 Answers

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.

like image 82
Vimal Mathew Avatar answered Sep 21 '22 10:09

Vimal Mathew