我是 Spring Security 的新手。如何添加在用户成功登录时调用的事件侦听器?另外,我需要在此侦听器中获取某种唯一的会话 ID,该 ID 应该可以进一步使用。我需要这个 ID 来与另一台服务器同步。
您需要定义一个 Spring Bean 来实现应用监听器 http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/context/ApplicationListener.html.
然后,在您的代码中执行如下操作:
public void onApplicationEvent(ApplicationEvent appEvent)
{
if (appEvent instanceof AuthenticationSuccessEvent)
{
AuthenticationSuccessEvent event = (AuthenticationSuccessEvent) appEvent;
UserDetails userDetails = (UserDetails) event.getAuthentication().getPrincipal();
// ....
}
}
然后,在您的 applicationContext.xml 文件中,只需定义该 bean,它就会自动开始接收事件:)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)