Laravel 5.2.31中API的TokenMismatchException [英] TokenMismatchException for API in Laravel 5.2.31

查看:72
本文介绍了Laravel 5.2.31中API的TokenMismatchException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试什么?

我已经有一个网站,并且正在尝试使用相同代码对API进行基于令牌的身份验证,下面是示例身份验证代码的开始

I already have a website and I am trying Token based authentication for an API in same code and below is the start for sample authentication code

我在下面创建的控制器是代码.

class AccountController extends \App\Http\Controllers\Controller
{
    public function apilogin($UserData) {
        return json_decode($UserData);
    }
}

我的路线配置在下面.

Route::group(['prefix' => 'api/v1', 'middleware' => 'auth.api'], function () {
    Route::post('/apilogin', 'API\User\Account\AccountController@apilogin');
});

**然后从Postman Chrome扩展程序中,我发布了请求,并且如果我在Kernel.php中的$ middlewareGroups中注释了以下行,则工作正常

**Then from the Postman Chrome Extension, I have posted the request and worked fine if I comment the following line from $middlewareGroups in Kernel.php

\App\Http\Middleware\VerifyCsrfToken::class,

如果我从POSTMan Extension获得GET请求,我没有问题VerifyCsrfToken

I have no issues VerifyCsrfToken if I do GET request from POSTMan Extension

推荐答案

打开您的app\http\Middleware\VerifyCsrfToken.php文件.

在此处使用以下内容编辑$except属性:

Here edit $except property with:

protected $except = [
  'api/*' 
];

这将从CSRF验证中排除您的api路由.

This will exclude your api routes from CSRF verification.

这篇关于Laravel 5.2.31中API的TokenMismatchException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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