简体   繁体   English

在Spring MVC中,有什么方法可以请求不继续用户会话?

[英]Are there any way for request to not continue user session in Spring MVC?

I need to make an ajax request to server to check if user's session expired. 我需要向服务器发出ajax请求,以检查用户会话是否过期。 The problem is that this request will continue user session if session was not expired. 问题在于,如果会话未过期,则该请求将继续用户会话。 How to tell Spring not to continue session for this particular request? 如何告诉Spring不要为此特定请求继续会话?

One possible way to expire the HTTP session is to expire the JSESSIONID cookie. 终止HTTP会话的一种可能方法是使JSESSIONID cookie失效。 You can get an array of cookies from HttpServletRequest 's getCookies() method. 您可以从HttpServletRequest的getCookies()方法获取一个cookie数组。 You can find the cookie with name JSESSIONID from that array. 您可以从该数组中找到名称为JSESSIONID的cookie。 We can not delete the cookie using the Cookie object though. 但是,我们无法使用Cookie对象删除Cookie。 But we can expire it by setting the max age to zero ie cookie.setMaxAge(0). 但是我们可以通过将最长使用期限设置为零(即cookie.setMaxAge(0))来使它过期。 Here is a possible implementation you can try to use: 这是您可以尝试使用的可能实现:

public static void expireJSessionIdCookie(HttpServletRequest request) {
    Cookie[] cookies = request.getCookies();
    Optional<Cookie> cookieOpt = Stream.of(cookies)
            .filter(c -> c.getName().equalsIgnoreCase("JSESSIONID"))
            .findFirst();
    if (cookieOpt.isPresent()) {
        Cookie cookie = cookieOpt.get();
        cookie.setMaxAge(0);            
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM