2016-05-12 220 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(); 
    }