【问题标题】:sec:authorize doesn't work in spring security 3.2 and jsfsec:authorize 在 spring security 3.2 和 jsf 中不起作用
【发布时间】:2014-05-26 17:04:19
【问题描述】:

当我在我的页面中放置一些这样的标签时:
无论如何,用户和管理员都会在运行时显示?
xmlns:sec="http://www.springframework.org/security/tags"
spring-security-taglibs-3.2.3.RELEASE
我有 2 个文件夹(管理员和用户)
还可以使用 use-expressions="true"
进行测试 没有结果!
我用过mysql
表(用户和用户角色)...

<sec:authorize access="ROLE_ADMIN">
        <div> test Admin</div>
    </sec:authorize> 
    <sec:authorize access="ROLE_USER">
        <div> test User</div>
    </sec:authorize>
            or
  <sec:authorize  access="hasRole('ROLE_ADMIN')" >
        <h:outputText value="Admin"/>
    </sec:authorize> 
    <sec:authorize access="hasRole('ROLE_USER')" >
        <h:outputText value="User"/>
    </sec:authorize>

安全性.xml

 <http auto-config="true" use-expressions="true"  >
        <intercept-url pattern="/Admin/*" access="hasRole('ROLE_ADMIN')" />
        <intercept-url pattern="/user/*" access="hasRole('ROLE_USER')"/>
         ...
 </http>

谢谢!

【问题讨论】:

    标签: jsf-2 spring-security


    【解决方案1】:

    要使用 Spring Security Facelets 标记库,您需要创建一个 .taglib.xml 文件并将其注册到 web.xml。

    使用以下内容创建文件/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中注册上述文件taglib:

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

    您还可以在任何 JSF 组件的渲染属性或其他属性中使用多个 EL 函数之一:

    <!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">
    
        <!-- Rendered only if user has all of the listed roles -->
        <h:outputText value="Lorem ipsum dolor sit amet" rendered="#{sec:areAllGranted('ROLE_FOO, ROLE_BAR')}"/>
    
        <!-- Rendered only if user does not have any of the listed roles -->
        <h:outputText value="Lorem ipsum dolor sit amet" rendered="#{sec:areNotGranted('ROLE_FOO, ROLE_BAR')}"/>
    
        <!-- Rendered only if user has any of the listed roles -->
        <h:outputText value="Lorem ipsum dolor sit amet" rendered="#{sec:areAnyGranted('ROLE_FOO, ROLE_BAR')}"/>
    
        <!-- Rendered only if user has access to given HTTP method/URL as defined in Spring Security configuration -->
        <h:outputText value="Lorem ipsum dolor sit amet" rendered="#{sec:isAllowed('/secured/foo', 'POST')}"/>
    
    </ui:composition>
    

    测试于:

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

    【讨论】:

      猜你喜欢
      • 2018-10-31
      • 2021-06-20
      • 2016-08-03
      • 2021-02-23
      • 2020-08-05
      • 2014-12-12
      • 2010-12-13
      • 2015-04-20
      • 2014-06-14
      相关资源
      最近更新 更多