2011-12-18 18 views
0

カスタムAuthenticationSuccessHandlerでユーザーのLastLogin時刻を設定しようとしていますが、ユーザー名を取得する方法はわかりません(すべての権限関数私はUserDetailsで作業していないのでnullを返すようです)。 ユーザーデータはMYSQLテーブルに格納されており、Hibernateを使用してユーザーを取得/作成/更新しています。私のアプリケーションでは、Springユーザークラスとは関係のない、自己作成のUserクラスを使用しています。私は、任意のカスタムUserDetails/UserDetailsS​​erviceを持っていないと私はAuthenticationSuccessHandlerはこのようになります(アドオンのように追加の値を)Spring Security - UserDetails/UserDetailsS​​erviceなしでAuthenticationSuccessHandlerのユーザー名を取得

をDBテーブルのレイアウトを変更することはできませんので、それらを避けたい:

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'を使用することもできます。 –

答えて

4

お試しauthentication.getPrincipal()

関連する問題