2016-06-14 102 views
0

嘗試登錄時在VerifyCsrfToken中獲取TokenMismatchException。可以看到cookies沒有被創建。可能的原因是什麼?Laravel中的TokenMismatchException:無法登錄

下面是意見:

1)有加:

< input type="hidden" name="_token" value="{{ csrf_token() }}">   

,但仍問題依然。

2)sessions.php物是人非來自:

'driver' => env('SESSION_DRIVER', 'file'), 

'driver' => env('SESSION_DRIVER', 'cookie'), 

,但仍問題依然。

3)我試過所有的瀏覽器,仍然沒有運氣。

4)相同的代碼庫在其他系統上正常工作,但不在我的系統上。此外,我也重新安裝了Windows,但仍然是同樣的問題。

請幫

Stack trace: 
#0 [internal function]: Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure)) 
#1 D:\Development\Code\biz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array) 
#2 D:\Development\Code\biz\vendor\laravel\framework\src\Illuminate\View\Middleware\ShareErrorsFromSession.php(49): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) 
#3 [internal function]: Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure)) 
#4 D:\Development\Code\biz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array) 
#5 D:\Development\Code\biz\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php(62): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) 
#6 [internal function]: Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure)) 
#7 D:\Development\Code\biz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array) 
#8 D:\Development\Code\biz\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse.php(37): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) 
#9 [internal function]: Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure)) 
#10 D:\Development\Code\biz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array) 
#11 D:\Development\Code\biz\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\EncryptCookies.php(59): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) 
#12 [internal function]: Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure)) 
#13 D:\Development\Code\biz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array) 
#14 D:\Development\Code\biz\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php(44): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) 
#15 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure)) 
#16 D:\Development\Code\biz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array) 
#17 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) 
#18 D:\Development\Code\biz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(102): call_user_func(Object(Closure), Object(Illuminate\Http\Request)) 
#19 D:\Development\Code\biz\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(122): Illuminate\Pipeline\Pipeline->then(Object(Closure)) 
#20 D:\Development\Code\biz\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(87): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request)) 
#21 D:\Development\Code\biz\public\index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request)) 
#22 D:\Development\Code\biz\server.php(21): require_once('D:\\Development\\...') 
#23 {main} 

.ENV

APP_ENV=local 
APP_DEBUG=true 
APP_KEY=fbq7vdvBI18IzYMLDxi9fmvSUaXuxaNg 

APP_TIMEZONE=Asia/Kolkata 

DB_CONNECTION=mysql 
DB_HOST=localhost 
DB_DATABASE=tnpdb 
DB_USERNAME=********* 
DB_PASSWORD=********* 
DB_STRICT=true 
DB_COLLATION=utf8_general_ci 

CACHE_DRIVER=file 
SESSION_DRIVER=file 
QUEUE_DRIVER=sync 

MAIL_DRIVER=smtp 
MAIL_HOST=******.*******************.net 
MAIL_PORT=587 
MAIL_USERNAME=*******@******.com 
MAIL_PASSWORD=********* 
MAIL_ENCRYPTION=tls 
MAIL_FROM_ADDRESS=*******@*******.com 
MAIL_FROM_NAME=********* 

WKHTMLTOPDF_BIN=/usr/local/bin/wkhtmltopdf 
WKHTMLTOIMG_BIN=/usr/local/bin/wkhtmltoimage 

sessions.php

<?php 

return [ 

/* 
|-------------------------------------------------------------------------- 
| Default Session Driver 
|-------------------------------------------------------------------------- 
| 
| This option controls the default session "driver" that will be used on 
| requests. By default, we will use the lightweight native driver but 
| you may specify any of the other wonderful drivers provided here. 
| 
| Supported: "file", "cookie", "database", "apc", 
|   "memcached", "redis", "array" 
| 
*/ 

'driver' => env('SESSION_DRIVER', 'file'), 

/* 
|-------------------------------------------------------------------------- 
| Session Lifetime 
|-------------------------------------------------------------------------- 
| 
| Here you may specify the number of minutes that you wish the session 
| to be allowed to remain idle before it expires. If you want them 
| to immediately expire on the browser closing, set that option. 
| 
*/ 

'lifetime' => 15, 

'expire_on_close' => false, 

/* 
|-------------------------------------------------------------------------- 
| Session Encryption 
|-------------------------------------------------------------------------- 
| 
| This option allows you to easily specify that all of your session data 
| should be encrypted before it is stored. All encryption will be run 
| automatically by Laravel and you can use the Session like normal. 
| 
*/ 

'encrypt' => false, 

/* 
|-------------------------------------------------------------------------- 
| Session File Location 
|-------------------------------------------------------------------------- 
| 
| When using the native session driver, we need a location where session 
| files may be stored. A default has been set for you but a different 
| location may be specified. This is only needed for file sessions. 
| 
*/ 

'files' => storage_path('framework/sessions'), 

/* 
|-------------------------------------------------------------------------- 
| Session Database Connection 
|-------------------------------------------------------------------------- 
| 
| When using the "database" or "redis" session drivers, you may specify a 
| connection that should be used to manage these sessions. This should 
| correspond to a connection in your database configuration options. 
| 
*/ 

'connection' => null, 

/* 
|-------------------------------------------------------------------------- 
| Session Database Table 
|-------------------------------------------------------------------------- 
| 
| When using the "database" session driver, you may specify the table we 
| should use to manage the sessions. Of course, a sensible default is 
| provided for you; however, you are free to change this as needed. 
| 
*/ 

'table' => 'sessions', 

/* 
|-------------------------------------------------------------------------- 
| Session Sweeping Lottery 
|-------------------------------------------------------------------------- 
| 
| Some session drivers must manually sweep their storage location to get 
| rid of old sessions from storage. Here are the chances that it will 
| happen on a given request. By default, the odds are 2 out of 100. 
| 
*/ 

'lottery' => [2, 100], 

/* 
|-------------------------------------------------------------------------- 
| Session Cookie Name 
|-------------------------------------------------------------------------- 
| 
| Here you may change the name of the cookie used to identify a session 
| instance by ID. The name specified here will get used every time a 
| new session cookie is created by the framework for every driver. 
| 
*/ 

'cookie' => 'laravel_session', 

/* 
|-------------------------------------------------------------------------- 
| Session Cookie Path 
|-------------------------------------------------------------------------- 
| 
| The session cookie path determines the path for which the cookie will 
| be regarded as available. Typically, this will be the root path of 
| your application but you are free to change this when necessary. 
| 
*/ 

'path' => '/', 

/* 
|-------------------------------------------------------------------------- 
| Session Cookie Domain 
|-------------------------------------------------------------------------- 
| 
| Here you may change the domain of the cookie used to identify a session 
| in your application. This will determine which domains the cookie is 
| available to in your application. A sensible default has been set. 
| 
*/ 

'domain' => null, 

/* 
|-------------------------------------------------------------------------- 
| HTTPS Only Cookies 
|-------------------------------------------------------------------------- 
| 
| By setting this option to true, session cookies will only be sent back 
| to the server if the browser has a HTTPS connection. This will keep 
| the cookie from being sent to you if it can not be done securely. 
| 
*/ 

'secure' => env('HTTPS_ONLY_COOKIES', false), 

]; 

VerifyCsrfToken.php

<?php 

namespace App\Http\Middleware; 

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

class VerifyCsrfToken extends BaseVerifier { 
/** 
* The URIs that should be excluded from CSRF verification. 
* 
* @var array 
*/ 
protected $except = [ 
    '*****response', 
    '*****response', 
    '*****response', 
    'v1/*', 
    ]; 
} 
+0

向我們展示您的.env和您的session.php配置文件 –

+0

我已添加它們 – Abhi1988

+0

t ry將會話驅動程序更改爲cookie並測試 –

回答

0

好,你應該讓網絡中間件像這裏面的路線:

Route::group(['middleware' => ['web']], function() { 
     Route::get('/','[email protected]'); 
    }); 

因爲會話和令牌中間件是這樣的同一中間件組「WEB」:

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, 

     ], 
    ]; 
+0

我們正在使用laravel 5.1。當我們把上面的代碼獲取這個錯誤:「ReflectionException在Container.php行741: 類網絡不存在」 – Abhi1988

+0

你能給我你的控制器的名字嗎? –