zxl
2025-06-11 861582390dc7c395897159077a547d3e7078727c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package cn.lili.common.utils;
 
import lombok.extern.slf4j.Slf4j;
 
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * CookieUtil
 *
 * @author Chopper
 * @version v1.0
 * 2020-12-14 09:32
 */
@Slf4j
public class CookieUtil {
 
 
    /**
     * 新增cookie
     *
     * @param key      key值
     * @param value    对应值
     * @param maxAge   cookie 有效时间
     * @param response 响应
     */
    public static void addCookie(String key, String value, Integer maxAge, HttpServletResponse response) {
        try {
            Cookie c = new Cookie(key, value);
            c.setMaxAge(maxAge);
            c.setPath("/");
            response.addCookie(c);
        } catch (Exception e) {
            log.error("新增cookie错误",e);
        }
    }
 
    /**
     * 删除cookie
     *
     * @param key      key值
     * @param response 响应
     */
    public static void delCookie(String key, HttpServletResponse response) {
        try {
            Cookie c = new Cookie(key, "");
            c.setMaxAge(0);
            response.addCookie(c);
        } catch (Exception e) {
            log.error("删除cookie错误",e);
        }
    }
 
    /**
     * 获取cookie
     *
     * @param key     key值
     * @param request 请求
     * @return cookie value
     */
    public static String getCookie(String key, HttpServletRequest request) {
        try {
            if (request.getCookies() == null) {
                return null;
            }
            for (int i = 0; i < request.getCookies().length; i++) {
                if (request.getCookies()[i].getName().equals(key)) {
                    return request.getCookies()[i].getValue();
                }
            }
        } catch (Exception e) {
            log.error("获取cookie错误",e);
        }
        return null;
    }
}