2011-08-25 14 views
6

私はSpringフレームワークmvc 3 + spring security 3を使用しています。 私は春のセキュリティで役割階層を有効にしたいと思います。 http://static.springsource.org/spring-security/site/docs/3.1.x/reference/authz-arch.htmlによると、私はJava:Springセキュリティ3役割階層

<bean id="roleVoter" class="org.springframework.security.access.vote.RoleHierarchyVoter"> 
    <constructor-arg ref="roleHierarchy" /> 
</bean> 
<bean id="roleHierarchy" 
    class="org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl"> 
<property name="hierarchy"> 
    ROLE_ADMIN > ROLE_STAFF 
    ROLE_STAFF > ROLE_USER 
    ROLE_USER > ROLE_GUEST 
</property> 
</bean> 

を書く必要がありますが、私はそれをどこに置くべきか。私は私のアプリ-のsecurity.xmlにそれを入れてみました:

<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" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-3.0.xsd"> 
    <http> 
     <intercept-url pattern="/entryPost/**" access="ROLE_USER" requires-channel="https"/> 
     <intercept-url pattern="/entryDelete/**" access="ROLE_ADMIN" requires-channel="https"/> 
     <intercept-url pattern="/commentDelete/**" access="ROLE_ADMIN" requires-channel="https"/> 
     <intercept-url pattern="/login" access="ROLE_ANONYMOUS" requires-channel="https"/> 
     <form-login login-page="/login" default-target-url="/entryList/1" authentication-failure-url="/login?error=true" /> 
     <logout logout-success-url="/login" /> 
     <session-management> 
      <concurrency-control max-sessions="1" /> 
     </session-management> 
     <access-denied-handler error-page="/accessDenied"/> 
    </http> 
    <authentication-manager> 
     <authentication-provider> 
      <jdbc-user-service data-source-ref="dataSource" 
      users-by-username-query="SELECT username,password,'true' as enabled FROM member WHERE username=?" 
      authorities-by-username-query="SELECT member.username,role FROM member,memberRole WHERE member.username=? AND member.id=memberRole.member_id"/> 
     </authentication-provider> 
    </authentication-manager> 
<bean id="roleVoter" class="org.springframework.security.access.vote.RoleHierarchyVoter"> 
    <constructor-arg ref="roleHierarchy" /> 
</bean> 
<bean id="roleHierarchy" class="org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl"> 
    <property name="hierarchy"> 
     ROLE_ADMIN > ROLE_STAFF 
     ROLE_STAFF > ROLE_USER 
     ROLE_USER > ROLE_GUEST 
    </property> 
</bean> 

しかし、それは動作しません:HTTPステータス404

私はアプリ-servlet.xmlに入れた場合:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
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.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
    <context:component-scan base-package="rus.web"/> 
    <bean id="entryValidator" class="rus.domain.EntryValidator"/> 
    <bean id="commentValidator" class="rus.domain.CommentValidator"/> 
    <mvc:annotation-driven/> 
    <mvc:resources mapping="/resources/**" location="/resources/"/> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/jsp/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
     <property name="basename" value="messages"/> 
    </bean> 
    <!--<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> 
     <property name="defaultErrorView" value="error"/> 
    </bean> --> 

<bean id="roleVoter" class="org.springframework.security.access.vote.RoleHierarchyVoter"> 
    <constructor-arg ref="roleHierarchy" /> 
</bean> 
<bean id="roleHierarchy" class="org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl"> 
    <property name="hierarchy"> 
     ROLE_ADMIN > ROLE_STAFF 
     ROLE_STAFF > ROLE_USER 
     ROLE_USER > ROLE_GUEST 
    </property> 
</bean> 
</beans> 

それが例外をスローします。

org.springfram ework.beans.factory.xml.XmlBeanDefinitionStoreException:ServletContextリソース[/WEB-INF/rus-servlet.xml]のXMLドキュメントの行35が無効です。ネストされた例外はorg.xml.sax.SAXParseExceptionです:cvc-complex-type.2.3:要素のコンテンツ型は要素のみであるため、要素 'property'には文字[children]は使用できません。

org.xml.sax.SAXParseException:cvc-complex-type.2.3:要素のコンテンツタイプが要素のみであるため、 'property'要素に[children]という文字を含めることはできません。

この問題を解決するにはどうすればよいですか?

+0

私は同じ問題を抱えていました。私はここの指示に従うことで問題を解決しました:http://stackoverflow.com/questions/7809313/accessdeniedexception-if-using-rolehierarchyimpl –

答えて

6

ドキュメントでは、これが有効でない、間違っている:

<property name="hierarchy"> 
    ROLE_ADMIN > ROLE_STAFF 
    ROLE_STAFF > ROLE_USER 
    ROLE_USER > ROLE_GUEST 
</property> 

あなたは<value>内の内容をラップする必要があります。

<property name="hierarchy"> 
    <value> 
     ROLE_ADMIN > ROLE_STAFF 
     ROLE_STAFF > ROLE_USER 
     ROLE_USER > ROLE_GUEST 
    </value> 
</property> 

私は解決するためにそれらを求めて、SpringSource JIRA上の問題を提出する提案しますドキュメント。

+2

さて、それは助けましたが、それほど多くはありません...私はROLE_ADMINを使ってページentryPost /(access = "ROLE_USER")に "Access denied"と表示されます。それはなぜそうですか? – Twisty