0

我试图实现角色层次结构,但它不想工作。除此以外,其他一切都完美无缺。这是我的spring-security.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security-3.1.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">

<!-- Enable method-level security via annotations -->
<global-method-security secured-annotations="enabled" pre-post-annotations="enabled"/>

<!-- Configure form-based authentication -->
<http auto-config="true" use-expressions="true" entry-point-ref="securityEntryPoint" >
    <intercept-url pattern="/resources/script/jquery-ui/**" access="permitAll" />
    <intercept-url pattern="/resources/script/jquery*" access="permitAll" />
    [....]
    <intercept-url pattern="/**" access="isAuthenticated()" />

    <session-management invalid-session-url="/login.jsp?info=invalid" >
        <concurrency-control max-sessions="1" session-registry-alias="sessionRegistry" expired-url="/login.jsp?info=expired" />
    </session-management>

    <form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?error=credentials" />

    <logout logout-url="/logout" invalidate-session="true" logout-success-url="/login.jsp" />

</http>

<!-- Configure a spring security logger listener for logging authentication attempts. -->
<beans:bean id="loggerListener" class="org.springframework.security.access.event.LoggerListener"/>

<!-- Configure a delegating entry point -->
<beans:bean id="securityEntryPoint" class="org.springframework.security.web.authentication.DelegatingAuthenticationEntryPoint">

    <!-- Requests of type text/html or application/xhtml+xml should be handled by form-based authentication -->
    <beans:constructor-arg>
        <beans:map>
            <beans:entry>
                <beans:key>
                    <beans:bean class="com.test.security.AcceptHeaderRequestMatcher"/>
                </beans:key>
                <beans:bean class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
                    <beans:property name="loginFormUrl" value="/login.jsp" />
                </beans:bean>
            </beans:entry>
        </beans:map>
    </beans:constructor-arg>

    <!-- Otherwise use BASIC authentication by default -->
    <beans:property name="defaultEntryPoint">
        <beans:bean class="org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint">
            <beans:property name="realmName" value="test Web Service" />
        </beans:bean>
    </beans:property>

</beans:bean>

<!-- Configure an authentication manager via our defaultUserService -->
<authentication-manager alias="authenticationManager">
    <authentication-provider user-service-ref="defaultUserService">
        <password-encoder hash="md5" />
    </authentication-provider>
</authentication-manager>

<beans:bean id="accessDecisionManager"  class="org.springframework.security.access.vote.AffirmativeBased">
   <beans:property name="decisionVoters">
       <beans:list>
           <beans:ref bean="roleVoter" />
           <beans:ref bean="authenticatedVoter" />
       </beans:list>
   </beans:property>

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

<beans:bean id="roleHierarchy" class="org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl">
    <beans:property name="hierarchy">
        <beans:value>
            PERM_READ_ALL_USER_LIST > PERM_READ_USER_LIST
        </beans:value>
    </beans:property>
</beans:bean>

如果我尝试使用具有PERM_READ_ALL_USER_LIST 的用户访问需要PERM_READ_USER_LIST 的资源@PreAuthorize("hasRole('PERM_READ_USER_LIST')"),则它不起作用,但如果他有PERM_READ_USER_LIST,它可以工作。所以显然角色投票者没有做它的工作,但我不明白为什么......

谢谢你。

4

1 回答 1

1

您必须为MethodSecurityExpressionHandler.

有关更多详细信息,请参阅此 Stack Overflow 问题和答案。 如何在 Spring Security 3 和 Spring EL 中使用角色层次结构?

于 2013-03-07T18:55:29.127 回答