2011-04-14 76 views
0

我使用基於URL的攔截器來保護我的應用程序的安全。在用戶登錄後,我可以在哪些類/哪些點上執行一些自定義處理?Spring Security:在哪一點我才知道用戶已登錄?

我特別想保存用戶最後登錄的日期,但我無法弄清楚如何實現這一點。

非常感謝您的幫助。

回答

3

您可以考慮實施org.springframework.context.ApplicationListener接口。

然後,您將專門收聽org.springframework.security.authentication.event.AuthenticationSuccessEvent

然後,您可以堅持您的用戶的登錄。

可能的示例代碼:

public void onApplicationEvent(ApplicationEvent event) { 

    if (event instanceof AuthenticationSuccessEvent) { 

     try { 

      AuthenticationSuccessEvent authenticationSuccessEvent = (AuthenticationSuccessEvent) event; 

      Authentication authentication = authenticationSuccessEvent.getAuthentication(); 

      //Persist your user's login here. 

     } catch (Exception e) { 

      // Handle exception as needed. 
     } 
    } 
}