2017-08-04 112 views
1

在這背後的故事,我一直在努力從文檔的說明:https://laravel.com/docs/5.4/passportLaravel 5.4 401 /未經認證使用護照和多種令牌類型

  1. Laravel 5.4
  2. 「laravel /護照「:」^ 3.0「來自作曲家
  3. 本地Mac osx運行Mamp pro,Php 7.0.15

我打電話在路由示例用戶路由/ api.php

Route::get('/user', function() { 
    return 'testing'; 
})->middleware('auth:api'); 

郵差捲曲頭(從郵差代碼出口拉動):

CURLOPT_HTTPHEADER => array(
    "accept: application/json", 
    "authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImM3ZmI2ZmNmMWFkOGQ0NjFkNTdhMWU2NjFiYjhhOThmOTJhOTBkMDFkNDkwZDFjNDRkNDg5MTdlYjJiZWYyMDlkNjNmOTQwMjIxNTljZWI5In0.eyJhdWQiOiIxIiwianRpIjoiYzdmYjZmY2YxYWQ4ZDQ2MWQ1N2ExZTY2MWJiOGE5OGY5MmE5MGQwMWQ0OTBkMWM0NGQ0ODkxN2ViMmJlZjIwOWQ2M2Y5NDAyMjE1OWNlYjkiLCJpYXQiOjE1MDE3OTQ2NjIsIm5iZiI6MTUwMTc5NDY2MiwiZXhwIjoxNTMzMzMwNjYyLCJzdWIiOiIxIiwic2NvcGVzIjpbImFwaS1hY2Nlc3MiXX0.CPGM4PIKJBeiJvokuDzShz_1CnqHlnFIML-tWoBCn5GcijMXmQkWOHzTI8QwTws2h719TGA4hemXDljjqoZB0LiztAx2JZ3OhjNS-MhrMNujnTJUbvkXAVfcRdybhlDEWof_iboLICQTYNTslX1iw-2DCyFMh8gB4INAKUhpvzA955ALB-ZunKrjSNKdRkgtZRe0t6VyJf9LwzgjIAfSKoi_qRis36KD7hcf0Id_iWZkhvS-ZfuM5eUpzUooUe0rb4rkYYEYndlHlY7-uuZPlzmPMpaJTR4AW1CLkaK5Ic7fde1x1kk2duW_Znd9ki2YBP0kw7ifAmg2DaM5r2-0kEx_1iFuCIxE8QJns1aIm3XjWoOApovt7V6-s3yJZK3xlIDCjFI-C59RHiVSabh-hKdX4elvSL9taSQyuramPZPpsne9SUh4KCWul0iHoNjFdFJEut_TUBWyUPtD3J7gg6P97uRS_THDAUHMo2UYVhlnu9PV8SvbvjGj3OeaaH7ZbzWQCYKbqsLZAZ2mnJlFhTMghbaC2s_MND1zlRm7w9btmihxVW714NUbH8UAwSvrtIYYQ0itevZ59TLiAXprjmjkhiFkrhdX4bUje4uNEbLYawkZI-1o82IExW9D8kCYpOWOZdWTCLgmaE2wXcf-DTCV-9vDWRAdX1YmP4JbRsc", 
    "cache-control: no-cache", 
    "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", 
    "postman-token: 2ec7a2c8-3489-812d-4638-ebb7dc62aeb1" 
), 

我有使用所生成的1個個人訪問令牌Vue組件

我檢查了我設置了1年的令牌過期並且反映在數據庫中。

我AuthServiceProvider.php

Passport::routes(); 

     // TODO MAKE THEM LAST A LONG TIME 
     Passport::tokensExpireIn(Carbon::now()->addYears(20));//You can also use addDays(10) 
     Passport::refreshTokensExpireIn(Carbon::now()->addYears(20));//You can also use addDays(10) 
     Passport::pruneRevokedTokens(); //basic garbage collector 

     Passport::tokensCan([ 
      'api-access' => 'Access Complete API', 
     ]); 

我RouteServiceProvider.php(mapApiRoutes是所謂的地圖功能)

protected function mapApiRoutes() 
    { 
     Route::group([ 
      'namespace' => $this->namespace, 
      'prefix' => 'api', 
     ], function ($router) { 
      require base_path('routes/api.php'); 
     }); 
    } 

我Kernel.php路線中間件

protected $routeMiddleware = [ 
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class, 
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, 
    'can' => \Illuminate\Auth\Middleware\Authorize::class, 
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 
]; 

- 編輯 - 更多研究後

我正在使用郵遞員來測試我的api認證,並且每次嘗試時都會得到一個401。我嘗試過Personal Access Client和Password Grant Client,兩者都有相同的問題。看了他們兩個後,我意識到兩者都使用Authorization Bearer [token]格式。

因此,我開始在Passport源中的各種文件中註銷。

在TokenGuard.php

public function user(Request $request) { 

    Log::info('TokenGuard: '. $request); 

    if ($request->bearerToken()) { 
     return $this->authenticateViaBearerToken($request); 
    } elseif ($request->cookie(Passport::cookie())) { 
     return $this->authenticateViaCookie($request); 
    } 
} 

日誌看起來像這樣:

[2017-08-10 20:50:20] local.INFO: TokenGuard 93: GET /api/user HTTP/1.1 
Accept:   application/json 
Accept-Encoding: gzip, deflate 
Cache-Control: no-cache 
Connection:  keep-alive 
Content-Type: application/json 
Host:   url.com:8888 
Postman-Token: 66707fe5-8f6e-4920-948b-2804a76d4a65 
User-Agent:  PostmanRuntime/6.2.5 


[2017-08-10 20:50:20] local.INFO: TokenGuard 93: GET /api/user HTTP/1.1 
Accept:   application/json 
Accept-Encoding: gzip, deflate 
Cache-Control: no-cache 
Connection:  keep-alive 
Content-Type: application/json 
Host:   url.com:8888 
Postman-Token: 66707fe5-8f6e-4920-948b-2804a76d4a65 
User-Agent:  PostmanRuntime/6.2.5 

事情缺少的是該請求的承載[令牌]部分。 TokenGuard代碼塊正在運行一個if/else。這是我認爲失敗正在發生的地方。

它不應該記錄下來嗎?由於無記名令牌缺失,所以If/Else失敗,然後返回401是有意義的。

爲什麼我的令牌被剝離出請求。

+0

你如何讓你的令牌中的JavaScript?上面的代碼示例中的令牌似乎太長,您確定您沒有複製粘貼加密的cookie嗎? – Kyslik

+0

當您創建個人訪問令牌時,Vue組件將彈出一個包含該令牌的模式。我複製它,然後使用它。我已經嘗試了這三個不同的時間,以確保我沒有搞砸了。 –

+0

你唯一的選擇是自己調試它,使用'\ Log :: info ...'語句修改相關文件(甚至是Laravel核心文件),並看看你陷入了什麼困境。玩的開心! – Kyslik

回答

3

給你的.htaccess文件添加項目的公共目錄下:

RewriteEngine on 
RewriteCond %{HTTP:Authorization} ^(.*) 
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]