Springboot框架下操作Cookie的工具CookieUtil分享

2021-03-17 / 8 阅读 / Java
依赖了hutool的值转换工具,可建议改造脱离hutool。但hutool工具丰富,建议引入,避免重复造轮子

 

完整代码示例
package ***.utils;

import cn.hutool.core.convert.Convert;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * CookieUtil
 *
 * @author wangbing
 * @version 0.0.1
 * @since 1.8
 */
public class CookieUtil {

    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    public static HttpServletResponse getResponse() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
    }

    /**
     * 获取Cookie值
     *
     * @param key key
     */
    public static String getCookie(String key) {
        return getCookie(key, null);
    }

    /**
     * 获取Cookie值
     *
     * @param key key
     */
    public static String getCookie(String key, String defaultValue) {
        Cookie[] cookies = getRequest().getCookies();
        String value = getCookieValue(cookies, key);
        return Convert.toStr(value, defaultValue);
    }

    /**
     * 通过Cookies获取内容
     *
     * @param cookies Cookies
     * @return passportID
     */
    public static String getCookieValue(Cookie[] cookies, String key) {
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                // Cookie中存放的为passport的id ,Cookie名称通过ConfigToolObject获取
                if (cookie != null && cookie.getName().equals(key)) {
                    try {
                        return cookie.getValue();
                    } catch (Exception ignored) {
                    }
                }
            }
        }
        return null;
    }

    /**
     * 设置Cookie
     *
     * @param cookie
     */
    public static void setCookie(Cookie cookie) {
        getResponse().addCookie(cookie);
    }

    /**
     * 生成一个默认的Cookie,随游览器关闭而消失
     *
     * @param name
     * @param value
     */
    public static void setCookie(String name, String value) {
        Cookie cookie = new Cookie(name, value);
        cookie.setMaxAge(-1);
        cookie.setPath("/");
        getResponse().addCookie(cookie);
    }

    /**
     * 生成一个指定有效期的Cookie
     *
     * @param name
     * @param value
     * @param maxAge 最大有效期(秒)
     */
    public static void setCookie(String name, String value, int maxAge) {
        Cookie cookie = new Cookie(name, value);
        cookie.setMaxAge(maxAge);
        cookie.setPath("/");
        getResponse().addCookie(cookie);
    }

    /**
     * 删除一个Cookie
     *
     * @param name
     */
    public static void clearCookie(String name) {
        HttpServletResponse response = getResponse();
        Cookie cookie = new Cookie(name, null);
        cookie.setMaxAge(0);
        cookie.setPath("/");
        response.addCookie(cookie);
    }
}
相关推荐