【问题标题】:Laravel multiple domain origin CORSLaravel 多域起源 CORS
【发布时间】:2023-03-08 11:50:02
【问题描述】:

我想在我的 laravel 中允许 CORS 的两个域能够在本地和服务器上使用它,因此我不想将我的应用程序暴露给任何域。这就是我现在所拥有的

public function handle($request, Closure $next)
    {
        return $next($request)
            ->header('Access-Control-Allow-Origin', 'http://localhost:4200')
//            ->header('Access-Control-Allow-Origin', 'http://api.example.com')
            ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE')
            ->header('Access-Control-Allow-Headers', 'Content-Type');
    }

我既不能像我评论的那样做,也不能像数组那样做

【问题讨论】:

    标签: php laravel cors dingo-api


    【解决方案1】:

    您可以只检查您所在的主机,然后为该主机发送匹配的Access-Control-Allow-Origin

    $request->getHttpHost() 将为您提供请求中使用的主机名 - 如果您只需要基于此进行区分,我们可能可以忽略这里也是源(协议、端口)一部分的其他内容,并简单地使这个像

    public function handle($request, Closure $next)
        {
            $origin = $request->getHttpHost() == 'localhost' ?
                        'http://localhost:4200' : 'http://api.example.com';
    
            return $next($request)
                ->header('Access-Control-Allow-Origin', $origin)
                ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE')
                ->header('Access-Control-Allow-Headers', 'Content-Type');
        }
    

    当然,如果您需要更多可能的来源(例如将主机名与可能的值数组匹配,必要时考虑协议和端口),您当然可以使其更加“复杂”,但如果您只是暂时需要这两个,基本就可以了。

    【讨论】:

      【解决方案2】:

      您可以定义一个您想要允许的来源数组,然后检查传入的请求是否是其中之一:

      public function handle($request, Closure $next)
      {
          $allowedOrigins = ['example.com', 'example1.com', 'example2.com'];
          $origin = $_SERVER['HTTP_ORIGIN'];
      
          if (in_array($origin, $allowedOrigins)) {
              return $next($request)
                  ->header('Access-Control-Allow-Origin', $origin)
                  ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE')
                  ->header('Access-Control-Allow-Headers', 'Content-Type');
          }
      
          return $next($request);
      }
      

      【讨论】:

      • 这行得通,但是当我在浏览器中访问我的路线时,我的 Dingo Api 坏了。它说 {"message":"Undefined index: HTTP_ORIGIN","status_code":500}
      • @Sergey,奇怪,也许你可以试试$request->header('host')$_SERVER['HTTP_REFERER'] 而不是$_SERVER['HTTP_ORIGIN']
      • @thefallen 我需要创建一个新的中间件来添加这段代码吗?
      • @skm 是的,那将是更好的方法。如果你的所有路由上都有 web 中间件,你也可以让它成为 EncryptCookiesVerifyCsrfToken 类的一部分,但我会推荐一个新的中间件。
      • 这个方案在哪里实现,文件路径??
      【解决方案3】:

      @thefallen 的答案对我有用,我也遇到了 @sergey 的同样问题,我是这样解决的。

      public function handle($request, Closure $next)
      {
      
        $allowedOrigins = [env('FRONTEND_ENDPOINT', 'http://localhost:8080'), env('WORDPRESS_ENDPOINT', 'http://localhost'), env('EXTRA_ENDPOINT', 'http://127.0.0.1')];
      
        if($request->server('HTTP_ORIGIN')){
          if (in_array($request->server('HTTP_ORIGIN'), $allowedOrigins)) {
              return $next($request)
                  ->header('Access-Control-Allow-Origin', $request->server('HTTP_ORIGIN'))
                  ->header('Access-Control-Allow-Origin', '*')
                  ->header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE')
                  ->header('Access-Control-Allow-Headers', '*');
          }
        }
      
      
        return $next($request);
      
      }
      

      这样你也可以像这样在 .env 文件中设置变量。

      FRONTEND_ENDPOINT=http://localhost:8080
      WORDPRESS_ENDPOINT=http://localhost
      EXTRA_ENDPOINT=http://127.0.0.1:8080
      

      【讨论】:

      • 嘿,伙计,感谢您的回答,它帮助了我。但我注意到你这里有重复的行。是不是弄错了? ->header('Access-Control-Allow-Origin', $request->server('HTTP_ORIGIN')) ->header('Access-Control-Allow-Origin', '*')
      【解决方案4】:

      您可以添加到 TrustHosts.php 中间件而无需执行任何额外操作。

      // app/Http/Middleware/TrustHosts.php
      public function hosts()
      {
          return [
              $this->allSubdomainsOfApplicationUrl(),
              'https://www.nexgi.com'
          ];
      }
      

      以上代码在 laravel 8.x 中运行良好,如果您正在运行 queue 或 supervisorctl,则重新启动以反映更改。

      【讨论】:

        猜你喜欢
        • 2021-10-23
        • 1970-01-01
        • 2014-12-21
        • 2015-05-16
        • 2014-08-05
        • 2013-01-12
        • 2021-01-01
        • 2018-08-10
        • 2018-05-04
        相关资源
        最近更新 更多