2013-02-27 118 views
0

在我的Spring Security中我有一個問題。當我訪問url/admin,/ client甚至/admin/addUser.jsp時,它會將我返回到登錄頁面(如有必要),但是當我訪問url/addUser(映射到Spring MVC控制器)時,它會返回我無論如何,即使用戶未被認證也是一個頁面。我需要爲安全性添加/刪除/修改什麼配置才能正常運行。Spring安全認證決定

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:security="http://www.springframework.org/schema/security" 
     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.xsd"> 

     <security:http auto-config="true" use-expressions="true"> <!--access-decision-manager-ref="accessDecisionManager"--> 
      <security:intercept-url pattern="/login" access="permitAll"/> 
      <security:intercept-url pattern="/admin*/**" access="hasRole('ROLE_ADMIN')"/> 
      <security:intercept-url pattern="/client*/**" access="hasRole('ROLE_USER')"/> 
      <security:form-login login-page="/login" default-target-url="/index" authentication-failure-url="/loginFail" 
           authentication-success-handler-ref="redirectRoleStrategy"/> 
      <security:logout logout-success-url="/logout" invalidate-session="true"/> 
      <security:access-denied-handler error-page="/403"/> 
     </security:http> 

     <bean id="daoAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider"> 
      <property name="userDetailsService" ref="userDetailsService"/> 
     </bean> 

     <bean id="authenticationManager" class="org.springframework.security.authentication.ProviderManager"> 
      <property name="providers"> 
       <list> 
        <ref local="daoAuthenticationProvider"/> 
       </list> 
      </property> 
     </bean> 

     <security:authentication-manager> 
      <security:authentication-provider user-service-ref="userDetailsService"/> 
     </security:authentication-manager> 

     <bean id="redirectRoleStrategy" class="com.payment.system.util.RoleBasedAuthenticationSuccessHandler"> 
      <property name="roleUrlMap"> 
       <map> 
        <entry key="ROLE_ADMIN" value="/admin"/> 
        <entry key="ROLE_USER" value="/client"/> 
       </map> 
      </property> 
     </bean> 
</beans> 

P.S:對了,我想到了一個主意,通過我<property name="providers">行報告我,這家酒店已被棄用。我應該替換哪個屬性?

謝謝!

+0

誰應該訪問'/ addUser' – 2013-02-27 15:55:48

+0

用戶只有ROLE_ADMIN – sidlejinks 2013-02-27 16:02:34

回答

1

只需添加

<security:intercept-url pattern="/**" access="isFullyAuthenticated()" /> 

所有你攔截的URL標籤(我的意思是它必須是最後一個)

對於PS的問題後:在javadoc的解釋只是用使用構造函數注入(通過構造函數-arg標記)。

+0

謝謝Maksym! – sidlejinks 2013-02-27 16:10:27

+0

我的回答已更新 – 2013-02-28 13:13:53

0

當您添加Maksym建議的配置時,它將解決您的身份驗證問題。但它不會解決您的授權問題。具有角色ROLE_USER的用戶將能夠訪問/ addUser。 兩種解決方案:

1)將admin應該訪問的所有URL移動到/ admin **。這個解決方案可能很複雜。

2)更換配置建議我馬克西姆通過以下配置:

<security:intercept-url pattern="/**" access="hasRole('ROLE_ADMIN')" /> 

請不就是在這種情況下與該角色ROLE_USER用戶將只能訪問到URL /客戶端*/**

請告訴我,如果你需要任何額外的幫助。

最好的問候,

Michael 

附:我建議通過命名空間定義所有bean,如下所述:http://static.springsource.org/spring-security/site/docs/3.0.x/reference/ns-config.html

它應該消除警告。

+0

謝謝大家的回覆。我已達到所需的功能。但是,在我的開始帖子結尾呢,PS呢? :) – sidlejinks 2013-02-28 09:07:38

+0

我已經更新了答案 – Michael 2013-03-03 19:10:41