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


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

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

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

    1. /**
    2. * HTTP地址改成HTTPS地址
    3. * @param $url
    4. */
    5. function changeHttp2Https(&$url){
    6. if(stripos($url, 'http://') !== false){
    7. $url = str_replace('http://','https://',$url);
    8. }
    9. }

    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:

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

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

    问题2:  

    1. ​Access to XMLHttpRequest at 'http://saas-purchase.com/api/demand/search'
    2. from origin 'http://192.168.1.1:8899'has been blocked by CORS policy:
    3. Response to preflight request doesn't pass access control check:
    4. 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

    1. header('Access-Control-Allow-Origin:*');
    2. header('Access-Control-Allow-Methods:*');
    3. header('Access-Control-Allow-Headers:*');
    4. $queryType = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '';
    5. if ( false !== stripos($queryType, 'OPTIONS')) {
    6. echo 200;
    7. exit;
    8. }

    5、nginx代理

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

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

    1. /**
    2. * 替换JAVA地址为本地地址
    3. * @param $string
    4. */
    5. function changeJava2Proxy(&$string){
    6. $finds = [
    7. 'http://xxx.com/file/file/download/image',
    8. 'https://xxx.com/file/file/download/image'
    9. ];
    10. $replace = [
    11. 'http://localhost.com/file/file/download/image',
    12. 'http://localhost.com/file/file/download/image'
    13. ];
    14. // json格式字符串需要把 / 改成\/
    15. foreach ($finds as &$find){
    16. $find = addcslashes($find,'/');
    17. }
    18. foreach ($replace as &$find){
    19. $find = addcslashes($find,'/');
    20. }
    21. $string = str_replace($finds, $replace, $string);
    22. }

    目标地址
        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 开头的地址。

  • 相关阅读:
    输入M*N阶矩阵A和B,用函数编程计算并输出A与B之和
    TensorFlow入门(十八、激活函数)
    Yolov8小目标检测(8):微小目标检测的上下文增强和特征细化网络ContextAggregation,助力小目标检测
    Mediacodec 编码过程源码解析
    七夕节快到了,程序员独有的表白网页神器了解一下(HTML+CSS+JS)
    使用QMetaObject::invokeMethod来发射信号
    Postgres16版本中FROM子查询别名可以省略不写了
    RabbitMQ高级篇,进阶内容
    Python tests in.....
    Elasticsearch07:ES中文分词插件(es-ik)安装部署
  • 原文地址:https://blog.csdn.net/qq_16149125/article/details/137928780