在超时Spring Security上将用户重定向到登录页面

时间:2017-08-22 08:23:08

标签: java spring spring-mvc spring-security

我使用的是Spring Core 4.1.6和Spring security 4.0.1。

我想在超时时将用户重定向到登录页面。

经过一些研究后,我实施了ApplicationListener<HttpSessionDestroyedEvent>,现在我可以成功拦截超时和注销。

我在HttpSessionDestroyedEvent函数中有onApplicationEvent个对象。这个对象似乎没有任何方法,我可以从中重定向用户或返回登录模型对象。我的问题是如何将用户重定向到登录页面?

我看过this url但它没有拦截超时。我的问题更侧重于超时。

1 个答案:

答案 0 :(得分:0)

有几种方法可以做到这一点。首先,您可以通过设置@include('strah.header.header') applicationContext.xml中使用spring security auto config,它会自动将未登录的用户重定向到达某个登录页面的安全路由(例如/ userReged / **): / p>

login-page

另一种方法是检查用户在特定路线中手动登录控制器:

<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>

为了设置spring security的超时,您可以将其放在@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

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