Chrome浏览器升级80以后导致重定向自动登录失效问题记录和解决方案

问题

Chrome 浏览器升级到80版,针对sameSite设置更加严格,默认不允许设置第三方Cookie

查阅资料都是讲述为什么Chrome采取更加严格的cookie设置策略,没有详细的解决方案

我的情况说明,希望对你们有参考价值

环境:因为本站被嵌入到其他产品中使用,需要在iframe中实现自动登录,相当于我们站点是第三方在人家的产品中设置cookie来实现自动登录

 

参考资料

sameSite 介绍: https://www.cnblogs.com/gxp69/p/12565927.html

浏览器设置解决:https://www.cnblogs.com/tianma3798/p/13517449.html

后端Tomcat设置解决:https://stackoverflow.com/questions/57505939/how-to-set-samesite-cookie-in-tomcats-cookie-processor

方案:

肯定不可以在浏览器端设置,容易造成安全漏洞,因此只能通过后端来进行适配解决

因为samesite 设置为none, 必须是secure,也就是采用https协议

在Nginx中对本站配置https配置,配置完成后,发现中间有一环是sendRedirect重定向,请求协议从https降级到http,又需要解决这个问题

解决方案

1.Nginx 的location 中新增

proxy_set_header X-Forwarded-Proto $scheme;

2.Tomcat中的server.xml中新增

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

...
         <Valve className="org.apache.catalina.valves.RemoteIpValve" 
      protocolHeader="X-Forwarded-Proto" />
....
      </Host>

3.代码中设置获取https协议

/**
	 * 返回应用的根URL,包含结尾的<code>/</code>。兼容存在Apache
	 * mod_proxy反向代理的情况:首先判断是否有x-forwarded-host头,如有则从中得出原始请求的HOST头,再计算.
	 * (详见http://httpd.apache.org/docs/2.2/mod/mod_proxy.html) <b>例子</b>: if request
	 * "http://localhost:8080/app1/dir1/page1.jsp", the method return "http://localhost:8080/app1".
	 * 增加X-Forwarded-Proto头判断,如果有,则从请求中获取最原始的协议,可支持https反向代理至http.
	 * 
	 */
	public static String getContextUrl(HttpServletRequest request) {
		String xForwardedHost = request.getHeader("X-Forwarded-Host");
		String orginProtocol = request.getHeader("X-Forwarded-Proto");
		String host = "";
		if (!StringUtil.isEmpty(xForwardedHost)) {
			host = StringHelper.split(xForwardedHost, ",")[0].trim();
		} else {
			host = request.getHeader("Host");
		}
		String protocol = "";
		if (StringUtil.isNotEmpty(orginProtocol)) {
			if (("https").equals(orginProtocol.toLowerCase())) {
				protocol = "https";
			} else {
				protocol = "http";
			}

		} else {
			protocol = request.getScheme();
		}
		return protocol + "://" + host + getContextPathWithSlash(request);
	}

协议问题解决,需要解决第三方Cookie的问题

我用的Tomcat 9.0.38 版本

在Tomcat的conf/context.xml文件中新增

 <CookieProcessor className="org.apache.tomcat.util.http.LegacyCookieProcessor" sameSiteCookies="None"/

 

还有一个小问题,就是http 请求到https的时候referer路径不全

参考资料

https://www.cnblogs.com/amyzhu/p/9716493.html

设置

在html页面的<head>标签中新增

<meta name="referrer" content="no-referrer-when-downgrade">

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值