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;/** * Cookie公共方法类 * */public class CookieUtil { public static String getCookieValue(HttpServletRequest request, String cookieKey) { Cookie[] cookies = request.getCookies(); if (null != cookies) { Cookie[] var4 = cookies; int var5 = cookies.length; for (int var6 = 0; var6 < var5; ++var6) { Cookie cookie = var4[var6]; if (cookieKey.equals(cookie.getName())) { return cookie.getValue(); } } } return ""; } public static void setCookie(String cookieKey, String cookieValue, String domain, int maxAge) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletResponse response = attributes.getResponse(); Cookie loginCookie = new Cookie(cookieKey, cookieValue); if (domain != null) { loginCookie.setDomain(domain); } loginCookie.setPath("/"); loginCookie.setHttpOnly(false); loginCookie.setMaxAge(maxAge); response.addCookie(loginCookie); } public static void deleteCookie(HttpServletRequest request, String cookieKey, int maxAge) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletResponse response = attributes.getResponse(); Cookie[] cookies = request.getCookies(); if (cookies != null && cookies.length > 0) { for (Cookie cookie : cookies) { if (cookieKey.equals(cookie.getName())) { cookie.setMaxAge(maxAge); cookie.setPath("/"); response.addCookie(cookie); } } } }} cookie 工具类
2025-09-25
本文作者: linden
原文链接: cookie 工具类
版权声明: 本站所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
免责声明: 文中如涉及第三方资源,均来自互联网,仅供学习研究,禁止商业使用,如有侵权,联系我们24小时内删除!
评论0
暂时没有评论