当前位置:网站首页>【Utils】CookieUtil
【Utils】CookieUtil
2022-07-28 14:10:00 【One duck, one duck】
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Base64;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/** * * Cookie Tool class * */
public final class CookieUtils {
/** * obtain Cookie Value , No coding * * @param request * @param cookieName * @return */
public static String getCookieValue(HttpServletRequest request, String cookieName) {
return getCookieValue(request, cookieName, false);
}
/** * obtain Cookie Value , * * @param request * @param cookieName * @return */
public static String getCookieValue(HttpServletRequest request, String cookieName, boolean isDecoder) {
Cookie[] cookieList = request.getCookies();
if (cookieList == null || cookieName == null) {
return null;
}
String retValue = null;
try {
for (int i = 0; i < cookieList.length; i++) {
if (cookieList[i].getName().equals(cookieName)) {
if (isDecoder) {
retValue = URLDecoder.decode(cookieList[i].getValue(), "UTF-8");
} else {
retValue = cookieList[i].getValue();
}
break;
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return retValue;
}
public static String getCookieValueBase64(HttpServletRequest request, String cookieName) {
Cookie[] cookieList = request.getCookies();
if (cookieList == null || cookieName == null) {
return null;
}
String retValue = null;
for (int i = 0; i < cookieList.length; i++) {
if (cookieList[i].getName().equals(cookieName)) {
retValue = new String(Base64.getDecoder().decode(cookieList[i].getValue().getBytes()));
break;
}
}
return retValue;
}
public static final void doSetCookieBase64(HttpServletRequest request, HttpServletResponse response,
String cookieName, String cookieValue, int cookieMaxage) {
try {
if (cookieValue == null) {
cookieValue = "";
} else {
cookieValue = Base64.getEncoder().encodeToString(cookieValue.getBytes());
}
Cookie cookie = new Cookie(cookieName, cookieValue);
if (cookieMaxage > 0)
cookie.setMaxAge(cookieMaxage);
if (null != request) {
// Set the domain name cookie
String domainName = getDomainName(request);
System.out.println(domainName);
// if (!"localhost".equals(domainName)) {
// cookie.setDomain(domainName);
// }
}
cookie.setPath("/");
response.addCookie(cookie);
} catch (Exception e) {
e.printStackTrace();
}
}
/** * obtain Cookie Value , * * @param request * @param cookieName * @return */
public static String getCookieValue(HttpServletRequest request, String cookieName, String encodeString) {
Cookie[] cookieList = request.getCookies();
if (cookieList == null || cookieName == null) {
return null;
}
String retValue = null;
try {
for (int i = 0; i < cookieList.length; i++) {
if (cookieList[i].getName().equals(cookieName)) {
retValue = URLDecoder.decode(cookieList[i].getValue(), encodeString);
break;
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return retValue;
}
/** * Set up Cookie Value If the effective time is not set, the browser will be disabled by default , No coding */
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
String cookieValue) {
setCookie(request, response, cookieName, cookieValue, -1);
}
/** * Set up Cookie Value Take effect within the specified time , But don't code */
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
String cookieValue, int cookieMaxage) {
setCookie(request, response, cookieName, cookieValue, cookieMaxage, false);
}
/** * Set up Cookie Value Do not set the effective time , But coding */
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
String cookieValue, boolean isEncode) {
setCookie(request, response, cookieName, cookieValue, -1, isEncode);
}
/** * Set up Cookie Value Take effect within the specified time , Encoding parameters */
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
String cookieValue, int cookieMaxage, boolean isEncode) {
doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, isEncode);
}
/** * Set up Cookie Value Take effect within the specified time , Encoding parameters ( Specified encoding ) */
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
String cookieValue, int cookieMaxage, String encodeString) {
doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, encodeString);
}
/** * Delete Cookie belt cookie domain name */
public static void deleteCookie(HttpServletRequest request, HttpServletResponse response,
String cookieName) {
doSetCookie(request, response, cookieName, "", -1, false);
}
/** * Set up Cookie Value , And make it effective within the specified time * * @param cookieMaxage cookie The maximum number of seconds in effect */
private static final void doSetCookie(HttpServletRequest request, HttpServletResponse response,
String cookieName, String cookieValue, int cookieMaxage, boolean isEncode) {
try {
if (cookieValue == null) {
cookieValue = "";
} else if (isEncode) {
cookieValue = URLEncoder.encode(cookieValue, "utf-8");
}
Cookie cookie = new Cookie(cookieName, cookieValue);
if (cookieMaxage > 0)
cookie.setMaxAge(cookieMaxage);
if (null != request) {
// Set the domain name cookie
String domainName = getDomainName(request);
System.out.println(domainName);
// if (!"localhost".equals(domainName)) {
// cookie.setDomain(domainName);
// }
}
cookie.setPath("/");
response.addCookie(cookie);
} catch (Exception e) {
e.printStackTrace();
}
}
/** * Set up Cookie Value , And make it effective within the specified time * * @param cookieMaxage cookie The maximum number of seconds in effect */
private static final void doSetCookie(HttpServletRequest request, HttpServletResponse response,
String cookieName, String cookieValue, int cookieMaxage, String encodeString) {
try {
if (cookieValue == null) {
cookieValue = "";
} else {
cookieValue = URLEncoder.encode(cookieValue, encodeString);
}
Cookie cookie = new Cookie(cookieName, cookieValue);
if (cookieMaxage > 0)
cookie.setMaxAge(cookieMaxage);
if (null != request) {
// Set the domain name cookie
String domainName = getDomainName(request);
System.out.println(domainName);
if (!"localhost".equals(domainName)) {
cookie.setDomain(domainName);
}
}
cookie.setPath("/");
response.addCookie(cookie);
} catch (Exception e) {
e.printStackTrace();
}
}
/** * obtain cookie Domain name of */
private static final String getDomainName(HttpServletRequest request) {
String domainName = null;
String serverName = request.getRequestURL().toString();
if (serverName == null || serverName.equals("")) {
domainName = "";
} else {
final int end = serverName.indexOf("/");
serverName = serverName.substring(0, end);
final String[] domains = serverName.split("\\.");
int len = domains.length;
if (len > 3) {
// www.xxx.com.cn
domainName = "." + domains[len - 3] + "." + domains[len - 2] + "." + domains[len - 1];
} else if (len <= 3 && len > 1) {
// xxx.com or xxx.cn
domainName = "." + domains[len - 2] + "." + domains[len - 1];
} else {
domainName = serverName;
}
}
if (domainName != null && domainName.indexOf(":") > 0) {
String[] ary = domainName.split("\\:");
domainName = ary[0];
}
return domainName;
}
}
边栏推荐
- Niuke multi school link with level editor i- (linear DP)
- IntersectionObserver交叉观察器
- 线程阻塞的三种情况。
- [lvgl events] event code
- 一文读懂如何部署具有外部数据库的高可用 K3s
- Poj3268 shortest path solution
- LeetCode 0143. 重排链表
- R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置palette参数自定义不同水平小提琴图的边框颜色
- leetcode(442)数组中重复的数据
- DXF读写:对齐尺寸标注文字居中、上方的位置计算
猜你喜欢

什么是自旋锁 自旋锁是指当一个线程尝试获取某个锁时,如果该锁已被其他线程占用,就一直循环检测锁是否被释放,而不是进入线程挂起或睡眠状态。 /** * 为什么用自旋锁:多个线程对同一个变量

每日一题——奖学金

Machine learning (Zhou Zhihua) Chapter 6 notes on Support Vector Learning

深度学习基础----GNN谱域和空域 (不断完善更新积累)

走进音视频的世界——FLV视频封装格式

83.(cesium之家)cesium示例如何运行

基于NoneBot2的qq机器人配置记录

Socket class understanding and learning about TCP character stream programming

【飞控开发基础教程7】疯壳·开源编队无人机-SPI(气压计数据获取)

算法---不同路径(Kotlin)
随机推荐
R language test sample proportion: use prop The test function performs the single sample proportion test to calculate the confidence interval of the p value of the successful sample proportion in the
es6你用过哪些惊艳的写法
数据库系统概论(第5版)补充习题——第一章 绪论
论文研读--Masked Generative Distillation
QT自制软键盘 最完美、最简单、跟自带虚拟键盘一样
【翻译】如何为你的私有云选择一个网络网关
【LVGL事件(Events)】事件在不同组件上的应用(一)
LeetCode 0143. 重排链表
What is the reason why the words behind word disappear when typing? How to solve it?
什么是自旋锁 自旋锁是指当一个线程尝试获取某个锁时,如果该锁已被其他线程占用,就一直循环检测锁是否被释放,而不是进入线程挂起或睡眠状态。 /** * 为什么用自旋锁:多个线程对同一个变量
Several efficient APIs commonly used in inventory operation URL
深度学习基础----GNN谱域和空域 (不断完善更新积累)
R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置palette参数自定义不同水平小提琴图的边框颜色
LeetCode 105.从前序与中序遍历序列构造二叉树 && 106.从中序与后序遍历序列构造二叉树
ES6 what amazing writing methods have you used
安全保障基于软件全生命周期-Istio的授权机制
掌握常见的几种排序-选择排序
线程阻塞的三种情况。
【Utils】ServletUtil
R语言检验样本比例:使用prop.test函数执行单样本比例检验计算总体中成功样本比例p值的置信区间(设置conf.level参数指定置信水平、置信区间的大小)