LARAVEL + JWT

laravel 2016年7月28日

[vc_row][vc_column][vc_column_text]什麼是JWT

簡單來說就是JSON Web Tokens

今天,我登入了,就會收到一組token,然而我再拿著這組token繼續去要資料,當然也包括身分的識別,而且這組token是有時間限制的,當token過期,就必須重新登入。

JWT本身是極為輕巧的規範,有乖乖點進第一行的超連結的同學就會知道它有多輕巧。

今天我們就來使用Laravel來實作,若還不認識Laravel的同學請轉向

必備環境:Laravel、composer、Apache或其他Web Server、MySQL、PHP、Postman

當然,Wagon都幫我們準備好了,使用其他環境也是可以的,當然也能在現成的環境上增加。

Postman是一款可以用來測試GET、POST、UPDATE、DELETE等Restful API的工具。

進入正題:

1.利用commanad line來new一個專案並安裝JWT

composer create-project laravel/laravel JWTExam "5.2.*" cd JWTExam composer require tymon/jwt-auth

筆者使用版本:Laravel 5.2 tymon/jwt-auth 0.5.9

  1. 建立資料庫以及修改.env的DB部分

  2. common line: <code class="EnlighterJSRAW" data-enlighter-language="null">artisan migrate

  3. config\app.php

'providers' => [ ..., Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class ]

'aliases' => [ ..., 'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class, 'JWTFactory' => Tymon\JWTAuth\Facades\JWTFactory::class ]

  1. command line: <code class="EnlighterJSRAW" data-enlighter-language="null">artisan vendor:publish --provider="Tymon\JWTAuth\Providers\JWTAuthServiceProvider"

  2. 生成JWT KEY

command line: <code class="EnlighterJSRAW" data-enlighter-language="null">artisan jwt:generate

  1. 我們使用內建的user model與users資料表來試做

建立seed來模擬資料

database\seeds\DatabaseSeeder.php

'test', 'email' => '[email protected]', 'password' => Hash::make('secret') ] ); } } command line: `artisan db:seed` (建立完記得檢查是否成功) 8. app\Http\Kernel.php protected $routeMiddleware = [ ..., 'jwt.auth' => \Tymon\JWTAuth\Middleware\GetUserFromToken::class, 'jwt.refresh' => \Tymon\JWTAuth\Middleware\RefreshToken::class ]; 9. 建立路由 app\Http\routes.php Route::group(['prefix' => 'api'], function() { Route::get('auth', 'AuthController@index'); Route::post('auth', 'AuthController@auth'); }); 10. 建立controller `artisan make:controller AuthController` app\Http\Controllers\AuthController.php (路徑不要錯了) only('email', 'password'); try { if (! $token = JWTAuth::attempt($credentials)) { return response()->json(['error' => 'invalid_credentials'], 401); } } catch (JWTException $e) { return response()->json(['error' => 'could_not_create_token'], 500); } return response()->json(compact('token')); } public function __construct() { $this->middleware('jwt.auth', ['except' => ['auth']]); } public function index() { return response()->json(Auth::user()->all()); } } 11. 開啟Postman進行測試 (建議先在command line: `artisan route:list `看看有沒有錯誤訊息) ![1](https://bgpsekai.thisistap.com/wp-content/uploads/2016/07/1-500x270.png) 這個是沒有經過驗證的結果 我們現在來取得token ![2](https://bgpsekai.thisistap.com/wp-content/uploads/2016/07/2-500x270.png) 這個代表沒有經過Laravel的[csrf](https://laravel.tw/docs/5.2/routing#csrf-protection)認證(這邊就不贅述了) 讓我們把csrf關閉 在app\Http\Kernel.php找到這行,並且註解 ` \App\Http\Middleware\VerifyCsrfToken::class,` ![3](https://bgpsekai.thisistap.com/wp-content/uploads/2016/07/3-500x270.png) 出現此錯誤代表email或password錯誤 ![4](https://bgpsekai.thisistap.com/wp-content/uploads/2016/07/4-500x270.png) 這樣子就可以得到token了 接下來將這token這樣使用(如網址列) ![5](https://bgpsekai.thisistap.com/wp-content/uploads/2016/07/5-500x270.png) 如此,我們就能取得想要的資料了   ※圖中有切換GET與POST   另外,如果有同學想利用ajax來獲得資料的話會遇上cors的錯誤(如圖) ![6](https://bgpsekai.thisistap.com/wp-content/uploads/2016/07/6-500x194.png) 我們將在[這篇文章](https://bgpsekai.thisistap.com/tutorials/laravel/2016/07/laravel-cors/)講解如何解決   心理OS: TMD又要來測試這篇文章的完整性,重run一次…(趴   參考文章: http://blog.qiji.tech/archives/4091[/vc_column_text][/vc_column][/vc_row]
Success! Your account is fully activated, you now have access to all content.