【问题标题】:How to use the Spring Security Facelets tag library in JSF如何在 JSF 中使用 Spring Security Facelets 标签库
【发布时间】:2011-12-16 10:33:11
【问题描述】:

我想使用 Spring Security Facelets 标签库 来保护我的 UI 组件 在我的 JSF 2 页面中

我对 Spring Security 版本 3.0.5 有以下依赖项:

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

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

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

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

我配置了applicationSecurity.xml来进行spring安全登录,效果很好 使用 UserDetailsS​​ervice,并在尝试添加安全定义时:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ice="http://www.icesoft.com/icefaces/component"
    xmlns:pretty="http://ocpsoft.com/prettyfaces" 
    xmlns:sec="http://www.springframework.org/security/tags">

在运行应用程序时,出现以下错误:

Warning: This page calls for XML namespace http://www.springframework.org/security/tags declared with prefix sec but no taglibrary exists for that namespace. 

参考:http://static.springsource.org/spring-security/site/petclinic-tutorial.html

请指教。

【问题讨论】:

    标签: spring jsf jakarta-ee spring-security


    【解决方案1】:

    成功实施:

    除了您的常规 Spring Security 依赖项之外,您还需要以下两个额外的 Maven 依赖项

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

    在您的 POM 文件中。

    对于 JSF 2,将以下内容另存为 /WEB-INF/springsecurity.taglib.xml

    <?xml version="1.0"?>
    <!DOCTYPE facelet-taglib PUBLIC
      "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
      "http://java.sun.com/dtd/facelet-taglib_1_0.dtd">
    <facelet-taglib>
        <namespace>http://www.springframework.org/security/tags</namespace>
        <tag>
            <tag-name>authorize</tag-name>
            <handler-class>org.springframework.faces.security.FaceletsAuthorizeTagHandler</handler-class>
        </tag>
        <function>
            <function-name>areAllGranted</function-name>
            <function-class>org.springframework.faces.security.FaceletsAuthorizeTagUtils</function-class>
            <function-signature>boolean areAllGranted(java.lang.String)</function-signature>
        </function>
        <function>
            <function-name>areAnyGranted</function-name>
            <function-class>org.springframework.faces.security.FaceletsAuthorizeTagUtils</function-class>
            <function-signature>boolean areAnyGranted(java.lang.String)</function-signature>
        </function>
        <function>
            <function-name>areNotGranted</function-name>
            <function-class>org.springframework.faces.security.FaceletsAuthorizeTagUtils</function-class>
            <function-signature>boolean areNotGranted(java.lang.String)</function-signature>
        </function>
        <function>
            <function-name>isAllowed</function-name>
            <function-class>org.springframework.faces.security.FaceletsAuthorizeTagUtils</function-class>
            <function-signature>boolean isAllowed(java.lang.String, java.lang.String)</function-signature>
        </function>
    </facelet-taglib>
    

    在web.xml中注册上述文件:

    <context-param>
        <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
        <param-value>/WEB-INF/springsecurity.taglib.xml</param-value>
    </context-param>
    

    它将解决不存在标记库的警告,现在您可以在视图中使用标记库了。您可以使用授权标签有条件地包含嵌套内容:

    <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <ui:composition xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:sec="http://www.springframework.org/security/tags">
    
        <sec:authorize ifAllGranted="ROLE_FOO, ROLE_BAR">
            Lorem ipsum dolor sit amet
        </sec:authorize>
    
        <sec:authorize ifNotGranted="ROLE_FOO, ROLE_BAR">
            Lorem ipsum dolor sit amet
        </sec:authorize>
    
        <sec:authorize ifAnyGranted="ROLE_FOO, ROLE_BAR">
            Lorem ipsum dolor sit amet
        </sec:authorize>
    
    </ui:composition>
    

    参考:https://docs.spring.io/spring-webflow/docs/2.3.x/reference/html/spring-faces.html#spring-faces-security-taglib

    【讨论】:

      【解决方案2】:

      您需要先添加 springsecurity.taglib.xml 如此处所述:

      http://docs.spring.io/autorepo/docs/webflow/2.3.x/reference/html/spring-faces.html#spring-faces-security-taglib

      并且你应该在你的类路径中有 org.springframework.faces jar 以便使用它。

      然后使用安全标签如下:

      <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional/<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
          xmlns:ui="http://java.sun.com/jsf/facelets"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:sec="http://www.springframework.org/security/tags">
      

      Reference

      【讨论】:

      • 我正在开发JSF页面,扩展名是.xhtml。
      • 当我尝试在头部添加上面的标签时,我收到以下错误:The content of elements must consist of well-formed character data or markup
      • 有什么想法需要使用 springsecurity.taglib.xml 的 jar 吗?
      • 在关注 facelets static.springsource.org/spring-webflow/docs/2.2.x/reference/… 的文档链接后它工作正常,我也必须使用以下依赖项:org.springframework.webfloworg .springframework.faces2.2.1.RELEASE
      • 请更新答案并删除 jsp 包含并添加依赖项,以便我可以将其标记为正确答案。
      【解决方案3】:

      JSF 不像 Spring MVC 那样容易...

      但您可以在此错误报告中找到解决方法

      https://jira.springsource.org/browse/SWF-1333

      罗森·斯托扬切夫的最后一条消息

      【讨论】:

        猜你喜欢
        • 2013-03-12
        • 2012-06-03
        • 1970-01-01
        • 1970-01-01
        • 2010-09-10
        • 2011-10-23
        • 2015-03-29
        • 2012-05-27
        • 2011-07-25
        相关资源
        最近更新 更多