Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding spring library for usage of JSP Taglibs for security in Freemarker

I am using spring with freemarker as the template engine. Freemarker allows to use the Jsp Taglibs, for security for example, by adding

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

to the templates, what allows me to use for example

    <@security.authorize ifNotGranted="ROLE_ADMIN">
        whatever
    </@security.authorize>

But, Spring/Freemarker cannot find the taglibs, unless they are added to the classpath, so I added

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
        <version>${spring.version}</version>
    </dependency>

to my pom.xml in my project.

But anyway, the tags couldn't be found! I had to add the spring-security-taglibs.jar into WEB-INF/lib folder for the tags to be found.

Does someone know why the jar has to be added explicitly into the lib folder?? Why aren't they found by tomcat, in my case?

EDIT @ddekany

Thank you. The stacktrace is the following, if the spring-security-taglibs.jar is not copied into the WEB-INF/lib directory

    No mapping defined for http://www.springframework.org/security/tags 
    The problematic instruction: ---------- ==> assignment: 
            security=JspTaglibs["http://www.springframework.org/security/tags"] 
            [on line 12, column 1 in home.ftl] in user-directive content.main 
            [on line 8, column 9 in home.ftl] in user-directive layout.global 
            [on line 2, column 1 in home.ftl] 
    ---------- Java backtrace for programmers: ----------      
    freemarker.template.TemplateModelException: 
            No mapping defined for http://www.springframework.org/security/tags at         
    freemarker.ext.jsp.TaglibFactory.get(TaglibFactory.java:180) at 
    ...
like image 702
matthaeus Avatar asked Aug 22 '12 09:08

matthaeus


2 Answers

In case anyone else runs into this...

You need to add the spring support files, as outlined here (just some cut & paste) http://static.springsource.org/spring-webflow/docs/2.2.x/reference/html/ch13s09.html.

And then add some dependencies:

<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-taglibs</artifactId>
  <version>2.0.0</version>
</dependency>
<dependency>
  <groupId>org.springframework.webflow</groupId>
  <artifactId>spring-faces</artifactId>
  <version>2.3.1.RELEASE</version>
</dependency>

Assuming you have everything else working, you should now be able to add the taglib to your pages. For example:

xmlns:sec="http://www.springframework.org/security/tags"

< sec:authorize ifAllGranted="USER_ROLE">
Hello user
< /sec:authorize>

*had to add a space b/f 'sec' to post it

like image 192
MattC Avatar answered Sep 30 '22 05:09

MattC


Use this Maven dependency:

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-taglibs</artifactId>
    <version>3.2.5.RELEASE</version>
</dependency>

org.springframework and org.springframework.security are different frameworks with different version numbers.

like image 41
Jerome Jaglale Avatar answered Sep 30 '22 07:09

Jerome Jaglale