部分浏览器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个方法:readCookieValues
和writeCookieValue
,并且我们需要修改SameSite的值:
sb.append("; SameSite=").append("None");
参考 https://blog.csdn.net/u014705309/article/details/104949474
http://cn.voidcc.com/question/p-avvtahos-uu.html
本文作者:___mouM
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。
版权说明:本文版权归作者和博客园共有,欢迎转载。但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利.