You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.2 KiB

package com.fr.plugin.xx.ltqc.auth.utils;
import com.fr.stable.StringUtils;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author xx
* @since 2022/02/14
*/
public class CookieUtils {
/**
* 根据name获取cookie
* @param request
* @param name
* @return cookie对象
*/
public static Cookie getCookie(HttpServletRequest request, String name) {
Cookie[] cookies = request.getCookies();
if (cookies == null || name == null || name.length() == 0) {
return null;
}
Cookie cookie = null;
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals(name)) {
cookie = cookies[i];
break;
}
}
return cookie;
}
/**
* 新增cookie,过期时间为页面关闭
* @param response
* @param name
* @param value
* @param domain
*/
public static void setCookie(HttpServletResponse response, String name,
String value, String domain) {
if (value == null) {
value = "";
}
Cookie cookie = new Cookie(name, value);
if (!StringUtils.isEmpty(domain)) {
cookie.setDomain(domain);
}
cookie.setPath("/");
response.addCookie(cookie);
}
}