barryvdh / laravel-cors不适合我的路线 [英] barryvdh/laravel-cors not working for my routes

查看:2005
本文介绍了barryvdh / laravel-cors不适合我的路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用[这个laravel-cors包] [1],我已经阅读文档,我已经添加了服务提供程序到 config / app.php

I'm using [this laravel-cors package][1], I've read the docs and I've added the service provider to config/app.php.

将中间件添加到 kernel.php 之后:

  protected $middleware = [
  \Barryvdh\Cors\HandleCors::class,
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,

];

它适用于我的护照路线,但不适用于我自己的路线。

It works for my passport routes, but not for my own routes.

护照路线

Route::group([
    'middleware' => ['api']
], function ($router) {
    Passport::routes();
    Passport::tokensExpireIn(Carbon::now()->addDays(15));
    Passport::refreshTokensExpireIn(Carbon::now()->addDays(30));
});

我的路线

Route::group(['middleware' => ['auth:api']], function () {
    Route::resource('answers', 'AnswersController');
});


推荐答案

我已经读过CORS中间件的整个代码。

I've read the entire code of the CORS middleware. It will only add headers, as per the RFC, when:


  1. 请求是预飞行请求(OPTIONS)

  2. 请求有一个 Origin 头,而不是您的API所在的头。

  3. 远程主机正在尝试根据CORS配置加入您的API。

  1. The request is a Pre-Flight request (OPTIONS)
  2. The request has an Origin header other than the one your API resides on.
  3. The remote host that is trying to join your API is allowed as per the CORS config.

确保您发布CORS配置并根据您的需要进行编辑。

Make sure you publish the CORS config and edit it to your needs.

查看来源

这篇关于barryvdh / laravel-cors不适合我的路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆