2012-01-31 56 views
1

我有這樣的Spring XML:的UserDetailsS​​ervice沒有得到調用

<!-- Configure the authentication --> 
    <security:http auto-config="true" use-expressions="true"> 
     <security:form-login login-page="/login" 
          authentication-failure-url="/login?error=true" 
          default-target-url="/index" /> 

     <security:logout invalidate-session="true" 
         logout-success-url="/login" 
         logout-url="/logout" /> 
    </security:http> 

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

    <bean id="testUDS" class="net.safecycle.services.security.TestUserDetailService" /> 

我UserDetailsS​​ervice的實現是這樣的:

public class TestUserDetailService implements UserDetailsService { 
    public UserDetails loadUserByUsername 
    (
     String username 
    ) throws UsernameNotFoundException { 
     System.out.println ("loadUserByUsername (" + username + ")"); 

     Collection<GrantedAuthority> authorities; 
     authorities = new LinkedList<GrantedAuthority>(); 
     authorities.add (new GrantedAuthorityImpl ("Admin")); 

     UserDetails ud = new User (username, 
            "ca", 
            true, 
            true, 
            true, 
            true, 
            authorities); 
     return ud; 
    } 
} 

當我登錄的任何用戶名和密碼 'CA',我應該請參閱我的loadUserByUsername頂部的打印聲明,但我沒有。最讓人困惑的是我在另一個項目中使用了這個代碼,沒有任何問題。有什麼我失蹤,我希望的副本錯誤?

回答

0

嘗試指定哪些資源保護

<security:intercept-url pattern="/**" access="isAuthenticated()"/> 
+0

這讓我進入重定向循環。我也嘗試過'pattern =「/ **」access =「permitAll」'這給了我和原來的問題相同的結果。 – Nik 2012-01-31 06:00:47

+0

@Nik啓用日誌記錄以找出問題,如果您有log4j.properties,請添加log4j.logger.org.springframework.security = DEBUG – 2012-01-31 06:36:37

+0

問題最終以我的登錄代碼中的HTML表單出現。感謝您的日誌記錄建議,已經解決了這個問題。非常感謝。 – Nik 2012-02-01 05:37:44

1

這裏是我的xml文件我的代碼。唯一缺少的是別名..嘗試添加別名=「認證管理器」,也許它會有所幫助。

<beans:bean id="CustomUserDetailsService" 
    class="com.dennis.ehospital.hibernate.security.CustomUserDetailsService" /> 

<authentication-manager alias="authenticationManager"> 
    <authentication-provider user-service-ref="CustomUserDetailsService" /> 
</authentication-manager> 
相關問題