2017-08-22 121 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> 

现在,如果您想要在确切的超时时间重定向客户端,则可以在某些时间间隔内自动刷新客户端的页面。

相关问题