Spring Security:在什么时候我才能知道用户登录了



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

我特别想保存用户上次登录的日期,但我不知道如何实现这一点。

非常感谢你的帮助

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

最新更新