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?
You can create an
HandlerInterceptor
that can inject the user in context:And then register it:
And then use it in your template. e.g:
I'm using a Maven/Jetty setup and the Spring Security Tags don't automatically get put into WEB-INF/lib. Therefore the following adjustments need to be made:
<#assign security=JspTaglibs[ "/WEB-INF/security.tld" ]>
or<#assign security=JspTaglibs[ "/security.tld" ]>
depending on your web root.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
Step 3: To check logged in user's loginname
Hope this helps.