2016-05-12 116 views
2

我们使用Spring Security来保护Web应用程序,并且我们希望记录登录/注销/超时事件。SpringSecurity处理超时

请让我解释执行至今:

  1. 处理注销:

我们使用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()); 
    } 
    

    有了上面的代码,我们能够intercep t sessionDestroyed()方法中的超时,但此时HttpSessionEvent与Spring会话完全无关(例如,无法访问用户名)。

    我相信我们错过了将HttpSession与Spring联系起来的东西。在我看来,我们的会话过期配置与Spring无关。

    话虽如此,也有一些问题,我有:

    1. 有没有更好的方式来处理与春季会议(会话超时为例),所以我们不必创建在ServletContext的监听器?
    2. 我们如何才能拦截超时并能够打印类似于“Authentication.getName()”的用户详细信息?

    请任何建议或推荐演讲比欢迎!

    谢谢你,祝你有美好的一天!

    +0

    选中此http://stackoverflow.com/questions/36708580/ how-to-get-session-time-out-message-using-spring-security ... –

    +0

    注册会话创建和销毁时触发弹簧事件的'HttpSessionEventPublisher'。不需要自己搞砸。您可以使用这些来标记会话的开始和结束。当你收到一个'HttpSessionDestroyedEvent'时,你可以计算它是超时还是一般注销。 –

    回答

    0

    这里有一些替代方法:

    1)使用jQuery超时,你可以找到更多的免费插件,与您的网站使用。 (例如:jquery idle timeout

    2)有一次,我也为这个问题而斗争,我一直在做的一个解决方法是使用沙盒iframe。显示页面中和页面末尾的所有详细信息,放置具有链接的iframe以注销用户。

    这是一些建议。

    0

    以下是我得到它的工作再上一个超时的用户名:

    @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(); 
    }