当前位置:网站首页>【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;
}
}
边栏推荐
- 线程阻塞的三种情况。
- MVC模型:日历系统
- 83.(cesium之家)cesium示例如何运行
- 【Try to Hack】HFish蜜罐部署
- 你真的了解esModule吗
- P1797 heavy transportation problem solution
- .net for subtraction, intersection and union of complex type sets
- 文献阅读(245)Roller
- What is the reason why the words behind word disappear when typing? How to solve it?
- 30 day question brushing plan (III)
猜你喜欢

Product Manager: job responsibility table

MySQL开发技巧——视图

Clickhouse架构与设计

Leetcode 105. construct binary tree from preorder and inorder traversal sequence & 106. construct binary tree from inorder and postorder traversal sequence

Qt5 development from introduction to mastery -- the first overview

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

论文研读--Masked Generative Distillation

LeetCode 0142.环形链表 II

《机器学习》(周志华) 第6章 支持向量 学习心得 笔记

目标检测:速度和准确性比较(Fater R-CNN,R-FCN,SSD,FPN,RetinaNet和YOLOv3)
随机推荐
Clickhouse分布式集群搭建
LeetCode 105.从前序与中序遍历序列构造二叉树 && 106.从中序与后序遍历序列构造二叉树
30 day question brushing plan (IV)
R语言因子数据的表格和列联表(交叉表)生成:使用summay函数分析列表查看卡方检验结果判断两个因子变量是否独立(使用卡方检验验证独立性)
LeetCode 1331.数组序号转换
走进音视频的世界——FLV视频封装格式
30 day question brushing plan (III)
Socket类关于TCP字符流编程的理解学习
Istio IV fault injection and link tracking
es6你用过哪些惊艳的写法
R language uses dpois function to generate Poisson distribution density data and plot function to visualize Poisson distribution density data
Postgresql14安装及主从配置
Rust from introduction to mastery 01 introduction
qml 图片预览
VOS3000如何呼入送到OKCC
MVC模型:日历系统
83.(cesium之家)cesium示例如何运行
How to effectively conduct the review meeting (Part 1)?
R language ggplot2 visualization: visualize the scatter diagram and add text labels to the data points in the scatter diagram, using geom of ggrep package_ text_ The rep function avoids overlapping da
MySql5.5之后的默认存储引擎为InnoDB。