All Downloads are FREE. Search and download functionalities are using the official Maven repository.

matrix.boot.common.utils.http.CookieUtil Maven / Gradle / Ivy

There is a newer version: 2.1.11
Show newest version
package matrix.boot.common.utils.http;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Cookie工具
 *
 * @author wangcheng
 */
public class CookieUtil {

    /**
     * 加入一个Cookie信息
     *
     * @param response 响应
     * @param key      键
     * @param value    值
     * @return 是否成功
     */
    public static boolean addSimpleCookie(HttpServletResponse response, String key, String value) {
        return CookieUtil.addCookie(response, key, value, -1, "/", null, false, false, false);
    }

    /**
     * 加入一个HttpOnly Cookie信息
     *
     * @param response 响应
     * @param key      键
     * @param value    值
     * @param isCors 是否跨域
     * @return 是否成功
     */
    public static boolean addSimpleHttpOnlyCookie(HttpServletResponse response, String key, String value, boolean isCors) {
        return CookieUtil.addCookie(response, key, value, -1, "/", null, false, true, isCors);
    }

    /**
     * 加入一个Cookie信息
     *
     * @param response   响应
     * @param key        键
     * @param value      值
     * @param maxAge     存活时间
     * @param path       路径
     * @param domain     域
     * @param isSecure   是否安全
     * @param isHttpOnly 是否HttpOnly
     * @param isCors 是否跨域
     * @return 是否成功
     */
    public static boolean addCookie(HttpServletResponse response, String key, String value, Integer maxAge, String path,
                                    String domain, boolean isSecure, boolean isHttpOnly, boolean isCors) {
        StringBuilder buffer = new StringBuilder();
        buffer.append(key).append("=").append(value).append(";");
        if (maxAge == 0) {
            buffer.append("Expires=Thu Jan 01 08:00:00 CST 1970;");
        } else if (maxAge > 0) {
            buffer.append("Max-Age=").append(maxAge).append(";");
        }
        if (domain != null) {
            buffer.append("domain=").append(domain).append(";");
        }
        if (path != null) {
            buffer.append("path=").append(path).append(";");
        }
        if (isSecure || isCors) {
            buffer.append("secure;");
        }
        if (isHttpOnly) {
            buffer.append("HTTPOnly;");
        }
        if (isCors) {
            buffer.append("SameSite=None;");
        }
        response.addHeader("Set-Cookie", buffer.toString());
        return true;
    }

    /**
     * 获取Cookie
     *
     * @param request 请求
     * @param name    键
     * @return Cookie信息
     */
    public static Cookie getCookie(HttpServletRequest request, String name) {
        Map cookieMap = readCookieMap(request);
        return cookieMap.getOrDefault(name, null);
    }

    /**
     * 获取Cookie值
     *
     * @param request 请求
     * @param name    键
     * @return 值
     */
    public static String getCookieValue(HttpServletRequest request, String name) {
        Map cookieMap = readCookieMap(request);
        if (cookieMap.containsKey(name)) {
            return cookieMap.get(name).getValue();
        } else {
            return null;
        }
    }

    /**
     * 将cookie封装到Map里面
     *
     * @param request 参数
     * @return Cookie字典项
     */
    private static Map readCookieMap(HttpServletRequest request) {
        Map cookieMap = new HashMap<>();
        Cookie[] cookies = request.getCookies();
        if (null != cookies) {
            for (Cookie cookie : cookies) {
                cookieMap.put(cookie.getName(), cookie);
            }
        }
        return cookieMap;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy