本文介绍了春季安全性:添加“成功登录事件侦听器"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是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.
推荐答案您需要定义一个实现 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 :)
更多推荐
春季安全性:添加“成功登录事件侦听器"
发布评论