2017-10-10 51 views
0

有没有办法来会话超时之间由Tomcat容器注销链接按用户区别?如何区分Tomcat容器的会话超时和用户按下的注销链接?

EDIT1:环境细节:的Java 1.8的Apache Tomcat 8.0.36Windows Server 2012中

上我有一个MyHttpSessionListener类,它实现javax.servlet.http.HttpSessionListener

public class MyHttpSessionListener implements HttpSessionListener {   
    @Override 
    public void sessionCreated(HttpSessionEvent httpSessionEvent) { 
     System.out.println(httpSessionEvent.getSession().getId()+" was CREATED"); 
    } 

    @Override 
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { 
     System.out.println(httpSessionEvent.getSession().getId()+" was DESTROYED"); 
    } 
} 

和内的相应配置声明文件

<listener> 
    <listener-class>com.webapp.listeners.MyHttpSessionListener</listener-class> 
</listener> 

我没有使用Spring Security我的web应用程序。

回答

1

HttpSessionListener将在Servlet级别解决它。这里一个重要的问题是你正在使用哪个应用程序/ Web服务器? 你需要看看SessionDeletedEvent and SessionExpiredEvent
现在有SessionDestroyedEvent

@Component 
public class SessionEndedListener implements ApplicationListener<SessionDestroyedEvent> { 

    @Override 
    public void onApplicationEvent(SessionDestroyedEvent event) 
    { 
     for (SecurityContext securityContext : event.getSecurityContexts()) 
     { 
      Authentication authentication = securityContext.getAuthentication(); 
      YourPrincipalClass user = (YourPrincipalClass) authentication.getPrincipal(); 
      // do something 
     } 
    } 

} 

一个尝试,还需要在web.xml

<listener> 
    <listener-class> 
     org.springframework.security.web.session.HttpSessionEventPublisher 
    </listener-class> 
</listener> 

更新回答
注册此最近的后编辑,现在您需要使用

HttpSessionBindingListener 


当从会话中删除对象时(明确由removeAttribute()方法HTTPSession或由会话的失效/失效),将调用此函数。

+0

Thanx Khan(@Freak)回复时,我没有在我的应用程序中使用** Spring Security **。有没有办法在普通的'Servlets'中检测会话超时的来源?在上面的** EDIT1 **中更新了我的环境详细信息。 – Shiva

+0

编辑之前你的问题还不清楚,你有一个Spring-MVC的标签,所以我想你可能会在春季寻找一些解决方案 – Freak

+0

@Shiva看看更新回答 – Freak