• java cookie 工具类


      1 package com.xxx.xxx.xxx.xxx; 
      2 
      3 import java.net.URLDecoder;
      4 import java.net.URLEncoder;
      5 
      6 import javax.servlet.http.Cookie;
      7 import javax.servlet.http.HttpServletRequest;
      8 import javax.servlet.http.HttpServletResponse;
      9 
     10 import org.apache.commons.lang.StringUtils;
     11 import org.apache.http.cookie.SetCookie;
     12 import org.slf4j.Logger;
     13 import org.slf4j.LoggerFactory;
     14 import org.springframework.web.bind.annotation.ResponseBody;
     15 
     16 /**
     17 * Cookie 工具类
     18 * 
     19 * @author  
     20 * @date  
     21 */
     22 public final class CookieUtils {
     23 protected static final Logger LOGGER = LoggerFactory
     24 .getLogger(CookieUtils.class);
     25 
     26 /**
     27 * 获取cookie的值
     28 * 
     29 * @author  
     30 * @date  
     31 * @param req
     32 * @param res
     33 * @param cookieName
     34 * @param isDecoder
     35 * 是:utf-8编码 否:不编码使用默认的
     36 * @return
     37 */
     38 public static String getCookieValue(HttpServletRequest req, String cookieName, Boolean isDecoder) {
     39 Cookie[] cookieList = req.getCookies();
     40 if (cookieList == null || cookieName == null) {
     41 return null;
     42 }
     43 String retValueString = null;
     44 try {
     45 for (int i = 0; i < cookieList.length; i++) {
     46 if (cookieList[i].getName().equals(cookieName)) {
     47 if (isDecoder) {
     48 retValueString = URLDecoder.decode(
     49 cookieList[i].getValue(), "utf-8");
     50 } else {
     51 retValueString = cookieList[i].getValue();
     52 }
     53 }
     54 }
     55 } catch (Exception e) {
     56 LOGGER.error("Cookie Decode Error : ", e);
     57 }
     58 return retValueString;
     59 
     60 }
     61 
     62 /**
     63 * 设置cookie
     64 * 
     65 * @author  
     66 * @date  
     67 * @param req
     68 * @param res
     69 * @param cookieName
     70 * @param cookieValue
     71 * @param cookieMaxAge
     72 * 设置cookie最大生存时间 单位秒
     73 * @param isDecoder
     74 * 是:utf-8编码 否:不编码使用默认的
     75 */
     76 public static void setCookie(HttpServletRequest req,
     77 HttpServletResponse res, String cookieName, String cookieValue,
     78 int cookieMaxAge, Boolean isDecoder) {
     79 if (cookieValue == null) {
     80 LOGGER.info(cookieName + " 为 null");
     81 return;
     82 }
     83 try {
     84 if (isDecoder) {
     85 cookieValue = URLEncoder.encode(cookieValue, "utf-8");
     86 }
     87 Cookie cookie = new Cookie(cookieName, cookieValue);
     88 if (cookieMaxAge > 0) {
     89 cookie.setMaxAge(cookieMaxAge);
     90 }
     91 if (req != null) {
     92 cookie.setDomain(getDomainName(req)); //设置cookie域名
     93 }
     94 cookie.setPath("/");
     95 res.addCookie(cookie);
     96 } catch (Exception e) {
     97 LOGGER.error("Cookie Decode Error : ", e);
     98 }
     99 
    100 }
    101 /**
    102 * 删除cookie
    103 * @author 
    104 * @date  
    105 * @param req
    106 * @param res
    107 * @param cookieName
    108 */
    109 public static void deleteCookie(HttpServletRequest req,HttpServletResponse res,String cookieName){
    110 setCookie(req, res, cookieName, "", 0, false);
    111 }
    112 
    113 /**
    114 * 获取cookie作用域
    115 * @author gaobing
    116 * @date 2016年11月23日 上午11:13:37
    117 * @param req
    118 * @return
    119 */
    120 private static final String getDomainName(HttpServletRequest req) {
    121 String domainName = "";
    122 String serverName = req.getRequestURL().toString();
    123 
    124 //String basePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort(); 
    125 if (StringUtils.isNotBlank(serverName)) {
    126 serverName = serverName.toLowerCase().substring(7);
    127 final int end = serverName.indexOf("/");
    128 serverName = serverName.substring(0, end);
    129 final String[] domains = serverName.split("\.");// \表示转义
    130 int len = domains.length;
    131 if (len > 3) {
    132 // www.xxx.com.cn
    133 domainName = "." + domains[len - 3] + "." + domains[len - 2]
    134 + "." + domains[len - 1];
    135 } else if (len <= 3 && len > 1) {
    136 // xxx.com or xxx.cn
    137 domainName = "." + domains[len - 2] + "." + domains[len - 1];
    138 } else {
    139 domainName = serverName;
    140 }
    141 }
    142 
    143 if (domainName != "" && domainName.indexOf(":") > 0) {
    144 String[] ary = domainName.split("\:");
    145 domainName = ary[0];
    146 }
    147 return domainName;
    148 }
    149 
    150 }

    注意:删除cookie时 浏览器不会立刻删除,重启后会删除。可根据value 值判断cookie是否被删除

     

  • 相关阅读:
    WPF 调用WINForm中的ColorDialog
    WPF 获取ControlTemplate 中的控件方法
    <转> 8个超棒的免费高质量图标搜索引擎
    WPF 右键菜单动画
    WPF 创建超级连接
    WPF 数据模板的切换简单事例
    WPF 关于ShowDialog后主窗体依然能响应键盘输入法的解决方案。
    <转>强制类型转换总结
    WPF 中的MessageBox返回值获取并判断
    WPF数据绑定实现自定义数据源
  • 原文地址:https://www.cnblogs.com/wei-lai/p/6094868.html
Copyright © 2020-2023  润新知