国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

laravel dingo-api添加jwt-auth認證

2020-03-22 19:31:45
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了關于laravel dingo/api添加jwt-auth認證,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

 

前面我們學了laravel dingo/api創建簡單的api,這樣api是開放給所有人的,如何查看和限制api的調用呢?可以用jwt-auth來驗證,JSON Web Token Authentication

1,首先安裝jwt-auth插件,在命令行中用composer安裝

 

composer require tymon/jwt-auth 0.5.* 

 

2,然后發布

 

php artisan vendor:publish --provider= TymonJWTAuthProvidersJWTAuthServiceProvider 

 

在/config/生成了一個jwt.php文件

3,生成key

 

php artisan jwt:generate

 

如果命令無法運行,可以在/config/jwt.php文件中修改changeme為自己設置的密匙

 

 secret = env( JWT_SECRET , changeme ),

 

4,修改/app/Api/Controllers/HelloController.php為

 

 ?phpnamespace AppApiControllers;use IlluminateHttpRequest;use AppHttpControllersController;//添加jwt-auth認證use JWTAuth;use TymonJWTAuthExceptionsJWTException;html' target='_blank'>class HelloController extends Controller public function index() return {content:Helloworld!} //添加jwt-auth認證 public function authenticate(Request $request) // grab credentials from the request $credentials = $request- only( email , password  try { // attempt to verify the credentials and create a token for the user if (! $token = JWTAuth::attempt($credentials)) { return response()- json([ error = invalid_credentials ], 401); } catch (JWTException $e) { // something went wrong whilst attempting to encode the token return response()- json([ error = could_not_create_token ], 500); // all good so return the token return response()- json(compact( token }

 

5,添加路由(/routes/web.php)

 

$api- post( auth , AppApiControllersHelloController@authenticate 

 

6,測試路由:php artisan api:routes,如果出現如下提示表示正確

訪問url:***.com/api/auth顯示錯誤,因為沒加token

重新修改hellocontrol和loutes

 

 ?phpnamespace AppApiControllers;use IlluminateHttpRequest;use AppHttpControllersController;use JWTAuth;use TymonJWTAuthExceptionsJWTException;class HelloController extends Controller * Create a new controller instance. * @return void // attempt to verify the credentials and create a token for the user if (! $token = JWTAuth::attempt($credentials)) { return response()- json([ error = invalid_credentials ], 401); } catch (JWTException $e) { // something went wrong whilst attempting to encode the token return response()- json([ error = could_not_create_token ], 500); // all good so return the token return response()- json(compact( token  //添加user public function user() JWTAuth::parseToken(); $user = JWTAuth::parseToken()- authenticate(); return $user;}

 

 ?phpRoute::get( / , function () { return view( welcome Auth::routes();Route::get( /home , HomeController@index )- name( home $api = app( DingoApiRoutingRouter $api- version( v1 , function ($api) { $api- get( helloworld , AppApiControllersHelloController@index  $api- post( auth , AppApiControllersHelloController@authenticate  $api- get( auth , AppApiControllersHelloController@user });

 

用谷歌瀏覽器postman插件獲取token,注意是post方法,步驟如下圖所示

將獲取的token復制,黏貼到第二步的用戶驗證token中,下圖5中就是我們剛剛注冊的用戶

以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP !

相關推薦:

Laravel5.5中利用Passport實現Auth認證的方法講解

Laravel框架中composer自動加載的實現詳解

以上就是laravel dingo/api添加jwt-auth認證的詳細內容,PHP教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宜川县| 横峰县| 开鲁县| 大余县| 登封市| 绩溪县| 绥芬河市| 赣榆县| 志丹县| 东城区| 于都县| 吉首市| 黄龙县| 察隅县| 惠安县| 句容市| 顺义区| 昌吉市| 西昌市| 青铜峡市| 唐海县| 班玛县| 临朐县| 罗甸县| 石台县| 孟州市| 苗栗县| 将乐县| 托克托县| 华安县| 即墨市| 丽水市| 宁乡县| 潢川县| 大足县| 中牟县| 嘉鱼县| 文昌市| 久治县| 合阳县| 城步|