我们使用Security来保护web应用程序,我们希望记录登录/注销/超时事件。
请允许我解释一下迄今的执行情况:
我们使用java配置和登录/注销工作,我们捕获注销事件和会话细节,比如用户名和logoutSuccessHandler()。但是,这只在单击注销链接时起作用,而在超时发生时不起作用。
在配置类中:
.and().formLogin().loginPage("/login").permitAll()
.and().logout().permitAll().logoutSuccessHandler(customLogoutSuccessHandler);以及处理程序的定义:
@Component
public class LogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler {
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
if (authentication != null) {
System.out.println("This user is closing the session: " + authentication.getName());
}
super.onLogoutSuccess(request, response, authentication);
}
}到目前为止,在登录和注销方面做得很好,当我们单击注销链接时,我们能够拦截事件并打印用户名。让我们看看超时配置..。
为了实现按超时结束的会话,我们在附加到ServletContext的侦听器中配置它:
public class SessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent event) {
System.out.println("session created");
event.getSession().setMaxInactiveInterval(15);
}
@Override
public void sessionDestroyed(HttpSessionEvent event) {
System.out.println("session destroyed");
}
}然后在初始化器中:
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
servletContext.addListener(new SessionListener());
}使用上面的代码,我们能够拦截sessionDestroyed()方法中的超时,但此时HttpSessionEvent与春季会话毫无关系(例如,无法访问用户名)。
我相信我们遗漏了一些东西来把HttpSession和春天联系起来。在我看来,我们对会话到期的配置与Spring无关。
话虽如此,我仍有一些问题:
欢迎您提出任何建议或建议演讲!
谢谢,祝您今天愉快!
发布于 2016-05-13 03:54:53
以下是一些替代方法:
1)使用,您可以找到更多用于web的免费插件。(例:jquery空闲超时)
2)有一次,我也为这个问题而奋斗,我所做的解决办法之一就是使用沙箱iframe。显示页面中的所有细节,并在页面末尾放置具有链接的iframe以注销用户。
这是个建议。
发布于 2018-02-23 18:47:55
下面是我如何在超时时获得用户名的方法:
@Override
public void sessionDestroyed(HttpSessionEvent event) {
HttpSession httpSession = event.getSession();
long lastAccessedTime = httpSession.getLastAccessedTime();
int maxInactiveTime = httpSession.getMaxInactiveInterval() * 1000; //millis
long currentTime = System.currentTimeMillis();
if ((currentTime - lastAccessedTime) >= maxInactiveTime ){
//if is a timeout
SessionInformation i = sessionRegistry().getSessionInformation(event.getSession().getId());
String username = ((User) i.getPrincipal()).getUsername();
}
}在我有的同一个文件里
@Bean
public SessionRegistry sessionRegistry() {
return new SessionRegistryImpl();
}https://stackoverflow.com/questions/37198904
复制相似问题