【发布时间】:2016-06-28 18:59:26
【问题描述】:
每当 HTTP 会话被破坏时,我都会尝试记录一条消息。 我在这个 Web 应用程序中使用 Spring Boot、Spring Security 和 Tomcat 8(嵌入式)。
在会话超时期间,sessionDestroyed() 方法被调用了 2 次strong>,所以我的消息被记录了两次。
我在两次调用期间检查了会话 ID 和 会话 ID 相同。
这就是我的代码的样子...
import org.springframework.security.core.session.SessionRegistry;
...
@Component
public class MySessionListener implements javax.servlet.http.HttpSessionListener, ApplicationContextAware {
@Autowired(required = false)
SessionRegistry sessionRegistry;
下面是 sessionDestroyed()。
@Override
public void sessionDestroyed(HttpSessionEvent se) {
HttpSession session = se.getSession();
SecurityContextImpl springSecurityContext = (SecurityContextImpl)session.getAttribute("SPRING_SECURITY_CONTEXT");
if(springSecurityContext!=null){
Authentication authentication = springSecurityContext.getAuthentication();
LdapUserDetails userDetails = (LdapUserDetailsImpl)authentication.getPrincipal();
WebAuthenticationDetails WebAuthenticationDetails = (WebAuthenticationDetails)authentication.getDetails();
String userIp = WebAuthenticationDetails.getRemoteAddress();
Log.info(userDetails.getUsername(),userIp,timestamp,"timeout or logout","session destroyed");
}
sessionRegistry.removeSessionInformation(se.getSession().getId());
logger.info("Due to timeout/logout Session is Destroyed : Session ID is..." + session.getId());
}
任何帮助将不胜感激...
注意:我注意到这个问题是 Tomcat 5 中的一个缺陷,我认为该缺陷在 Tomcat 8 中仍未修复。
【问题讨论】: