Spring security 3.2:自定义UserDetails& UserDetailsS​​ervice需要自定义AuthenticationManager吗?

时间:2013-10-04 13:34:43

标签: authentication service spring-security

我正在使用spring security 3.2,JSF2,Hibernate4。

我已经完成了3/4的工作:)但我的身份验证系统还没有工作。

我有一个实现UserDetailsS​​ervice的UserService,一个实现UserDetails的域类用户。

登录系统永远不会阻止用户访问安全页面,我尝试了我的数据库中不存在的用户名和密码......

感谢您的帮助。

我有一个loginBean,当他通过登录表单连接时尝试对用户进行身份验证:

  

public String login() {

        try {

            Authentication request = new UsernamePasswordAuthenticationToken(this.getUsername(), this.getPassword());
            Authentication result = authenticationManager.authenticate(request);
            SecurityContextHolder.getContext().setAuthentication(result);

        } catch (AuthenticationException e) { e.printStackTrace();}

        return "secured";
    }

我的春季安全性如下:

`<security:global-method-security jsr250-annotations="enabled" pre-post-annotations="enabled" secured-annotations="enabled" />

<security:http auto-config="true" use-expressions="true">
        <security:intercept-url pattern="/Admin" access="isAuthenticated()" />
        <security:form-login login-page="/login.xhtml" authentication-failure-url="/" >    </security:form-login>
    </security:http>

   <!-- User Data Access Object -->
   <beans:bean id="userDao" class="com.clb.genomic.lyon.dao.UserDaoImpl" >
        <beans:property name="sessionFactory" ref="sessionFactory"></beans:property>
   </beans:bean>

     <!-- User Business Object -->
   <beans:bean id="userBo" class="com.clb.genomic.lyon.bo.UserBoImpl" >
        <beans:property name="userDao" ref="userDao" />
   </beans:bean>


    <beans:bean id="login" class="com.clb.genomic.lyon.beans.LoginBean" scope ="request">
         <beans:property name="authenticationManager" ref="authenticationManager" /> 
     </beans:bean>

    <beans:bean id="standardPasswordEncoder" class="org.springframework.security.crypto.password.StandardPasswordEncoder"/>

   <security:authentication-manager alias="authenticationManager">
        <security:authentication-provider user-service-ref="userBo" >
            <security:password-encoder ref="standardPasswordEncoder"/>   
        </security:authentication-provider>
    </security:authentication-manager>`

这是出现的错误......

org.springframework.security.authentication.AuthenticationServiceException: 1
at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:109)
at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:132)
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:156)
at com.clb.genomic.lyon.beans.LoginBean.login(LoginBean.java:47).....

Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    at com.clb.genomic.lyon.dao.UserDaoImpl.loadUserByUsername(UserDaoImpl.java:59)
    at com.clb.genomic.lyon.bo.UserBoImpl.loadUserByUsername(UserBoImpl.java:68)
    at com.clb.genomic.lyon.bo.UserBoImpl$$FastClassByCGLIB$$9ea98abf.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204).....

1 个答案:

答案 0 :(得分:0)

异常堆栈跟踪显示你得到ArrayIndexOutOfBoundsException,好像你从空数组中读取?你能分享UserDaoImpl吗? 还检查了传递给loadUserByUsername()方法的值,以及该用户是否存在