Spring Security:我知道用户何时登录? [英] Spring Security: At which point do I get to know that a user logged in?

查看:363
本文介绍了Spring Security:我知道用户何时登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using spring security with URL based interceptors to secure my application. In which classes/at which points can I do some custom processing after a user logged in?

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

I specifically want to save the date the user logged in last, but I cannot figure out how to achieve this.

非常感谢你的帮助。

推荐答案

您可以考虑实施 org.springframework.context.ApplicationListener 接口。

然后,您将专门侦听 org.springframework.security.authentication.event.AuthenticationSuccessEvent

然后您可以保留用户的登录信息。

You could then persist your user's login.

可能的示例代码:

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.
        }
    }
}

这篇关于Spring Security:我知道用户何时登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆