首页 文章

在Freemarker中添加Spring库以使用JSP Taglibs以确保安全性

提问于
浏览
9

我使用带有freemarker的spring作为模板引擎 . Freemarker允许使用Jsp Taglibs,例如,通过添加安全性

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

到模板,允许我使用的例子

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

但是,Spring / Freemarker找不到taglib,除非它们被添加到类路径中,所以我补充说

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

到我项目中的pom.xml .

但无论如何,无法找到标签!我必须将spring-security-taglibs.jar添加到WEB-INF / lib文件夹中,以便找到标签 .

有人知道为什么必须将jar显式添加到lib文件夹中吗?在我看来,tomcat为什么不找到它们?

编辑@ddekany

谢谢 . 如果未将spring-security-taglibs.jar复制到WEB-INF / lib目录中,则堆栈跟踪如下所示

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 
    ...

3 回答

  • 2

    您是否已将 JspSupportServlet 包含在herehere

    [编辑]在仔细阅读你的帖子后,我建议你阅读"JSP.7.3.2"(以及之后的)JSP specification .

  • 0

    万一其他人遇到这个......

    您需要添加 spring 支持文件,如此处所述(只是一些剪切和粘贴)http://static.springsource.org/spring-webflow/docs/2.2.x/reference/html/ch13s09.html .

    然后添加一些依赖项:

    <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>
    

    假设您已完成其他所有工作,您现在应该能够将taglib添加到您的页面中 . 例如:

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

    <sec:authorize ifAllGranted = "USER_ROLE">
    你好用户
    </ sec:authorize>

    *必须添加空格b / f 'sec'才能发布

  • 7

    使用此Maven依赖项:

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

    org.springframeworkorg.springframework.security 是具有不同版本号的不同框架 .

相关问题