我们使用Spring Security来保护Web应用程序,并且我们希望记录登录/注销/超时事件。SpringSecurity处理超时
请让我解释执行至今:
- 处理注销:
我们使用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的监听器?
- 我们如何才能拦截超时并能够打印类似于“Authentication.getName()”的用户详细信息?
为了实现会话由超时到期我们在附着于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());
}
有了上面的代码,我们能够intercep t sessionDestroyed()方法中的超时,但此时HttpSessionEvent与Spring会话完全无关(例如,无法访问用户名)。
我相信我们错过了将HttpSession与Spring联系起来的东西。在我看来,我们的会话过期配置与Spring无关。
话虽如此,也有一些问题,我有:
请任何建议或推荐演讲比欢迎!
谢谢你,祝你有美好的一天!
选中此http://stackoverflow.com/questions/36708580/ how-to-get-session-time-out-message-using-spring-security ... –
注册会话创建和销毁时触发弹簧事件的'HttpSessionEventPublisher'。不需要自己搞砸。您可以使用这些来标记会话的开始和结束。当你收到一个'HttpSessionDestroyedEvent'时,你可以计算它是超时还是一般注销。 –