Spring Security 3.1.4 taglib authorize/authentication are not working with role hierarchy in JSF 2.2 on Tomcat 7

最后都变了- 提交于 2019-12-01 06:09:30

问题


The roleHeirarchies are taken into account for Web Security Expressions defined as intercept URLs via the http namespace but not in expressions using the JSP Authorize taglib.

I read a lot of stuffs already... ref1 ref2 ref3 ref4 ref5 ref6

****EDIT:**** Ref1 and Ref6 mention stuffs about a problem with filters order and security context not available in jsp...(by the way,i'm using jsf2) maybe there is something to dig ...

EDIT 2: Is JSF handling security tag lib ? I read this and try that with no sucess

EDIT 3 : : I tried to install via Maven spring-faces 2.3.2 and spring-security-taglibs...nothing happens...Some tuto said to create a custom taglib.xml but it didn't work too , i think it was for older version...

I tested this facesContext.externalContext.isUserInRole('ROLE') and sec:authorize access="hasRole('Role')... The first works only if it's the same ROLE but it's no taking account the hierarchy stuff... And sec: autorize simply do nothing, all the roles are printed.

See the example above whith a user with ROLE 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>

This is what I tried to access for testing with the role hierachies stuff :

When a User comes only with a Role GUEST...All the tag are showed...they souldn't appear, only GUEST (cf definition at bottom )should appear :

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

This is my security configuration :

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

Thanks a lot , you are welcome if you have an other idea to test...

EDIT 3:

Why does it return 0 for RoleHierarchyVoter and 1 for WebExpressionVoter...is it normal ?

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

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

回答1:


It can be quite challenging to get the RoleHiearchy set in all the correct places. Spring WebFlow currently re-implements a number of features (i.e. it reimplements the taglibs rather than extending from Spring Security's base tag) so this approach is unlikely to work.

A different approach that will work seamlessly with JSF is is to create a RoleHierarchyAuthoritiesMapper and plug it into your AuthenticationProvider. For example, the following configuration should give you the hierarchy you are looking for.

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

A few additional highlights is that we longer need the following beans:

  • accessDecisionManager
  • defaultWebSecurityExpressionHandler
  • roleVoter


来源:https://stackoverflow.com/questions/19387228/spring-security-3-1-4-taglib-authorize-authentication-are-not-working-with-role

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!