【问题标题】:Laravel CORS issue for public files公共文件的 Laravel CORS 问题
【发布时间】:2021-02-16 22:17:44
【问题描述】:

我正在尝试从 Angular 访问日志文件 URL,但它显示以下错误:

Access to XMLHttpRequest at 'https://myurl.com/storage/rmlogs/MyFileNameDetail.log' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

所有 API 请求都运行良好,错误仅针对文件请求。我的文件存储在rmlogs 目录中,该目录位于storage/app/public/rmlogs 中,并且链接到公用文件夹。

API 代码库是 Laravel,文件存储在 storage 目录中。我可以通过浏览器和 Postman 访问该文件。由于该 URL 是公开可用的,因此它不需要我为其他 API 请求传递的身份验证令牌。

我有cors.php的配置文件如下:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Cross-Origin Resource Sharing (CORS) Configuration
    |--------------------------------------------------------------------------
    |
    | Here you may configure your settings for cross-origin resource sharing
    | or "CORS". This determines what cross-origin operations may execute
    | in web browsers. You are free to adjust these settings as needed.
    |
    | To learn more: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
    |
    */

    'paths' => ['api/*'],

    'allowed_methods' => ['*'],

    'allowed_origins' => ['*'],

    'allowed_origins_patterns' => [],

    'allowed_headers' => ['*'],

    'exposed_headers' => [],

    'max_age' => 0,

    'supports_credentials' => true,

];

并且有一个Cors.php 中间件,如下所示:

<?php

namespace App\Http\Middleware;

use Closure;

class Cors
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
    return $next($request)
        ->header('Access-Control-Allow-Origin', '*')
        ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    }
}

【问题讨论】:

  • 那是一个错字。都是rmlogs。但它不起作用
  • 你需要允许 http://localhost:4200 的交叉放 'paths' =&gt; ['api/*','http://localhost:4200'],
  • 由于它访问的是文件而不是路由,我认为您可能需要在添加 CORS 标头的 rmlogs 文件夹中添加 .htaccess
  • @KamleshPaul — paths 是本地系统上要应用 CORS 标头的文件,而不是允许的来源。

标签: php laravel apache cors


【解决方案1】:

通过this blog:

有一些可用的包允许将 CORS 标头添加到从 Laravel API 发送的响应中。他们通过将中间件应用于所有 API 路由来实现这一点。但是,这样的中间件并不适用于公共目录,而公共目录通常是用户头像图像等资源的存储位置。因此,当这些资源返回到浏览器时,它们没有应用必要的 CORS 标头。

因此您需要使用其他方法应用标题。使用 HTTP 服务器的配置文件可能最容易做到这一点。

您已标记此,以便您可以设置:

Header set Access-Control-Allow-Origin "*"

在您的配置文件中。

例如:

<Location "/storage/rmlogs/">
    Header set Access-Control-Allow-Origin "*"
</Location>

您也可以将Header 指令放在a .htaccess file 中,但不建议这样做:

如果您有权访问 httpd 主服务器配置文件,则应完全避免使用 .htaccess 文件。使用 .htaccess 文件会降低 Apache http 服务器的速度。您可以在 .htaccess 文件中包含的任何指令都最好设置在 Directory 块中,因为它具有相同的效果并具有更好的性能。

如果您使用的是共享主机,那么您可能别无选择。

【讨论】:

  • 我应该在哪里添加.htaccess 文件?在rmlogs 文件夹内?
  • 如果我使用php artisan server会怎样?
  • @Quentin 我有与您提供的博客链接类似的要求。我想将图像用于画布。到目前为止,我在本地主机中,我在 .htaccess 中添加了标题,但我无法访问画布的角度图像。我能够使用图像绘制画布,但无法使用 canvas.toDataURL()。 (错误:可能无法导出受污染的画布)任何帮助表示赞赏。谢谢。
【解决方案2】:

您正在访问非标准存储目录中的文件。可能是您的请求没有通过 laravel 过滤,因为它位于存储目录中,通常允许直接访问。

在这种情况下,它由网络服务器处理。 这些是在我的安装中通过 htaccess 重写到 laravel 控制器的存储访问规则。

RewriteRule ^storage/cms/.* index.php [L,NC]
RewriteRule ^storage/logs/.* index.php [L,NC]
RewriteRule ^storage/framework/.* index.php [L,NC]
RewriteRule ^storage/temp/protected/.* index.php [L,NC]
RewriteRule ^storage/app/uploads/protected/.* index.php [L,NC]

如您所见,storage/rmlogs 不是其中的一部分,因此如果我尝试访问存储中的 rmlogs 目录,它会由网络服务器处理。

为了解决这个问题,如果你在 apache 上运行,你可以创建一个 .htaccess 规则来添加它,或者如果你运行 nginx,你可以创建一个 nginx 指令。

RewriteRule ^storage/rmlogs/.* index.php [L,NC]

【讨论】:

  • 这应该添加到public文件夹内的.htaccess文件或rmlogs
  • @HappyCoder 在您的 index.php 文件所在的最顶层目录中。
猜你喜欢
  • 2020-02-18
  • 2017-03-08
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 2014-07-18
  • 2021-11-01
  • 2020-04-01
  • 2018-11-08
相关资源
最近更新 更多