| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- package platform.common.util;
- import org.springframework.web.util.WebUtils;
- import javax.servlet.http.Cookie;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- * @author lhf
- */
- public class CookieUtil {
- /**
- * 添加cookie
- * @param httpServletResponse
- * @param name cookie的key
- * @param value cookie的value
- * @param secure 表示这个cookie仅在https环境下才能使用
- * @param maxAge 最长存活时间 单位为秒
- * @param domain
- */
- public static void create(HttpServletResponse httpServletResponse, String name, String value, Boolean secure, Integer maxAge, String domain, String path) {
- Cookie cookie = new Cookie(name, value);
- cookie.setHttpOnly(true);
- cookie.setSecure(secure);
- cookie.setMaxAge(maxAge);
- if(domain!=null){
- cookie.setDomain(domain);
- }
- cookie.setPath(path);
- httpServletResponse.addCookie(cookie);
- }
- /**
- * 添加cookie
- * @param httpServletResponse
- * @param name cookie的key
- * @param value cookie的value
- * @param secure 表示这个cookie仅在https环境下才能使用
- * @param maxAge 最长存活时间 单位为秒
- * @param domain
- */
- public static void createWebCookie(HttpServletResponse httpServletResponse, String name, String value, Integer maxAge) {
- Cookie cookie = new Cookie(name, value);
- cookie.setHttpOnly(false);
- cookie.setSecure(false);
- cookie.setMaxAge(maxAge);
- cookie.setPath("/");
- httpServletResponse.addCookie(cookie);
- }
- /**
- * 添加cookie
- * @param httpServletResponse
- * @param name cookie的key
- * @param value cookie的value
- * @param secure 表示这个cookie仅在https环境下才能使用
- * @param maxAge 最长存活时间 单位为秒
- * @param domain
- */
- public static void createWebCookie(HttpServletResponse httpServletResponse, String name, String value, Integer maxAge) {
- Cookie cookie = new Cookie(name, value);
- cookie.setHttpOnly(false);
- cookie.setSecure(false);
- cookie.setMaxAge(maxAge);
- cookie.setPath("/");
- httpServletResponse.addCookie(cookie);
- }
- /**
- * 往根下面存一个cookie
- * * @param name cookie的key
- * @param value cookie的value
- * @param value secure 表示这个cookie仅在https环境下才能使用
- * @param domain domain
- * @param maxAge 最长存活时间 单位为秒
- * @param response
- */
- public static void create(HttpServletResponse response, String name ,String value, Boolean secure, Integer maxAge, String domain){
- create(response, name, value, secure, maxAge, domain, "/");
- }
- /**
- * 清空所有cookie
- * @param httpServletResponse
- * @param name
- */
- public static void clear(HttpServletResponse httpServletResponse, String name) {
- Cookie cookie = new Cookie(name, null);
- cookie.setPath("/");
- cookie.setHttpOnly(true);
- cookie.setMaxAge(0);
- httpServletResponse.addCookie(cookie);
- }
- public static String getValue(HttpServletRequest httpServletRequest, String name) {
- Cookie cookie = WebUtils.getCookie(httpServletRequest, name);
- return cookie != null ? cookie.getValue() : null;
- }
- /**
- * 刷新cookie
- * @param request
- * @param response
- * @param name
- * @param domain
- * @param maxAge
- */
- public static void refreshSessionCookie(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
- String name, String domain, int maxAge) {
- Cookie cookie = WebUtils.getCookie(httpServletRequest, name);
- if (cookie != null) {
- cookie.setMaxAge(maxAge);
- cookie.setDomain(domain);
- cookie.setPath("/");
- httpServletResponse.addCookie(cookie);
- }
- }
- }
|