2020-08-27

重写cookies 帮助类

调用cookie

@Value("${cookie_domain}")
	private String cookie_domain;
@SuppressWarnings("static-access")
拿值
String teaid =(String) CookieUtils.getCookieValue(request, "teaid");
存值
//存到线上
cookieUtils.createCookie(request, response, "teaid", et.getID(), cookie_domain);
//存到本地
cookieUtils.createCookie(request, response, "uname", et.getNAME(), localhost:9096);
private final static Integer COOKIETIME = -1;//会话级cookie,关闭浏览器失效
public synchronized static void createCookieWx(HttpServletRequest request, HttpServletResponse response, String titleName, Object value,String wwwUrl) {
		boolean fals = false;
		Cookie[] cookie = request.getCookies();
		if (cookie != null) {
			for (Cookie cookieTow : cookie) {
				if (cookieTow.getName().equals(titleName)) {
					cookieTow.setPath("/");
					cookieTow.setDomain("."+wwwUrl);
					cookieTow.setMaxAge(5 * 365 * 24 * 60 * 60); // 5年
					cookieTow.setValue(value.toString());
					response.addCookie(cookieTow);
					fals = true;
				}
			}
		}
		if (fals == false) {
			Cookie cookies = new Cookie(titleName, value.toString());
			cookies.setPath("/");
			cookies.setDomain("."+wwwUrl);
			cookies.setMaxAge(5 * 365 * 24 * 60 * 60); // 5年
			response.addCookie(cookies);
		}
	}

	public static void createCookie(HttpServletRequest request, HttpServletResponse response, String titleName, Object value,String wwwUrl) {
		Cookie cookie = new Cookie(titleName, value.toString());
		cookie.setMaxAge(5 * 365 * 24 * 60 * 60);
		cookie.setPath("/");
       if(wwwUrl.split(":").length==2){
        	wwwUrl = wwwUrl.substring(0,wwwUrl.lastIndexOf(":"));
        }
	    if(wwwUrl.contains("localhost")){
	    	cookie.setDomain(wwwUrl.replace("/", ""));
	    }else{
	    	cookie.setDomain(wwwUrl.replace("/", ""));
	    }
		response.addCookie(cookie);
	}
	
	public static void createCookieSevenDays(HttpServletRequest request, HttpServletResponse response, String titleName, Object value,String wwwUrl) {
		Cookie cookie = new Cookie(titleName, value.toString());
		cookie.setMaxAge(7 * 24 * 60 * 60);
		cookie.setPath("/");
        if(wwwUrl.split(":").length==2){
        	wwwUrl = wwwUrl.substring(0,wwwUrl.lastIndexOf(":"));
        }
	    if(wwwUrl.contains("localhost")){
	    	cookie.setDomain(wwwUrl.replace("/", ""));
	    }else{
	    	cookie.setDomain(wwwUrl.replace("/", ""));
	    }
		response.addCookie(cookie);
	}
	
	public static void createCookieData(HttpServletRequest request, HttpServletResponse response, String titleName, Object value,String wwwUrl) {
		Cookie cookie = new Cookie(titleName, value.toString());
		cookie.setMaxAge(60 * 24 * 60 * 60);
		cookie.setPath("/");
        if(wwwUrl.split(":").length==2){
        	wwwUrl = wwwUrl.substring(0,wwwUrl.lastIndexOf(":"));
        }
	    if(wwwUrl.contains("localhost")){
	    	cookie.setDomain(wwwUrl.replace("/", ""));
	    }else{
	    	cookie.setDomain("."+wwwUrl.replace("/", ""));
	    }
		response.addCookie(cookie);
	}
	
	public static  void createCookieUser(HttpServletRequest request, HttpServletResponse response, String titleName, Object value,String wwwUrl) {
		Cookie cookie = new Cookie(titleName, value.toString());
		cookie.setMaxAge(COOKIETIME);
		cookie.setPath("/");
        if(wwwUrl.split(":").length==2){
        	wwwUrl = wwwUrl.substring(0,wwwUrl.lastIndexOf(":"));
        }
	    if(wwwUrl.contains("localhost")){
	    	cookie.setDomain(wwwUrl.replace("/", ""));
	    }else{
	    	cookie.setDomain("."+wwwUrl.replace("/", ""));
	    }
		response.addCookie(cookie);
	}
	
	
	
	public static void deleteCookieAuto(HttpServletResponse response, String titleName){
		Cookie deleteNewCookie=new Cookie(titleName,null);
		deleteNewCookie.setMaxAge(0); //删除该Cookie
		deleteNewCookie.setPath("/");
		response.addCookie(deleteNewCookie);
	}
	public static void createCookieAuto(HttpServletResponse response, String titleName, Object value,String wwwUrl) {
		Cookie cookie = new Cookie(titleName, value.toString());
		cookie.setMaxAge(60*60*24);
		cookie.setPath("/");
		wwwUrl = wwwUrl.replace("http://", "");
        if(wwwUrl.split(":").length==2){
        	wwwUrl = wwwUrl.substring(0,wwwUrl.lastIndexOf(":"));
        }
	    if(wwwUrl.contains("localhost")){
	    	cookie.setDomain(wwwUrl.replace("/", ""));
	    }else{
	    	cookie.setDomain("."+wwwUrl.replace("/", ""));
	    }
		response.addCookie(cookie);
	}

	public static void createCookie(HttpServletRequest request, HttpServletResponse response, String titleName, Object value, Integer DateSize) {
		Cookie cookie = new Cookie(titleName, value.toString());
		cookie.setMaxAge(DateSize);
		cookie.setPath("/");
		response.addCookie(cookie);
		request.getSession().setAttribute("qtuser", value);
	}

	public static Cookie getCookie(HttpServletRequest request, HttpServletResponse response, String title) {
		Cookie[] cookie = request.getCookies();
		for (Cookie cookieTow : cookie) {
			if (cookieTow.getName().equals(title)) {
				return cookieTow;
			}
		}
		return null;
	}
	
	public static Object getCookieValue(HttpServletRequest request, HttpServletResponse response, String title) {
		Cookie[] cookie = request.getCookies();
		if (cookie != null) {
			for (Cookie cookieTow : cookie) {
				if (cookieTow.getName().equals(title)) {
					if (cookieTow.getMaxAge() == 0) {
						return null;
					}
					return cookieTow.getValue();
				}
			}
		}
		return null;
	}
	
	public static Object getCookieValue(HttpServletRequest request, String title) {
		Cookie[] cookie = request.getCookies();
		if (cookie != null) {
			for (Cookie cookieTow : cookie) {
				if (cookieTow.getName().equals(title)) {
					if (cookieTow.getMaxAge() == 0) {
						return null;
					}
					return cookieTow.getValue();
				}
			}
		}
		return null;
	}

	public static void YiChucookieValue(HttpServletRequest request, HttpServletResponse response, String title,String wwwUrl) {
		Cookie[] cookie = request.getCookies();
		for (Cookie cookieTow : cookie) {
			if (cookieTow.getName().equals(title)) {
				cookieTow.setMaxAge(0);
				cookieTow.setPath("/");
		        if(wwwUrl.split(":").length==2){
		        	wwwUrl = wwwUrl.substring(0,wwwUrl.lastIndexOf(":"));
		        }
			    if(wwwUrl.contains("localhost")){
			    	cookieTow.setDomain(wwwUrl.replace("/", ""));
			    }else{
			    	cookieTow.setDomain("."+wwwUrl.replace("/", ""));
			    }
			    response.addCookie(cookieTow);
			}
		}
	}
	
	public static void updateCookie(HttpServletRequest request,HttpServletResponse response,String title,String wwwUrl){
		Cookie[] cookie = request.getCookies();
		for (Cookie cookieTow : cookie) {
			if (cookieTow.getName().equals(title)) {
				cookieTow.setMaxAge(COOKIETIME);
				cookieTow.setValue(null);
				 if(wwwUrl.split(":").length==2){
			        	wwwUrl = wwwUrl.substring(0,wwwUrl.lastIndexOf(":"));
			      }
				 if(wwwUrl.contains("localhost")){
					 cookieTow.setDomain(wwwUrl.replace("/", ""));
				    }else{
				    	cookieTow.setDomain("."+wwwUrl.replace("/", ""));
				    }
				response.addCookie(cookieTow);
			}
		}
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值