バグ レコード - パブリック ネットワークからプライベート ネットワークへのクロスドメインの問題を記録します。

1. エラー報告

エラーは次のとおりです: Access to XMLHttpRequest at 'http://LAN address' from origin 'http://public address on the cloud' has been blocked by CORS policy: The request client is not a secure context and the resource is not a secure context and the resource is not a secure context and the resource is not a secure context and the resource is not a secure context and the resource isよりプライベートなアドレス空間「private」で。

2. ドメイン間で生成されるシナリオ

このクロスドメインは、プロジェクトが LAN を介して別のサーバー上のリソースにアクセスするために、クラウド上に配置された Web リソースをロードするために electron を使用する必要があるためです. ここでのクライアントは Electron シェル ブラウザを指し、すべてのページはクラウド上にロードされます 別の Webプロジェクトを作成すると、クラウド上のプロジェクトはクラウド上のサービスのみをロードでき、LAN を介して他のサーバーにアクセスすることはできません.アクセスされた場合、上記のエラーが報告されます.情報を確認したところ、Chrome であると言われています.ブラウジング バージョン 94 以降では、安全でないプライベート ネットワーク要求をブロックする非推奨テストが導入されました。つまり、ターゲット サイトの IP アドレスが要求の開始者の IP アドレスよりもプライベートである場合、ブロックされます。

プライベート ネットワーク アクセス

6bc6e40e40ce466991a1fdd6608c2e21.png

3. 解決策

        ここで使われているのは、パブリック ネットワークからプライベート ネットワークへのリクエストを、プライベート ネットワークからプライベート ネットワークへのリクエストに変換することです.最終的に、LAN 内の複数の電子クライアントが LAN を介して相互接続され、Web ページがパラメータをイベントを通じてクライアント B に送信し、クライアント B がクライアント A のサービスにリクエストを送信して、このブラウザ セキュリティ ポリシーを回避します。

// electron---ipcMain
ipcMain.handle("proxy-http", async (event, args) => {
      let res
      if (args.method == 'get') {
        try {
          res = await get(args.url, args.data)
          return res
        } catch (error) {
          new Error(error)
        }
      } else if (args.method == 'post') {
        try {
          res = await post(args.url, args.data)
          return res
        } catch (error) {
          return new Error(error)
        }

      }
    })

// api.js
export const barService = {
    //呼叫
    call: (data) => {
        return ipcRenderer.invoke("proxy-http", {
            method: "post",
            url: "/barService/call",
            data
        })
    }
}

// call.vue
let params = {
   hostName: os.hostname(),
   status: 0,
}
let { code } = await barService.call(params) //code ==200



おすすめ

転載: blog.csdn.net/qq_52965813/article/details/126218379
おすすめ