2011-12-18 116 views
0

我想在自定义AuthenticationSuccessHandler中为用户设置LastLogin时间,但是我不知道如何检索用户名(因为所有权限函数似乎返回null,因为我不使用UserDetails)。 用户数据存储在MYSQL表中,我使用Hibernate来检索/创建/更新用户。在我的应用程序中,我使用了一个自编的User类,它与Spring用户类没有任何关系。我没有任何自定义的UserDetails/UserDetailsS​​ervice的,我想,以避免他们,因为我不能更改数据库表布局(如添加其他值)春季安全 - 在没有UserDetails/UserDetailsS​​ervice的AuthenticationSuccessHandler中获取用户名

的AuthenticationSuccessHandler看起来是这样的:

public class PostSuccessfulAuthenticationHandler extends SimpleUrlAuthenticationSuccessHandler { 
@Autowired 
private UserService userService; 
@Override 
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, 
     Authentication authentication) throws ServletException, IOException 
     { 
      userService.trackUserLogin(authentication.getName()); //Doesn't work, getName seems to return null 
      super.onAuthenticationSuccess(request, response, authentication); 
     } 
} 

我的applicationContext-security.xml文件看起来是这样的:

<?xml version="1.0" encoding="UTF-8"?> 
<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" 
xmlns:p="http://www.springframework.org/schema/p" 
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"> 

<global-method-security pre-post-annotations="enabled"/> 
<!-- authentication-success-handler-ref='authSuccHandler' --> 
<http use-expressions="true" disable-url-rewriting="true"> 
    <intercept-url pattern="..." access="hasRole('ROLE_USER')" /> 
    <form-login login-page="/index.htm" 
       authentication-failure-handler-ref='authFailureHandler' 
       authentication-success-handler-ref='authSuccHandler' 
       default-target-url='...' 
       always-use-default-target='true'/> 
    <logout logout-success-url="..."/> 
    <session-management invalid-session-url="/index.htm"> 
     <concurrency-control max-sessions="2" error-if-maximum-exceeded="true" /> 
    </session-management> 
</http> 

<authentication-manager> 
    <authentication-provider> 
     <jdbc-user-service data-source-ref="mysqldataSource" 
        authorities-by-username-query="select username, authority from benutzer where username = ?" 
        users-by-username-query="select username, password, enabled from benutzer where username = ?"/> 
    </authentication-provider> 
</authentication-manager> 

</beans:beans> 

本身工作正常(即使我只是注释掉我AuthenticationSuccessHandler的trackUserLogin线),这使我相信,有登录是一种方式来获得那你sername。谁能帮忙?

+0

您的设置说明似乎与配置不匹配。例如,你说你使用Hibernate来检索用户,但配置使用'jdbc-user-service'进行认证,而这个**是一个'UserDetailsS​​ervice',所以你正在使用'UserDetails'。目前还不清楚你的意思是“所有权威函数似乎都返回null”。你也可以说'getName()'方法“似乎返回null”,它实际上无法做到。您应该调试代码并验证发生了什么。您也可以使用ApplicationListener来代替。 – 2011-12-18 23:14:28

回答

4

尝试authentication.getPrincipal()