下面由laravel教程欄目給大家介紹laravel里api路由的 auth:api 和 api_Token,希望對需要的朋友有所幫助!
搞一搞laravel里api路由的 auth:api 和 api_token
我這幾天正好在琢磨這個東西, 找資料的時候也找到了這個問題. 順帶寫一下挖的過程, 省得以后有人想問個為啥, 卻搜半天搜不到點中文資料. 就是怕像個人筆記,不太干.
問題的地址:
https://segmentfault.com/q/1010000008388170/a-1020000009910771
這個東西(token based authentication )是在5.2中出現的.那么下面開始:
首先看 /config/auth 中的 guards 字段:
'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ]
對于上面兩個東西(guards), 在路徑 {project}/vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php 和 {project}/vendor/laravel/framework/src/Illuminate/Auth/TokenGuard.php 里面可以看到.
在TokenGuard里面可以看到 user() 方法, 比如 Auth::user() 會返回一個用戶, 調用的就是這個方法.
然后看 {project}/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php, 這個里面的 guard 方法, 就是 Auth::guard(‘api’)->check() 或者 Auth::check() 之類的代碼執行時候會調用的方法. 它干了什么呢
public function guard($name = null) { //這里就是沒有提供名字,就默認用web $name = $name ?: $this->getDefaultDriver(); //然后如果已經有這個guard,就返回; 沒有的話,就resolve這個名字 return isset($this->guards[$name]) ? $this->guards[$name] : $this->guards[$name] = $this->resolve($name); }
那么接著看 resolve 干了什么
protected function resolve($name) { $config = $this->getConfig($name); if (is_null($config)) { throw new InvalidArgumentException("Auth guard [{$name}] is not defined."); } if (isset($this->customCreators[$config['driver']])) { return $this->callCustomCreator($name, $config); } $driverMethod = 'create'.ucfirst($config['driver']).'Driver'; if (method_exists($this, $driverMethod)) { return $this->{$driverMethod}($name, $config); } throw new InvalidArgumentException("Auth guard driver [{$name}] is not defined."); }
第一步的 getConfig :
protected function getConfig($name) { return $this->app['config']["auth.guards.{$name}"]; }
去找開頭提到的 config/auth 里面的配置項. 比如 api 得到的就是
[ 'driver' => 'token', 'provider' => 'users', ],
搞到配置項以后, 在 resolve 里面繼續
$driverMethod = 'create'.ucfirst($config['driver']).'Driver'; if (method_exists($this, $driverMethod)) { return $this->{$driverMethod}($name, $config); }
如果存在相應名字的custom的driver,調用, (這個在默認的兩個之前)
如果存在自帶的Driver的話, 調用相應的 createXXXXXDriver 方法. 傳進去 $name 和 $config.
那么繼續看:
public function createTokenDriver($name, $config) { // The token guard implements a basic API token based guard implementation // that takes an API token field from the request and matches it to the // user in the database or another persistence layer where users are. $guard = new TokenGuard( $this->createUserProvider($config['provider']), $this->app['request'] ); $this->app->refresh('request', $guard, 'setRequest'); return $guard; }
注意這里用戶未必一定是數據庫里面搞出來的. 也可能是別的地方, 然而要看你的provider. laravel 這里的 provider 默認是 EloquentUserProvider, 那顯然呵呵了, 你只能從數據庫表里面找.
實例化了一個 TokenGuard :
public function user() { if (! is_null($this->user)) { return $this->user; } $user = null; $token = $this->getTokenForRequest(); if (! empty($token)) { $user = $this->provider->retrieveByCredentials( [$this->storageKey => $token] ); } return $this->user = $user; }
如果么有已經存在的用戶,就用 getTokenForRequest 來搞一個.
public function getTokenForRequest() { $token = $this->request->query($this->inputKey); if (empty($token)) { $token = $this->request->input($this->inputKey); } if (empty($token)) { $token = $this->request->bearerToken(); } if (empty($token)) { $token = $this->request->getPassword(); } return $token; }
基本都是在搞request里面的 $this->inputKey 字段. 劃重點.
這個屬性在構造器里面默認了: $this->inputKey = ‘api_token’.
也就是你的api request 里面, 應該是有一個
[ api_token => ' 一堆隨便什么字符串OUVjkknag89s8c987235iohiscovy89q235 ' ]
這樣的東西
我確實沒在文檔里找見.
那么現在結論反而很簡單, 如果你想用 laravel 自帶的 auth:api來寫API, ?那么:
- 你的post或者任何需要驗證的api請求, 都應該有一個api_token的字段.
-
你的用戶表里面應該有一個字段api_token, 隨便什么東西bcrypt一下.
-
然后你 routes/api 下面就可以寫一堆api路由來測試了.
之后你可以看看官網的 passport 文檔之類的.
相關推薦:最新的五個Laravel視頻教程