SpringBoot | 第二十五章:spring boot项目之登出删除缓存session至redis和cookies

一、从request中读出cookies集合,然后封装成map,为的是能够直接通过name得到相应的cookie即get方法

public static Cookie get(HttpServletRequest request,
                         String name) {
    Map cookieMap = readCookieMap(request);
    if (cookieMap.containsKey(name)) {
        return cookieMap.get(name);
    }else {
        return null;
    }
}

/**
 * 将cookie封装成Map
 * @param request
 * @return
 */
private static Map readCookieMap(HttpServletRequest request) {
    Map cookieMap = new HashMap<>();
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (Cookie cookie: cookies) {
            cookieMap.put(cookie.getName(), cookie);
        }
    }
    return cookieMap;
}
二、在SellUserController类中的logout方法中调用CookieUtil.get()读取cookie

SpringBoot | 第二十五章:spring boot项目之登出删除缓存session至redis和cookies_第1张图片

然后清除redis和cookie

你可能感兴趣的:(#,SpringBoot2.x)