chrome v94谷歌浏览器开始禁止了https页面获取http资源的解决办法 CORS跨域限制policy: The request client is not a secure context 看全部

chrome v94谷歌浏览器开始禁止了https页面获取http资源的解决办法 CORS跨域限制policy: The request client is not a secure context

跳错
policy: The request client is not a secure context and the resource is in more-private address space `local`.

普通用户解决方案,浏览器改设置选Disabled
chrome://flags/#block-insecure-private-network-requests
Block insecure private network requests.
Prevents non-secure contexts from making sub-resource requests to more-private IP addresses. An IP address IP1 is more private than IP2 if 1) IP1 is localhost and IP2 is not, or 2) IP1 is private and IP2 is public. This is a first step towards full enforcement of CORS-RFC1918: https://wicg.github.io/cors-rfc1918 – Mac, Windows, Linux, Chrome OS, Android

服务器网站管理员解决方案,增加headers响应头,标红重点
Access-Control-Allow-Origin: *
Access-Control-Allow-Private-Network: true

经过测试,增加headers响应头无法解决,纯属老外瞎掰。。不过改浏览器设置确实是可以

参考:https://stackoverflow.com/questions/66534759/chrome-cors-error-on-request-to-localhost-dev-server-from-remote-site


补充
今天找了下资料,看到了谷歌官方文档,,,说这个限制是为了安全性不允许加载127.0.0.1和localhost的资源,垃圾谷歌说远程站点请求 local 资源是不安全的,所以要禁止这种行为。https://developer.chrome.com/blog/private-network-access-update/
chrome94-101版本,也就是说102版本开始,就彻底不允许访问来自本地资源了,然后就导致了一个最大的问题,v2上一些网站的时候,此时会变成127.0.0.1的ip,首次访问倒是没有问题,但是二次访问从浏览器加载磁盘高速缓存时候,然后就会强制触发到这个跨域问题。
所以,,,用上面的Block insecure private network requests 设置,才能解决这个问题。还有一种办法就是,v2上检查远程站点和img等图片信息是否全部都用上了127.0.0.1,如果是这样全用的话也没有问题,不要站点不用v2,只在图片用v2。

chrome官方也给了临时解决办法,101及版本之前有效:Private Network Access update: Introducing a deprecation trial
服务器网站管理员方法,升级为https来获得安全性,但是咱们要用的是127.0.0.1,自签名证书肯定是不受信任的,所以还要通知用户去手动允许信任就可以访问本地资源内容了,让用户来操作不太现实所以本地还是需要使用http协议来实现。
为了解决这个兼容,所以出了个deprecation trial, 可以一直用到 2022-05-17:Register for the deprecation trial
去develop后台,申请一个token,然后把这个token加入Origin-Trial响应头部中,例如Origin-Trial: xxxxxxxx 注意这个不是127.0.0.1的server,是远程站点服务器的server
申请地址:https://developer.chrome.com/origintrials/#/trials/active 选择 Private Network Access from non-secure contexts 这个申请

参考:https://developer.chrome.com/blog/private-network-access-update/#policies
https://chromeenterprise.google/policies/#InsecurePrivateNetworkRequestsAllowed
https://chromeenterprise.google/policies/#InsecurePrivateNetworkRequestsAllowedForUrls