qiguai54 2022-07-12 10:51 采纳率: 48%
浏览 91
已结题

浏览器访问本地 cookie没问题 ,然后部署上服务器,cookie就不生成了,用的cookie 工具类

问题遇到的现象和发生背景

为什么用浏览器访问本地 cookie没问题 ,然后部署上服务器,cookie就不生成了,用的cookie 工具类

问题相关代码

这是cookie工具类
package com.yc.seckill.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**

  • Cookie工具类

  • @author yc

  • @since 1.0.0

  • /
    public final class CookieUtil {

    /**

    • 得到Cookie的值, 不编码
    • @param request
    • @param cookieName
    • @return
    • /
      public static String getCookieValue(HttpServletRequest request, String cookieName) {
      return getCookieValue(request, cookieName, false);
      }

    /**

    • 得到Cookie的值,
    • @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;
      }

    /**

    • 得到Cookie的值,
    • @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;
      }

    /**

    • 设置Cookie的值 不设置生效时间默认浏览器关闭即失效,也不编码
    • /
      public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
                             String cookieValue) {
      
      setCookie(request, response, cookieName, cookieValue, -1);
      }

    /**

    • 设置Cookie的值 在指定时间内生效,但不编码
    • /
      public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
                             String cookieValue, int cookieMaxage) {
      
      setCookie(request, response, cookieName, cookieValue, cookieMaxage, false);
      }

    /**

    • 设置Cookie的值 不设置生效时间,但编码
    • /
      public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
                             String cookieValue, boolean isEncode) {
      
      setCookie(request, response, cookieName, cookieValue, -1, isEncode);
      }

    /**

    • 设置Cookie的值 在指定时间内生效, 编码参数
    • /
      public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
                             String cookieValue, int cookieMaxage, boolean isEncode) {
      
      doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, isEncode);
      }

    /**

    • 设置Cookie的值 在指定时间内生效, 编码参数(指定编码)
    • /
      public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
                             String cookieValue, int cookieMaxage, String encodeString) {
      
      doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, encodeString);
      }

    /**

    • 删除Cookie带cookie域名
    • /
      public static void deleteCookie(HttpServletRequest request, HttpServletResponse response,
                                String cookieName) {
      
      doSetCookie(request, response, cookieName, "", -1, false);
      }

    /**

    • 设置Cookie的值,并使其在指定时间内生效
    • @param cookieMaxage cookie生效的最大秒数
    • /
      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) {// 设置域名的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();
      
      }
      }

    /**

    • 设置Cookie的值,并使其在指定时间内生效
    • @param cookieMaxage cookie生效的最大秒数
    • /
      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) {// 设置域名的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();
      
      }
      }

    /**

    • 得到cookie的域名

    • /
      private static final String getDomainName(HttpServletRequest request) {
      String domainName = null;
      // 通过request对象获取访问的url地址
      String serverName = request.getRequestURL().toString();
      if (serverName == null || serverName.equals("")) {

        domainName = "";
      

      } else {

        // 将url地下转换为小写
        serverName = serverName.toLowerCase();
        // 如果url地址是以http://开头  将http://截取
        if (serverName.startsWith("http://")) {
            serverName = serverName.substring(7);
        }
        int end = serverName.length();
        // 判断url地址是否包含"/"
        if (serverName.contains("/")) {
            //得到第一个"/"出现的位置
            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;
      }
      }

  • 写回答

2条回答 默认 最新

  • qiguai54 2022-07-12 15:26
    关注

    后来仔细看了下cookieutils,发现它截取字符时有问题

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月5日
  • 已采纳回答 3月28日
  • 创建了问题 7月12日

悬赏问题

  • ¥15 工创大赛太阳能电动车项目零基础要学什么
  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的
  • ¥15 我看了您的文章,遇到了个问题。
  • ¥15 GitHubssh虚拟机连接不上
  • ¥15 装完kali之后下载Google输入法 重启电脑后出现以下状况 且退不出去 桌面消失 反复重启没用