2017-08-22 123 views
1

我正在使用Spring Core 4.1.6和Spring security 4.0.1。將用戶重定向到超時登錄頁面Spring Security

我想超時重定向用戶登錄頁面。

到目前爲止,經過一番研究,我實現了ApplicationListener<HttpSessionDestroyedEvent>,現在我可以成功地截獲超時和退出。

我有HttpSessionDestroyedEvent對象在onApplicationEvent函數。這個對象似乎沒有任何方法,我可以重定向用戶或返回登錄模型對象。我的問題是如何將用戶重定向到登錄頁面?我看過this url但它不會攔截超時。我的問題更多地集中在超時。

+1

[如何使用Spring Security自動註銷](https://stackoverflow.com/questions/27775651/how-to-make-automatically-log-out-with-spring-security) – Syroezhkin

+0

I已經看到這個網址,但它不攔截超時。我的問題更多地集中在超時。 – user4906240

+0

可能在spring security.xml中,我必須在中添加一些額外的標籤,以便將SimpleUrlLogoutSuccessHandler註冊到應用程序中? – user4906240

回答

0

這有幾種方法。

<security:http auto-config="true"> 
    <security:intercept-url pattern="/admin/**" access="ROLE_ADMIN"/> 
    <security:intercept-url pattern="/userReged/**" access="ROLE_USER"/> 
    <security:form-login 
      login-page="/" 
      default-target-url="/somePage" 
      authentication-failure-url="/user/logfailed?error" 
      username-parameter="userName" 
      password-parameter="userPassword" /> 
    <security:logout 
      logout-success-url="/?logout"/> 
</security:http> 

另一個:首先,你可以通過設置login-page它會自動重定向未登陸用戶達到安全的途徑(如/ userReged/**)到某些登錄頁面使用Spring Security自動配置在applicationContext.xml方法是檢查用戶正在登錄在你的控制器手動具體路線:

@RequestMapping("/somePage") 
public String getSomePage(Model model, HttpServletRequest request) { 

    Principal principal = request.getUserPrincipal(); 
    if (principal != null) { 

     User activeUser = userService.getUserByPhone(principal.getName()); 
     // ... 

    } else { // user is not authenticated 
     System.out.println("user is not authenticated to proceed the somePage!!!!!!!"); 
     return "redirect:/"; 
    } 
} 

爲了設置超時時間爲春季安全,你可以把這個在您的web.xml

<session-config> 
    <session-timeout> 
     1440 
     <!--mins--> 
    </session-timeout> 
</session-config> 

現在,如果您想要在確切的超時時間重定向客戶端,則可以在某些時間間隔內自動刷新客戶端的頁面。

相關問題