当前位置: 首页 > news >正文

iframe下系统访问跨域问题解决办法

问题描述:iframe下嵌入web页面,访问后端接口跨域,导致接口调不通。

产生原因:iframe下,web端访问后端接口时,会优先向后端发送请求方法为OPTIONS的预检测请求,该请求调用不通,导致真实接口请求跨域无法访问。

解决办法:web端配置nginx增加隐藏iframe下跨域参数,同时针对OPTIONS方法的请求,设置请求头,其中'Access-Control-Allow-Headers'需要包含项目前后端交互使用到的鉴权字段如Authorization,Customip,允许跨域。如下所示:

(1)静态资源请求代理配置,增加黄颜色标记部分内容。

location / {
            try_files $uri $uri/ /index.html;
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Allow-Methods' 'OPTIONS, GET, PUT, POST, DELETE';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,authorization,Customip,customip,Accesstoken,accesstoken';
           
        # 处理预检请求
            if ($request_method = 'OPTIONS') {
                 return 204;
            }  
 
        }

(2)后端接口代理,,增加黄颜色标记部分内容。

location /api {
            default_type application/json;
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Allow-Methods' 'OPTIONS, GET, PUT, POST, DELETE';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,authorization,Customip,customip,Accesstoken,accesstoken';
           
        # 处理预检请求
            if ($request_method = 'OPTIONS') {
                 return 204;
            }    

           proxy_pass http://ip:port;
           proxy_set_header Host $http_host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_http_version 1.1;
           proxy_set_header Upgrade $http_upgrade;
           proxy_set_header Connection "Upgrade";
           rewrite "^/api/(.*)$" /$1 break;
        }

(3)跨域访问其他服务接口,增加黄颜色标记部分内容。

location /api/yyjc {
           default_type application/json;
           proxy_pass http://ip:port;

            add_header 'Access-Control-Allow-Methods' 'OPTIONS, GET, PUT, POST, DELETE';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,authorization,Customip,customip,Accesstoken,accesstoken';
           #add_header 'Access-Control-Allow-Credentials' 'true';

          #iframe 下跨域设置

           proxy_hide_header X-Frame-Options;
           add_header X-Frame-Options 'ALLOWALL';

           
           if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Allow-Origin' '*' always;
                add_header 'Access-Control-Allow-Methods' 'OPTIONS, GET, PUT, POST, DELETE';
                add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,authorization,Customip,customip,Accesstoken,accesstoken';
            
               return 204;
           }

           rewrite "^/api/yyjc(.*)$" $1 break;
    }

相关文章:

  • 统计图表ECharts
  • vue vite开发时保留console.log打包完后依然想保留某个文件夹下的console.log方便以后的观察
  • Linux的基础的操作指令
  • 关于ORM
  • 辛格迪客户案例 | 上海科济药业细胞治疗生产及追溯项目(CGT)
  • Qt-创建模块化.pri文件
  • Windows BilibiliHistoryFetcher-v1.3.2-v1.2.1-开源B站历史记录管理工具[支持批量管理下载]
  • 01_Flask快速入门教程介绍
  • OneClicker脚本自动运行工具
  • 白嫖腾讯的H20!!!同时部署RVC模型
  • 堡垒机和跳板机之区别(The Difference between Fortress and Springboard Aircraft)
  • 一文详解卷积神经网络中的卷积层和池化层原理 !!
  • 量化交易 - RSRS(阻力支撑相对强度)- 正确用法 - 年均收益18%
  • 进行ecovadis认证有哪些优势?百胜咨询:专业ecovadis认证辅导机构
  • Java并发:线程池
  • 车载软件架构 --- 二级boot设计说明需求规范
  • 阻塞队列-ArrayBlockingQueue
  • C++算法(11):vector作为函数参数的三种传递方式详解
  • Mininet--nodelib.py源码解析
  • Fluent 内置双向流固耦合FSI 液舱晃荡仿真计算
  • 大连万达商业管理集团提前兑付“22大连万达MTN001” ,本息2.64亿元
  • 哈萨克斯坦一名副市长遭枪击
  • 庆祝中国印尼建交75周年招待会暨万隆会议70周年纪念活动在京举行
  • 我国与沙特签署《核能发展安全与安保合作谅解备忘录》
  • “站在亚洲实现整体振兴的新起点上”——习近平主席对越南、马来西亚、柬埔寨进行国事访问纪实
  • 思南读书会500期了,这是上海最具温度的阅读文化样本