Checking Spring Security Role in FreeMarker Template

2015-01-16T12:56:23

I have been struggling with integrating Spring Security and FreeMarker template in Spring MVC application. I used a very simple login form to submit the username and password to /j_spring_security_check. I am able to login and retrieve user's roles in Java layer. But when I tried to check Role in FreeMarker, I have no luck. I added the following on top of the FTL file:

<#assign security=JspTaglibs["http://www.springframework.org/security/tags"] />   

and used the following to check Role:

<@security.authorize ifAnyGranted="ROLE_ADMIN">
Your is Administrator
</@security.authorize>

I am getting the following error:

Jan 15, 2015 10:15:22 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet freemarker threw exception
FreeMarker template error:
The following has evaluated to null or missing:
==> security  [in template "WEB-INF/jsp/modules/user/welcome.ftl" at line 2, column 3]

Tip: If the failing expression is known to be legally null/missing, either specify a default value with myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthessis: (myOptionVar.foo)!myDefault, (myOptionVar.foo)??

The failing instruction (FTL stack trace):
----------
==> @security.authorize ifAnyGranted="ROL...  [in template "WEB-INF/jsp/modules/user/welcome.ftl" at line 2, column 1]
----------

Java stack trace (for programmers):
----------
freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...]
    at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:98)
    at freemarker.core.UnexpectedTypeException.newDesciptionBuilder(UnexpectedTypeException.java:41)
    at freemarker.core.UnexpectedTypeException.<init>(UnexpectedTypeException.java:24)
    at freemarker.core.Dot._eval(Dot.java:81)
    at freemarker.core.Expression.eval(Expression.java:111)
    at freemarker.core.UnifiedCall.accept(UnifiedCall.java:106)
    at freemarker.core.Environment.visit(Environment.java:265)
    at freemarker.core.MixedContent.accept(MixedContent.java:93)
    at freemarker.core.Environment.visit(Environment.java:265)
    at freemarker.core.Environment.process(Environment.java:243)
    at freemarker.template.Template.process(Template.java:277)
    at freemarker.ext.servlet.FreemarkerServlet.process(FreemarkerServlet.java:452)
    at freemarker.ext.servlet.FreemarkerServlet.doGet(FreemarkerServlet.java:391)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:551)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488)
    at freemarker.ext.jsp.FreeMarkerPageContext.include(FreeMarkerPageContext.java:406)
    at org.apache.tiles.request.jsp.JspRequest.doInclude(JspRequest.java:123)
    at org.apache.tiles.request.AbstractViewRequest.dispatch(AbstractViewRequest.java:47)
    at org.apache.tiles.request.render.DispatchRenderer.render(DispatchRenderer.java:47)
    at org.apache.tiles.request.render.ChainedDelegateRenderer.render(ChainedDelegateRenderer.java:68)
    at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:259)
    at org.apache.tiles.template.InsertAttributeModel.renderAttribute(InsertAttributeModel.java:188)
    at org.apache.tiles.template.InsertAttributeModel.execute(InsertAttributeModel.java:132)
    at org.apache.tiles.jsp.taglib.InsertAttributeTag.doTag(InsertAttributeTag.java:299)
    at freemarker.ext.jsp.SimpleTagDirectiveModel.execute(SimpleTagDirectiveModel.java:75)
    at freemarker.core.Environment.visit(Environment.java:341)
    at freemarker.core.UnifiedCall.accept(UnifiedCall.java:136)
    at freemarker.core.Environment.visit(Environment.java:265)
    at freemarker.core.MixedContent.accept(MixedContent.java:93)
    at freemarker.core.Environment.visit(Environment.java:265)
    at freemarker.core.Environment.process(Environment.java:243)
    at freemarker.template.Template.process(Template.java:277)
    at freemarker.ext.servlet.FreemarkerServlet.process(FreemarkerServlet.java:452)
    at freemarker.ext.servlet.FreemarkerServlet.doGet(FreemarkerServlet.java:391)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
    at org.apache.tiles.request.servlet.ServletRequest.forward(ServletRequest.java:265)
    at org.apache.tiles.request.servlet.ServletRequest.doForward(ServletRequest.java:228)
    at org.apache.tiles.request.AbstractClientRequest.dispatch(AbstractClientRequest.java:57)
    at org.apache.tiles.request.render.DispatchRenderer.render(DispatchRenderer.java:47)
    at org.apache.tiles.request.render.ChainedDelegateRenderer.render(ChainedDelegateRenderer.java:68)
    at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:259)
    at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:397)
    at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:238)
    at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:221)
    at org.apache.tiles.renderer.DefinitionRenderer.render(DefinitionRenderer.java:59)
    at org.springframework.web.servlet.view.tiles3.TilesView.renderMergedOutputModel(TilesView.java:137)
    at com.smartwcm.web.tiles.ExtTilesView.renderMergedOutputModel(ExtTilesView.java:41)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:264)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:662)

Can anyone suggest any idea why I am not able to check the Role in view (FreeMarker) file?

Copyright License:
Author:「Tom Johns」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/27977533/checking-spring-security-role-in-freemarker-template

About “Checking Spring Security Role in FreeMarker Template” questions

I have been struggling with integrating Spring Security and FreeMarker template in Spring MVC application. I used a very simple login form to submit the username and password to /j_spring_security_...
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
I'm trying to check User Role in Freemarker template. I've try this: &lt;#assign security=JspTaglibs["http://www.springframework.org/security/tags"] /&gt; And check roles by this: &lt;@security.
After uprading to Spring Security 3.2.0 and configuring the xml, the _csrf token is not working. Fundamentals: Spring 4.0.1 Spring Security 3.2.0. Freemarker Template Language Step 1 - the spring
I am using spring with freemarker as the template engine. Freemarker allows to use the Jsp Taglibs, for security for example, by adding &lt;#assign security=JspTaglibs["http://www.springframew...
hi i'm working with a apring mvc project and i'm using freemarker as my template engine, i added spring secuirty to my pages like this &lt;#assign security=JspTaglibs["http://www.springframework....
I'm trying to use Session object in Freemarker template: &lt;#assign known = Session.SPRING_SECURITY_CONTEXT?? &gt; &lt;#if known&gt; &lt;#assign user = Session.SPRING_SECURITY_CONTEXT.
I have added Springs jsp security taglib to a freemarker template because I'm using freemarker for the view of my web app rather than jsps. For anyone searching for how to set this up, I found Add...
Code on freemarker template - &lt;#assign sForm=JspTaglibs["http://www.springframework.org/tags/form"]&gt; But I catch an exception when I go to the page Error executing FreeMarker template
I'm having trouble resolving JSP tags in the Freemarker template inside a Spring test harness. My questions is: Why are the JSP references not resolving inside the JUnit environment? And how do...

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.