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