php跨域和https访问http问题分析

1、https的web访问http地址的资源

问题:请求状态status=(canceled)   

解决办法:把资源地址改成https的

/**
 * HTTP地址改成HTTPS地址
 * @param $url
 */
function changeHttp2Https(&$url){
    if(stripos($url, 'http://') !== false){
        $url = str_replace('http://','https://',$url);
    }
}

2、https的web访问http地址的资源

问题:请求状态status=(blocked:mixed-content)

mixed-contend解释是:出现这个问题的原因是因为 在https网站中发起的http请求被禁止。也就是在https的网站中引入了 http 的图片、css、javascript 等其他资源或文件,浏览器便会提示 “Mixed Content” 错误,这是因为http 的资源容易被恶意攻击者利用,可能会导致安全问题,浏览器认为网页不是完全安全的。

解决办法一:升级http资源为https。

解决办法二:请求当前web服务地址,再通过web服务器调用目标http资源,web服务器做中转,这样服务调服务就不会存在安全问题。

3、设置服务器允许跨域

问题1:

​Access to XMLHttpRequest at 'http://saas-purchase.com/api/demand/search' 
from origin 'http://192.168.1.1:8899' has been blocked by CORS policy: 
Response to preflight request doesn't pass access control check: 
The 'Access-Control-Allow-Origin' header contains multiple values '*, *',
but only one is allowed. 

解决办法:( '*, *' 表示设置了两个跨域请求头)检查是否nginx和php代码重复设置了响应头,删除一个即可。

问题2:  

​Access to XMLHttpRequest at 'http://saas-purchase.com/api/demand/search' 
from origin 'http://192.168.1.1:8899'has been blocked by CORS policy: 
Response to preflight request doesn't pass access control check: 
No 'Access-Control-Allow-Origin' header is present on the requested resource.

解决办法:在nginx或代码中设置跨域响应头。

在代码中添加跨域响应头的办法:
(1) 在 项目入口中(index.php)文件设置添加以下三行:
header('Access-Control-Allow-Origin:*');//设置允许域名
header('Access-Control-Allow-Methods:POST,GET,PUT,DELETE,OPTIONS');//允许请求方法 
// 或者 header('Access-Control-Allow-Methods:*');
header('Access-Control-Allow-Headers:authorization,token,content-type,Authorization');//允许请求头名
// 或者 header('Access-Control-Allow-Headers:*');


(2) 添加成功之后,访问接口会返回Response Headers:
Access-Control-Allow-Headers: *
Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS
Access-Control-Allow-Origin: *

4、OPTIONS请求

HTTP 的 OPTIONS 方法 用于获取目的资源所支持的通信选项。POST请求的时候会自动发送一个OPTIONS的请求,服务器要支持这个请求,并响应status=200

<?php
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:*');
header('Access-Control-Allow-Headers:*');

$queryType = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '';
if ( false !== stripos($queryType, 'OPTIONS')) {
    echo 200;
    exit;
}

5、nginx代理

如果http服务器无法改成https,或者依旧存在http status=canceled问题,考虑使用nginx代理。

在php代码中把返回的资源地址中的域名替换成当前服务器的域名

/**
 * 替换JAVA地址为本地地址
 * @param $string
 */
function changeJava2Proxy(&$string){
    $finds = [
        'http://xxx.com/file/file/download/image',
        'https://xxx.com/file/file/download/image'
    ];
    $replace = [
        'http://localhost.com/file/file/download/image',
        'http://localhost.com/file/file/download/image'
    ];
    // json格式字符串需要把 / 改成\/
    foreach ($finds as &$find){
        $find = addcslashes($find,'/');
    }
    foreach ($replace as &$find){
        $find = addcslashes($find,'/');
    }

    $string = str_replace($finds, $replace, $string);

}

目标地址
    http://xxx.com/file/file/download/image?filePath=group1/M06/00/B4/rBAylWYg11KAF3sxAA5-gORui7U802.pdf
替换后的地址
    http://aaa.com/file/file/download/image?filePath=group1/M06/00/B4/rBAylWYg11KAF3sxAA5-gORui7U802.pdf

在nginx主机server中添加代理nginx添加代理(生产环境)

​location ~ ^/(file/file/download/image) {
   proxy_pass https://xxx.com;
}

匹配到 /file/file/download/image 开头的地址,会自动跳转到 http://xxx.com 进行处理。

多个匹配规则使用

​location ~ ^/(file/file/download/image|file/file/download/pdf) {
   proxy_pass https://xxx.com;
}

匹配到 /file/file/download/image 和 /file/file/download/pdf 开头的地址。

  • 10
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值