部分浏览器cookie无法传输cookie,谷歌51-66版本

public void setCookieSameSite (HttpServletRequest request,HttpServletResponse response){
String userAgent = request.getHeader("user-agent");
Browser browser = UserAgent.parseUserAgentString(userAgent).getBrowser();
if(browser)&&browser.getName().contains("Chrome"){
    Version version = browser.getVersion(userAgent);
    if(version.getMajorVersion()!=null){
        try{
            int majorVersion = Integer.parseInt(version.getMajorVersion());
    CookieSetUtil CookieSetUtil = new CookieSetUtil();
            // 如果是谷歌并且版本大于等于67,则重赛COOKIE
            if(majorVersion>=67){
                List<String> cookieValues = CookieSetUtil.readCookieValues(request);
                cookieValues.forEach(cookieValueStr -> {
                    CookieSerializer.CookieValue cookieValue = new CookieSerializer.CookieValue(request, response, cookieValueStr);
                    CookieSetUtil.writeCookieValue(cookieValue);
                });
            }
        }catch (Exception e ){
            e.printStackTrace();
        }
    }
}
}
<dependency>
         <groupId>eu.bitwalker</groupId>
         <artifactId>UserAgentUtils</artifactId>
         <version>1.21</version>
</dependency>

Browser browser = UserAgent.parseUserAgentString(userAgent).getBrowser(); 会出现异常

在pom.xml中添加

 <plugin> 
       <artifactId>maven-assembly-plugin</artifactId> 
       <configuration> 
        <archive> 
         <manifest> 
          <mainClass>{pathToMainClass}</mainClass> 
         </manifest> 
        </archive> 
        <descriptorRefs> 
         <descriptorRef>jar-with-dependencies</descriptorRef> 
        </descriptorRefs> 
       </configuration> 
      </plugin> 

CookieSetUtil类,就是复制的DefaultCookieSerializer类,主要用到了其中的2个方法:readCookieValueswriteCookieValue,并且我们需要修改SameSite的值:

sb.append("; SameSite=").append("None");

参考 https://blog.csdn.net/u014705309/article/details/104949474

  http://cn.voidcc.com/question/p-avvtahos-uu.html

posted @ 2020-12-14 14:46  ___mouM  阅读(284)  评论(0编辑  收藏  举报