跨域问题,提示以下报错

Blade 未结 2 67
69partner
69partner 剑圣 2024-10-13 16:04

我们前端使用微应用的方式,不能匹配所有域名只能使用指定域名,此时用Nginx去做的add header,但是报了以下错误,我应该如何处理呢


has been blocked by CORS policy: Request header field blade-auth is not allowed by Access-Control-Allow-Headers in preflight response.

2条回答
  • 2024-10-13 18:21

    检查一下allow-headers的配置,bladex默认需要这些请求头:

    X-Requested-With, Tenant-Id, Blade-Auth, Content-Type, Authorization, credential, X-XSRF-TOKEN, token, username, client, knfie4j-gateway-request, knife4j-gateway-code, request-origion

    nginx配置参考这个

    server {
        listen 80;  # 或者使用的其他端口
        server_name your-domain.com;  # 替换为域名
    
        location / {
            # 添加 CORS 相关的头信息
            add_header 'Access-Control-Allow-Origin' 'https://allowed-domain.com';  # 替换为允许的域名
            add_header 'Access-Control-Allow-Methods' 'GET,POST,PUT,DELETE,OPTIONS,HEAD';  # 允许的方法
            add_header 'Access-Control-Allow-Headers' 'Content-Type, blade-auth';  # 允许的请求头
    
            # 处理预检请求
            if ($request_method = OPTIONS) {
                add_header 'Access-Control-Allow-Origin' 'https://allowed-domain.com';  # 替换为允许的域名
                add_header 'Access-Control-Allow-Methods' 'GET,POST,PUT,DELETE,OPTIONS,HEAD';
                add_header 'Access-Control-Allow-Headers' 'X-Requested-With, Tenant-Id, Blade-Auth, Content-Type, Authorization, credential, X-XSRF-TOKEN, token, username, client, knfie4j-gateway-request, knife4j-gateway-code, request-origion';
                add_header 'Content-Length' 0;
                return 204;  # 返回204 No Content
            }
    
            # 其他配置...
            proxy_pass http://your_backend;  # 替换为后端服务
        }
    }


    2 讨论(0)
  • 2024-10-17 19:54

    大佬的解答一级棒,二级棒,三级棒,无穷棒。

    0 讨论(0)
提交回复