Spring Security:我知道用户何时登录?

我正在使用带有基于URL的拦截器的spring security来保护我的应用程序。 在用户登录后,我可以在哪些类/哪个点进行自定义处理?

我特别想保存用户最后登录的日期,但我无法弄清楚如何实现这一点。

非常感谢你的帮助。

您可以考虑实现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. } } }