当前位置: 首页 > 知识库问答 >
问题:

Spring Security 3.1.4 taglib Authorize/Authentication与Tomcat 7上JSF 2.2中的角色层次结构无关

巫马庆
2023-03-14

****编辑:****Ref1和Ref6提到了JSP中不可用的过滤器顺序和安全上下文的问题...(顺便说一下,我使用的是jsf2)也许有什么要挖掘的...

编辑2:JSF处理安全标记库吗?我读了这个,试了那个,没有吸血鬼

编辑3::我试图通过Maven spring-faces 2.3.2和spring-security-taglibs安装...什么也没发生...有些tuto说要创建一个自定义taglib.xml,但它也不起作用,我想它是用于旧版本的...

    <h:outputText rendered="#{facesContext.externalContext.isUserInRole('ROLE_ADMIN_PROFILER_NGS')}"  value ="ROLE_ADMIN_PROFILER_NGS"></h:outputText> // WORKS <br></br>
<h:outputText rendered="#{facesContext.externalContext.isUserInRole('ROLE_GUEST')}"  value ="ROLE_GUEST"></h:outputText> // SHOULD APPEAR BUT NOTHING HAPPENS<br></br>
<h:outputText rendered="#{facesContext.externalContext.isUserInRole('ROLE_ADMIN')}"  value ="ROLE_ADMIN"> // SHOULD NOT APPEAR AND THAT'S THE CASE</h:outputText><br></br>

/* ALL THE THREE NEXT ARE DISPLAYED WHITHOUT CONTROL AUTORIZATION.*/

<sec:authorize access="hasRole('ROLE_ADMIN_PROFILER_NGS')">ROLE_ADMIN_PROFILER_NGS<br></br></sec:authorize>
<sec:authorize access="hasRole('ROLE_GUEST')">ROLE_GUEST <br></br></sec:authorize>
<sec:authorize access="hasRole('ROLE_ADMIN')">ROLE_ADMIN  <br></br></sec:authorize>

这是我试图使用角色hierachies进行测试的内容:

当一个用户只带着一个角色GUEST来的时候...所有的标记都显示了...他们不会出现,只有GUEST(底部的cf定义)应该出现:

    <sec:authentication property="username" />
    <sec:authorize access="hasRole('ROLE_BABAB')">BABA</sec:authorize>
    <sec:authorize access="hasRole('ROLE_GUEST')">GUEST</sec:authorize>
    <sec:authorize access="hasRole('ROLE_ADMIN')">ADMIN</sec:authorize>

这是我的安全配置:

   <security:http auto-config="true" access-decision-manager-ref="accessDecisionManager" use-expressions="true" disable-url-rewriting="true">
    <security:intercept-url pattern="/Participant/New/*" access="hasRole('ROLE_ADMIN')" />  
    <security:intercept-url pattern="/Home" access="hasRole('ROLE_GUEST')" />  
    <security:intercept-url pattern="/Login" access="hasRole('ROLE_ANONYMOUS')" />   
     <security:intercept-url pattern="/Login/Error" access="hasRole('ROLE_ANONYMOUS')" />    
    <security:form-login  login-page="/Login"  login-processing-url="/j_spring_security_check"  authentication-failure-url="/Login/Error" default-target-url="/Home" />
    <security:logout logout-url="/j_spring_security_logout" logout-success-url="/Home" delete-cookies="JSESSIONID"  invalidate-session="true"/>
    <security:anonymous/>
     <security:expression-handler ref="defaultWebSecurityExpressionHandler" />
    <security:session-management invalid-session-url="/Login" >
        <security:concurrency-control max-sessions="1" error-if-maximum-exceeded="true"  />
    </security:session-management>
   <security:port-mappings>
     <security:port-mapping http="8086" https="8443"/>
    </security:port-mappings>
  </security:http>

<beans:bean id="defaultWebSecurityExpressionHandler" class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler">
      <beans:property name="roleHierarchy" ref="roleHierarchy"/>
</beans:bean>

<beans:bean id="accessDecisionManager" class="org.springframework.security.access.vote.AffirmativeBased">
  <beans:property name="decisionVoters">
    <beans:list>
       <beans:ref bean="roleVoter" />
        <beans:bean class="org.springframework.security.web.access.expression.WebExpressionVoter">
            <beans:property name="expressionHandler" ref="defaultWebSecurityExpressionHandler"/>
        </beans:bean>
       <beans:bean class="org.springframework.security.access.vote.AuthenticatedVoter"/>

    </beans:list>
  </beans:property>
</beans:bean>


<beans:bean id="roleVoter" class="org.springframework.security.access.vote.RoleHierarchyVoter">
    <beans:constructor-arg ref="roleHierarchy" />
</beans:bean>

<beans:bean id="roleHierarchy" class="org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl">
    <beans:property name="hierarchy">
        <beans:value>
            ROLE_ADMIN > ROLE_ADMIN_PROFILER_NGS
            ROLE_ADMIN_PROFILER_NGS > ROLE_GUEST
            ROLE_GUEST > ROLE_GUEST_PROFILER_NGS  
            ROLE_ADMIN > ROLE_ADMIN_PROFILER_CGH
            ROLE_ADMIN_PROFILER_CGH > ROLE_GUEST
            ROLE_GUEST > ROLE_GUEST_PROFILER_CGH
        </beans:value>
    </beans:property>
 </beans:bean>
     <beans:bean id="login" class="com.clb.genomic.lyon.beans.LoginBean" scope ="session">
          <beans:property name="authenticationManager" ref="authenticationManager" /> 
     </beans:bean>

>     <security:authentication-manager alias="authenticationManager">
>         <security:authentication-provider user-service-ref="userBo" >
>             <security:password-encoder ref="standardPasswordEncoder"/>   
>         </security:authentication-provider>
>     </security:authentication-manager>

共有1个答案

公冶阳德
2023-03-14

在所有正确的地方设置RoleHiearchy可能相当具有挑战性。Spring WebFlow目前重新实现了许多特性(即,它重新实现了taglibs,而不是从Spring Security的基本标记中扩展出来),因此这种方法不太可能奏效。

与JSF无缝配合的另一种方法是创建RoleHierarchyAuthoritiesMapper并将其插入AuthenticationProvider。例如,下面的配置应该给出您要查找的层次结构。

<security:http auto-config="true"
               use-expressions="true"
               disable-url-rewriting="true">
    <security:intercept-url pattern="/Participant/New/*"
        access="hasRole('ROLE_ADMIN')" />
    <security:intercept-url pattern="/Home"
        access="hasRole('ROLE_GUEST')" />
    <security:intercept-url pattern="/Login"
        access="hasRole('ROLE_ANONYMOUS')" />
    <security:intercept-url pattern="/Login/Error"
        access="hasRole('ROLE_ANONYMOUS')" />
    <security:form-login login-page="/Login"
        login-processing-url="/j_spring_security_check"
        authentication-failure-url="/Login/Error" default-target-url="/Home" />
    <security:logout logout-url="/j_spring_security_logout"
        logout-success-url="/Home" delete-cookies="JSESSIONID"
        invalidate-session="true" />
    <security:anonymous />

    <security:session-management invalid-session-url="/Login">
        <security:concurrency-control max-sessions="1"
                                      error-if-maximum-exceeded="true" />
    </security:session-management>
    <security:port-mappings>
        <security:port-mapping http="8086" https="8443" />
    </security:port-mappings>
</security:http>


<beans:bean id="authenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
    <beans:property name="userDetailsService" ref="userDetailsService"/>
    <beans:property name="authoritiesMapper">
        <beans:bean class="org.springframework.security.access.hierarchicalroles.RoleHierarchyAuthoritiesMapper">
            <beans:constructor-arg ref="roleHierarchy"/>
        </beans:bean>
    </beans:property>
    <beans:property name="passwordEncoder">
        <beans:bean class="org.springframework.security.authentication.encoding.Md5PasswordEncoder"/>
    </beans:property>
</beans:bean>
<beans:bean id="roleHierarchy" class="org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl">
    <beans:property name="hierarchy">
        <beans:value>
            ROLE_ADMIN > ROLE_ADMIN_PROFILER_NGS
            ROLE_ADMIN_PROFILER_NGS > ROLE_GUEST
            ROLE_GUEST > ROLE_GUEST_PROFILER_NGS
            ROLE_ADMIN > ROLE_ADMIN_PROFILER_CGH
            ROLE_ADMIN_PROFILER_CGH > ROLE_GUEST
            ROLE_GUEST > ROLE_GUEST_PROFILER_CGH
        </beans:value>
    </beans:property>
 </beans:bean>

<security:user-service id="userDetailsService">
    <security:user name="joe" password="bf403351dfb2ae819874163aff25a49c"
        authorities="ROLE_ADMIN" />
    <security:user name="pete" password="5d2ea1f70185e4357183bb9c00187219"
        authorities="ROLE_ADMIN_PROFILER_CGH" />
</security:user-service>

一些额外的亮点是,我们更需要以下bean:

    null
 类似资料:
  • 我正在spring security中使用角色层次结构我的是 在一个服务方法中,我使用了如果角色为ROLE_ADMIN的用户登录到应用程序并尝试访问此安全方法,则会引发拒绝访问异常。

  • 我的Grails是2.2.1 Java 1.7 Spring-security-core 1.2.7.3 Spring-security-ui 0.2 在Controller方法中,我使用@security(['role_user'])作为前言,当我以ROLE_ADMIN登录时,它会抛出一个权限错误。

  • 我们正在创建一个REST API,目前我们有两种方法来定义资源。 基本上,我们有、和,其中一个有n个和一个有n个。 分层方法 该层次结构在URI中可见 要搜索所有图像,我们需要一个搜索资源 平进路 我们还需要考虑移动和修改。

  • 我有这样的层次结构: 因此,用户可以添加他的工作经验。此外,他还可以为特定的项目添加角色。 我想为用户id 1获取项目,但项目之间只有关系 获取用户 获得工作经验 获取角色 获取项目 因此,如果我有更多不同工作经验的角色,我就必须提出20个请求才能得到我的项目。这不是很有效率吗?我必须加载一些不必要的数据。。。 是否可以只创建endpoint:并按用户ID过滤它? 应该如何在API上管理它?对我来

  • 我的应用程序中有一个TreeView对象,由文件夹层次结构填充。我想尝试“爬升”此层次结构,以便获得当前选定项目的路径。我尝试使用TreeViewItem提供的DependencyObject。但是我很困惑,我怎么才能让父母成为一个树上项目,这样我才能继续“爬”上去。 有没有办法将DependencyObject转换成TreeViewItem?

  • 在Tableau中,可以构建层次结构以可视化数据。可以通过以下步骤在Tableau中创建它: 例如,考虑数据源,例如Sample-Superstore,以及它的维度和度量。 第1步: 首先转到工作表。然后, 选择一个维度,然后右键单击该维度以创建层次结构。 转到“层次结构(Hierarchy)”选项。 并且,单击下面屏幕截图中显示的“创建层次结构(Create Hierarchy)”选项。 第2步