春季安全性:添加“成功登录事件侦听器" [英] Spring security: adding "On successful login event listener"

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

问题描述

我是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.

推荐答案

您需要定义一个实现然后,在您的代码中,执行以下操作:

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 :)

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

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