为混合移动 Web 应用程序设置 CORS 策略

Setting up CORS policy for hybrid mobile web apps

我正在使用 Cordova、Ionic 和 Angular 框架开发混合移动应用程序。使用 ASP.NET API 作为我的后端。为了能够从移动应用程序访问我的网站 API,我需要设置 CORS 策略以允许所有传入请求 (*)。当我的应用程序投入生产时,我不确定这是否是一种好的做法和安全的做法?

我搜索过这个主题,但在 Whosebug 或任何其他网站上没有找到有用的内容。

无需将策略设置为通配符。您可以根据 Webview 版本/Capacitor 或 Cordova 添加以下规则:

  • 电容:

    .----------.-----------------------.
    | Platform |        Origin         |
    :----------+-----------------------:
    | iOS      | capacitor://localhost |
    :----------+-----------------------:
    | Android  | http://localhost      |
    '----------'-----------------------'
    
    Replace localhost with your own hostname if you have changed the default in the Capacitor config.
    
  • Cordova 上的 Ionic WebView 3.x 插件

    .----------.-------------------.
    | Platform |      Origin       |
    :----------+-------------------:
    | iOS      | ionic://localhost |
    :----------+-------------------:
    | Android  | http://localhost  |
    '----------'-------------------'
    
    Replace localhost with your own hostname if you have changed the default in the plugin config.
    
  • Cordova 上的 Ionic WebView 2.x 插件

    .----------.-----------------------.
    | Platform |        Origin         |
    :----------+-----------------------:
    | iOS      | http://localhost:8080 |
    :----------+-----------------------:
    | Android  | http://localhost:8080 |
    '----------'-----------------------'
    
    
    Replace port 8080 with your own if you have changed the default in the plugin config.
    
  • 浏览器本地开发

    .----------.------------------------------------------------------.
    | Platform |                        Origin                        |
    :----------+------------------------------------------------------:
    | iOS      | http://localhost:8100 or http://YOUR_MACHINE_IP:8100 |
    :----------+------------------------------------------------------:
    | Android  | http://localhost:4200                                |
    '----------'------------------------------------------------------'
    
    Port numbers can be higher if you are serving multiple apps at the same time. 
    

您可以找到更多信息 here