当前位置: 首页 > 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;
    }

http://www.xdnf.cn/news/66943.html

相关文章:

  • VTK知识学习(53)- 交互与Widget(四)
  • Unity3D ILRuntime与Scripting Backend整合指南
  • 剪映学习02
  • Kotlin协程学习笔记
  • OpenCV---图像预处理(四)
  • HCIE Datacom备考技巧
  • typedef MVS_API CLISTDEF0IDX(ViewScore, IIndex) ViewScoreArr;
  • 《解锁增强型上下文学习,打造你的专属智能助手》
  • 每天学一个 Linux 命令(29):tail
  • gnome中删除application中失效的图标
  • 齐次坐标系下的变换矩阵
  • 【图论 DFS BFS】P10725 [GESP202406 八级] 最远点对|普及+
  • LangChain实现PDF中图表文本多模态数据向量化及RAG应用实战指南
  • LeetCode算法题(Go语言实现)_54
  • ubuntu--汉字、中文输入
  • iso文件在麒麟V10系统上安装达梦数据库
  • 基础服务系列-Jupyter Notebook 支持JavaScript
  • 【技术派后端篇】基于 Redis 实现网站 PV/UV 数据统计
  • 前端笔记-Vue3(上)
  • Spark-SQL 四(实验)
  • 显卡及相关大模型部署需求概述
  • 靠华为脱胎换骨,但赛力斯仍需要Plan B
  • 【Linux网络编程十】网络原理之IP协议【网络层】
  • 悬空引用和之道、之禅-《分析模式》漫谈57
  • SystemWeaver详解:从入门到精通的深度实战指南
  • css3新特性第五章(web字体)
  • 极狐GitLab Git LFS 速率限制如何设置?
  • mysql的binlog,redolog,undolog的区别
  • 安卓垂直进度条
  • 学习深度学习是否要先学习机器学习?工程师的路径选择策略