UserDetailsS​​ervice未被调用

时间:2012-01-31 05:14:54

标签: java spring spring-mvc spring-security

我有这个Spring XML:

    <!-- 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的顶部看到print语句,但我没有。最令人困惑的是,我在另一个项目中使用了这个代码没有问题。有什么我想念的吗,我希望有一个副本错误?

2 个答案:

答案 0 :(得分:1)

这是我的xml文件中的代码。只有缺少的东西是allias ..尝试添加alias =“authenticationManager”也许它会有所帮助。

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

<authentication-manager alias="authenticationManager">
    <authentication-provider user-service-ref="CustomUserDetailsService" />
</authentication-manager>

答案 1 :(得分:0)

尝试指定受保护的资源

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