Spring security:添加“成功登录事件监听器" [英] Spring security: adding "On successful login event listener"

查看:155
本文介绍了Spring security:添加“成功登录事件监听器"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Spring Security 的新手.如何添加将在用户成功登录时调用的事件侦听器?此外,我需要在此侦听器中获得某种唯一的会话 ID,该 ID 应该可以进一步使用.我需要此 ID 才能与另一台服务器同步.

I'm new to Spring Security. How do I add an event listener which will be called as a user logs in successfully? Also I need to get some kind of unique session ID in this listener which should be available further on. I need this ID to synchronize with another server.

推荐答案

你需要定义一个 Spring Bean 来实现 ApplicationListener.

You need to define a Spring Bean which implements ApplicationListener.

然后,在您的代码中,执行以下操作:

Then, in your code, do something like this:

public void onApplicationEvent(ApplicationEvent appEvent)
{
    if (appEvent instanceof AuthenticationSuccessEvent)
    {
        AuthenticationSuccessEvent event = (AuthenticationSuccessEvent) appEvent;
        UserDetails userDetails = (UserDetails) event.getAuthentication().getPrincipal();

        // ....
    }
}

然后,在您的 applicationContext.xml 文件中,只需定义该 bean,它就会自动开始接收事件 :)

Then, in your applicationContext.xml file, just define that bean and it will automatically start receiving events :)

这篇关于Spring security:添加“成功登录事件监听器"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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