123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- /**
- * Copyright © 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
- */
- package com.ruoyi.common.utils.cookie;
- import javax.servlet.http.Cookie;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.UnsupportedEncodingException;
- import java.net.URLEncoder;
- public class CookieUtils {
- /**
- * 设置 Cookie(生成时间为1天)
- * @param name 名称
- * @param value 值
- */
- public static void setCookie(HttpServletResponse response, String name, String value) {
- setCookie(response, name, value, 60*60*24);
- }
-
- /**
- * 设置 Cookie
- * @param name 名称
- * @param value 值
- */
- public static void setCookie(HttpServletResponse response, String name, String value, String path) {
- setCookie(response, name, value, path, 60*60*24);
- }
-
- /**
- * 设置 Cookie
- * @param name 名称
- * @param value 值
- * @param maxAge 生存时间(单位秒)
- */
- public static void setCookie(HttpServletResponse response, String name, String value, int maxAge) {
- setCookie(response, name, value, "/", maxAge);
- }
- /**
- * 保存
- *
- * @param response
- * @param key
- * @param value
- * @param maxAge
- */
- public static void setCookie(HttpServletResponse response, String key, String value, String domain, String path, int maxAge, boolean isHttpOnly) {
- Cookie cookie = new Cookie(key, value);
- if (domain != null) {
- cookie.setDomain(domain);
- }
- cookie.setPath(path);
- cookie.setMaxAge(maxAge);
- cookie.setHttpOnly(isHttpOnly);
- response.addCookie(cookie);
- }
- /**
- * 设置 Cookie
- * @param name 名称
- * @param value 值
- * @param maxAge 生存时间(单位秒)
- * @param path 路径
- */
- public static void setCookie(HttpServletResponse response, String name, String value, String path, int maxAge) {
- Cookie cookie = new Cookie(name, null);
- cookie.setPath(path);
- cookie.setMaxAge(maxAge);
- try {
- cookie.setValue(URLEncoder.encode(value, "utf-8"));
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- response.addCookie(cookie);
- }
-
- /**
- * 获得指定Cookie的值
- * @param name 名称
- * @return 值
- */
- public static String getCookie(HttpServletRequest request, String name) {
- Cookie cookie = get(request, name);
- if (cookie != null) {
- return cookie.getValue();
- }
- return null;
- }
- /**
- * 查询Cookie
- *
- * @param request
- * @param key
- */
- private static Cookie get(HttpServletRequest request, String key) {
- Cookie[] arr_cookie = request.getCookies();
- if (arr_cookie != null && arr_cookie.length > 0) {
- for (Cookie cookie : arr_cookie) {
- if (cookie.getName().equals(key)) {
- return cookie;
- }
- }
- }
- return null;
- }
- /**
- * 删除Cookie
- *
- * @param request
- * @param response
- * @param key
- */
- public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String key) {
- Cookie cookie = get(request, key);
- if (cookie != null) {
- setCookie(response,key,"","/",0);
- }
- }
- }
|