VerifyCsrfToken始终在路由到API中间件Laravel 5.2.35时调用 [英] VerifyCsrfToken always called when route to API Middleware Laravel 5.2.35

查看:389
本文介绍了VerifyCsrfToken始终在路由到API中间件Laravel 5.2.35时调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类似的Laravel项目。这是kernel.php的一部分代码。这两个项目都有相同的代码。

  protected $ middlewareGroups = [
'web'=> [
\App\Http\Middleware\EncryptCookies :: class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse :: class,
\Illuminate\ Session \Middleware\StartSession :: class,
\Illuminate\View\Middleware\ShareErrorsFromSession :: class,
\App\Http\Middleware\VerifyCsrfToken :: class ,
],
'api'=> [
'throttle:60,1',
],
];

但是,VerifyCsrfToken总是被调用,尽管我把路由放入 api middlewareGroup。



我在Advanced REST Client中检查请求标题。我找到了这个。

第一个项目结果:




第二个项目结果:




第一个结果在请求标题中有cookie属性,但第二个结果没有

解决方案

routes.php 都包含在一个路由组中应用了网络中间件。您应该创建另一个路由文件,并让RouteServiceProvider将这些文件加载​​到一个组中并使用'api',并且不应用'web'中间件。

如果打开RouteServiceProvider你会看到这发生了什么。检查 map 方法以查看调用 mapWebRoutes


I have two similiar Laravel project. This is part code of kernel.php. Both projects have same code.

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
    ],
    'api' => [
        'throttle:60,1',
    ],
];

But, VerifyCsrfToken always be called although I put my route inside api middlewareGroup.

I check request header in Advanced REST Client. I found this.

First project result :

Second project result :

First result has cookie attribute in request header, but second result doesn't have

解决方案

All the routes in routes.php are included in a route group which has the 'web' middleware applied. You should probably create another routes file and have the RouteServiceProvider load those in a group with 'api' and without the 'web' middleware applied.

If you open up your RouteServiceProvider you will see where this is happening. Check the map method to see it calling mapWebRoutes.

这篇关于VerifyCsrfToken始终在路由到API中间件Laravel 5.2.35时调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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