我的代码如下:
@Component
public class LogoutListener implements ApplicationListener<SessionDestroyedEvent>
{
@Override
public void onApplicationEvent(SessionDestroyedEvent event)
{
System.out.println("Application event happened");
for (SecurityContext securityContext : event.getSecurityContexts())
{
System.out.println("session has ended");
}
}
}
由于我使用的是弹簧启动应用程序。我无法使用web.xml。那么我该如何配置监听器。 这个监听器正在寻找会话被破坏的事件。
答案 0 :(得分:0)
我认为答案here可能会有所帮助。在其中,您会发现,对于没有web.xml的应用程序,您必须查看ServletContext.html#addListener
答案 1 :(得分:0)
以下是一个例子:
public class SessionEventListener extends HttpSessionEventPublisher {
public void sessionCreated(HttpSessionEvent event) {
super.sessionCreated(event);
event.getSession().setMaxInactiveInterval(60*3);
}
@Override
public void sessionDestroyed(HttpSessionEvent event) {
String name = null;
SessionRegistry sessionRegistry = getSessionRegistry(event);
SessionInformation sessionInfo = (sessionRegistry != null ? sessionRegistry
.getSessionInformation(event.getSession().getId()) : null);
UserDetails ud = null;
if (sessionInfo != null) {
ud = (UserDetails) sessionInfo.getPrincipal();}
if (ud != null) {
name = ud.getUsername();
// YOUR METHOD IS CALLED HERE
getMyService(event).myMethod(name);
}
super.sessionDestroyed(event);
}
public YourBean4Service getMyService(HttpSessionEvent event) {
HttpSession session = event.getSession();
ApplicationContext ctx =
WebApplicationContextUtils.
getWebApplicationContext(session.getServletContext());
return (YourBean4Service) ctx.getBean("yourBean4Service");
}
public SessionRegistry getSessionRegistry(HttpSessionEvent event) {
HttpSession session = event.getSession();
ApplicationContext ctx =
WebApplicationContextUtils.
getWebApplicationContext(session.getServletContext());
return (SessionRegistry) ctx.getBean("sessionRegistry");
}
}
我的相关主题是here