jsf - Spring Security 3.1.4 taglib 授权/身份验证不适用于 Tomcat 7 上的 JSF 2.2 中的角色层次结构

标签 jsf spring-security hierarchy roles taglib

通过 http 命名空间定义为拦截 URL 的 Web 安全表达式考虑了 roleHeirarchies,但在使用 JSP Authorize 标签库的表达式中不考虑。

我已经读了很多东西... ref1 ref2 ref3 ref4 ref5 ref6

****编辑:**** Ref1 和 Ref6 提到了过滤器顺序和安全上下文在 jsp 中不可用的问题......(顺便说一下,我正在使用 jsf2)也许在那里是值得挖掘的东西......

编辑 2: JSF 是否处理安全标记库?我读了this并尝试 that没有成功

编辑 3::我尝试通过 Maven spring-faces 2.3.2 和 spring-security-taglibs 安装...没有任何反应...一些 tuto 说创建自定义 taglib.xml 但它也不起作用,我认为它适用于旧版本......

我测试了这个 facesContext.externalContext.isUserInRole('ROLE') 和 sec:authorize access="hasRole('Role')... 第一个只有当它是相同的 ROLE 时才有效,但它没有考虑层次结构的东西。 .. sec: autorize 什么都不做,所有的角色都被打印出来。

请参阅上面的示例,其中用户的角色为 ROLE_ADMIN_PROFILER_NGS:

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

这是我尝试访问以使用角色层次结构进行测试的内容:

当用户仅具有角色 GUEST 时...显示所有标签...它们不会出现,只有 GUEST(参见底部的定义)应该出现:

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

非常感谢,如果您有其他想法可以测试...欢迎您...

编辑 3:

为什么它为 RoleHierarchyVoter 返回 0 而为 WebExpressionVoter 返回 1...这正常吗?

14:48:32,861 DEBUG FilterSecurityInterceptor:194 - Secure object: FilterInvocation: URL: /Home; Attributes: [hasRole('ROLE_GUEST')] 14:48:32,861 DEBUG FilterSecurityInterceptor:310 - Previously Authenticated: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@43a64f5f: Principal: com.clb.genomic.lyon.model.User@d46: Username: jp; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_ADMIN_PROFILER_NGS; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@0: RemoteIpAddress: 192.168.154.18; SessionId: 084939D4E097F41ACA6A1F24CD8390BE; Granted Authorities: ROLE_ADMIN_PROFILER_NGS 14:48:32,861 DEBUG RoleHierarchyImpl:117 - getReachableGrantedAuthorities() - From the roles [ROLE_ADMIN_PROFILER_NGS] one can reach [ROLE_GUEST_PROFILER_NGS, ROLE_GUEST_PROFILER_CGH, ROLE_ADMIN_PROFILER_NGS, ROLE_GUEST] in zero or more steps. 14:48:32,861 DEBUG AffirmativeBased:65 - Voter: org.springframework.security.access.vote.RoleHierarchyVoter@6ff43d69, returned: 0 14:48:32,862 DEBUG RoleHierarchyImpl:117 - getReachableGrantedAuthorities() - From the roles [ROLE_ADMIN_PROFILER_NGS] one can reach [ROLE_GUEST_PROFILER_NGS, ROLE_GUEST_PROFILER_CGH, ROLE_ADMIN_PROFILER_NGS, ROLE_GUEST] in zero or more steps. 14:48:32,862 DEBUG AffirmativeBased:65 - Voter: org.springframework.security.web.access.expression.WebExpressionVoter@3fe932d5, returned: 1 14:48:32,862 DEBUG FilterSecurityInterceptor:215 - Authorization successful

编辑 5:

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

最佳答案

在所有正确的位置设置 RoleHiearchy 可能非常具有挑战性。 Spring WebFlow 当前重新实现了许多功能(即 it reimplements the 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>

一些额外的亮点是我们不再需要以下 beans:

  • 访问决策管理器
  • 默认WebSecurityExpressionHandler
  • 角色投票者

关于jsf - Spring Security 3.1.4 taglib 授权/身份验证不适用于 Tomcat 7 上的 JSF 2.2 中的角色层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19387228/

相关文章:

jsf - 使用向导时,如何将输入框值绑定(bind)到支持 bean 属性中的映射值

java - 一起使用 Spring jSTL 和 spring 表单标签

swift - 创建自定义 UITableView 类

Java JSF h :outPutText - no tag was defined

java - 加载一组带有primefaces的图像

java - Richfaces 文件上传组件 - 文件上传事件之前

java - spring中如何子类并发控制,抛出什么异常?

spring-boot - 如何使用 keycloak 在 spring security 中同时处理安全 Web 浏览器请求和 Restful 请求

python - 如何解析多个层次结构下的 XML 文件?

Java 类加载器层次结构