Laravel 5.2.31通过HttpRequester验证VerifyCsrfToken.php中的TokenMismatchException

时间:2017-04-05 17:55:37

标签: php laravel api laravel-5.2

我在“飞行”中使用了这种方法。控制器:

    /**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $arrivalDateTime = $request -> input('arrival.datetime');

    return $arrivalDateTime;
}

和路线:

Route::resource('api/v1/flights', v1\FlightController::class, ['except' => ['create', 'edit']]);

我尝试使用HttpRequester向此地址发出POST请求:

http://localhost:8000/api/v1/flights

有了这个内容:

{
"flightNumber":"JWM12345",
"status":"ontime",
"arrival": {
    "datetime":"2016-04-10 22:34:01",
    "iataCode":"A57"
},
"departure": {
    "datetime":"2016-04-10 21:34:01",
    "iataCode":"9C1"
}}

我将内容类型设置为' application / json',然后我提交请求并收到此错误:

VerifyCsrfToken.php第67行中的TokenMismatchException:

in VerifyCsrfToken.php line 67
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 726
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54
at require_once('/home/dendi/Documents/GitProjects/airview/public/index.php') in server.php line 21

我想让我的请求成功?

4 个答案:

答案 0 :(得分:1)

非常简单的修复 - 只需添加以下行:

{{ csrf_field() }}

在表单内。它将创建一个用于CSRF保护的隐藏字段,错误将消失。

答案 1 :(得分:1)

POST,PUT,DELETE方法中需要

_token。如果您默认使用Web中间件。

要获取令牌,您可以创建一个使用csrf_token()函数获取令牌并返回令牌的路由。 向该路线发出请求以获取与n2s68OPSzaMVYyiFvvDhlRwvFF55zDwKaQPjX8AS类似的令牌,并将其放入请求正文中,如

 {
   "_token":"n2s68OPSzaMVYyiFvvDhlRwvFF55zDwKaQPjX8AS",
   ...
 } 

并提出请求。

OR

如果您不想在某些路由上使用csrf vaildation,请在我使用此命名空间的VerifyCsrfToken

命名空间中创建一个新的App\Http\Middleware

现在在$except数组中添加您不想通过csrf标记验证的路由。

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;

class VerifyCsrfToken extends BaseVerifier {

     protected $except = [
        'api/v1/flights'
    ];

 }

现在在kernel.php

中替换它
protected $middleware = [
    'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
    'Illuminate\Cookie\Middleware\EncryptCookies',
    'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
    'Illuminate\Session\Middleware\StartSession',
    'Illuminate\View\Middleware\ShareErrorsFromSession',
    'Illuminate\Foundation\Http\Middleware\VerifyCsrfToken', // <-- this
 ]

用这个

protected $middleware = [
    'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
    'Illuminate\Cookie\Middleware\EncryptCookies',
    'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
    'Illuminate\Session\Middleware\StartSession',
    'Illuminate\View\Middleware\ShareErrorsFromSession',
    'App\Http\Middleware\VerifyCsrfToken', // <-- this
 ]

希望这有助于:)

答案 2 :(得分:0)

我认为你应该升级到Laravel 5.4,在5.4中路由文件夹中有一个名为api.php的不同文件,你可以编写你的api路由,可以通过“api / ruote_name”访问。

干杯。

答案 3 :(得分:0)

关于什么是CSRF令牌,我认为你应该阅读this excellent answer。 Laravel使用名为VerifyCsrfToken的中间件验证这些请求。

如果要在Laravel 5.2中禁用此功能,请打开App/Http/Kernel.php

protected $middleware = [
        'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
        'Illuminate\Cookie\Middleware\EncryptCookies',
        'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
        'Illuminate\Session\Middleware\StartSession',
        'Illuminate\View\Middleware\ShareErrorsFromSession',
        'Illuminate\Foundation\Http\Middleware\VerifyCsrfToken',
]

并从中间件阵列中删除'Illuminate\Foundation\Http\Middleware\VerifyCsrfToken'