Laravel 5.5 CORS发行加载Fontawesome 4.7.0

发布于 2025-01-27 01:28:05 字数 2769 浏览 1 评论 0原文

我有问题。我已经阅读了很多参考,但仍然会出现错误。

http:///192.168.0.1/fonts/fontawesome-webfont.woff2?v=4.7.7.0'从origin'null'被CORS策略阻止原点的标题存在于请求的资源上。

我做了什么?

第一努力

我尝试在/public/font文件夹中制作新文件.htaccess。

内部.htaccess i写作:

<FilesMatch "\.(ttf|otf|eot|woff|woff2)$">
    <IfModule mod_headers.c>
        Header set Access-Control-Allow-Origin "*"
    </IfModule>
</FilesMatch>

第二次努力

我尝试使用中间件,创建新的文件call cors.php cors.php

i Witer

<?php
namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode as Middleware;
use Closure;
use Illuminate\Support\Facades\Response;
use Log;

class CORS extends Middleware {

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        Log::info("CORS 1");

        $origin = $request->header('origin');
        Log::info("Origin in CORS : $origin");
        $origin = $origin ?? '*';

        // ALLOW OPTIONS METHOD
        // $headers = [
        //     'Access-Control-Allow-Origin' => $origin,
        //     'Access-Control-Allow-Methods'=> 'GET, POST, DELETE, PUT, OPTIONS, HEAD, PATCH',
        //     'Access-Control-Allow-Headers'=> 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Set-Cookie',
        //     'Access-Control-Allow-Credentials'=> 'true'
        // ];

        $headers = [
            'Access-Control-Allow-Origin' => $origin,
            'Access-Control-Allow-Methods'=> 'GET, POST',
            'Access-Control-Allow-Headers'=> 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Set-Cookie',
            'Access-Control-Allow-Credentials'=> 'true'
        ];

        if($request->getMethod() == "OPTIONS") {
            // The client-side application can set only headers allowed in Access-Control-Allow-Headers
            return Response::make('OK', 200, $headers);
        }

        $response = $next($request);

        foreach($headers as $key => $value) {
            $response->header($key, $value);
        }

        Log::info("CORS");
        Log::info($response);
        return $response;
    }

}

结果:

第一次努力和第二次努力仍然行不通。仅第二次努力 适用于API请求(如果您在API中添加get和选项)。

我的问题:

我的目标是我想从其他Laravel服务器访问Fontawesome。 我的错是什么?

I get a problem. I already read a lot of reference but still get error.

Access to font at 'http://192.168.0.1/fonts/fontawesome-webfont.woff2?v=4.7.0' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

What i have done?

First Effort

I try to make new file .htaccess in /public/font folder.

Inside .htaccess i write :

<FilesMatch "\.(ttf|otf|eot|woff|woff2)
quot;>
    <IfModule mod_headers.c>
        Header set Access-Control-Allow-Origin "*"
    </IfModule>
</FilesMatch>

Second effort

I try to use Middleware, create new file call CORS.php

Inside CORS.php i write

<?php
namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode as Middleware;
use Closure;
use Illuminate\Support\Facades\Response;
use Log;

class CORS extends Middleware {

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        Log::info("CORS 1");

        $origin = $request->header('origin');
        Log::info("Origin in CORS : $origin");
        $origin = $origin ?? '*';

        // ALLOW OPTIONS METHOD
        // $headers = [
        //     'Access-Control-Allow-Origin' => $origin,
        //     'Access-Control-Allow-Methods'=> 'GET, POST, DELETE, PUT, OPTIONS, HEAD, PATCH',
        //     'Access-Control-Allow-Headers'=> 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Set-Cookie',
        //     'Access-Control-Allow-Credentials'=> 'true'
        // ];

        $headers = [
            'Access-Control-Allow-Origin' => $origin,
            'Access-Control-Allow-Methods'=> 'GET, POST',
            'Access-Control-Allow-Headers'=> 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Set-Cookie',
            'Access-Control-Allow-Credentials'=> 'true'
        ];

        if($request->getMethod() == "OPTIONS") {
            // The client-side application can set only headers allowed in Access-Control-Allow-Headers
            return Response::make('OK', 200, $headers);
        }

        $response = $next($request);

        foreach($headers as $key => $value) {
            $response->header($key, $value);
        }

        Log::info("CORS");
        Log::info($response);
        return $response;
    }

}

Result :

First effort and Second Effort still not works. Second effort only
works for API request (if you add GET and OPTIONS in API).

My Question :

My goal is i want to access fontawesome from other Laravel Server.
What's my fault?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文