\u57fa\u4e8e Laravel Auth \u5b9e\u73b0\u81ea\u5b9a\u4e49\u63a5\u53e3 API \u7528\u6237\u8ba4\u8bc1\u8...

专栏三
33
九月来了

\u57fa\u4e8e laravel \u9ed8\u8ba4\u7684 auth<\/code> \u5b9e\u73b0 api \u8ba4\u8bc1<\/h2>\n

\u73b0\u5728\u5fae\u670d\u52a1\u8d8a\u6765\u8d8a\u6d41\u884c\u4e86. \u5f88\u591a\u4e1c\u897f\u90fd\u62c6\u5206\u6210\u72ec\u7acb\u7684\u7cfb\u7edf,\u5404\u4e2a\u7cfb\u7edf\u4e4b\u95f4\u6ca1\u6709\u76f4\u63a5\u7684\u5173\u7cfb. \u8fd9\u6837\u6211\u4eec\u5982\u679c\u505a\u7528\u6237\u8ba4\u8bc1\u80af\u5b9a\u662f\u7edf\u4e00\u7684\u505a\u4e00\u4e2a\u72ec\u7acb\u7684 \u7528\u6237\u8ba4\u8bc1<\/code> \u7cfb\u7edf,\u800c\u4e0d\u662f\u6bcf\u4e2a\u4e1a\u52a1\u7cfb\u7edf\u90fd\u8981\u91cd\u65b0\u53bb\u5199\u4e00\u904d\u7528\u6237\u8ba4\u8bc1\u76f8\u5173\u7684\u4e1c\u897f. \u4f46\u662f\u53c8\u9047\u5230\u4e00\u4e2a\u95ee\u9898\u4e86. laravel<\/code> \u9ed8\u8ba4\u7684auth \u8ba4\u8bc1<\/code> \u662f\u57fa\u4e8e\u6570\u636e\u5e93\u505a\u7684,\u5982\u679c\u8981\u5fae\u670d\u52a1\u67b6\u6784\u53ef\u600e\u4e48\u505a\u5462?<\/p>\n

\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b:<\/h2>\n

UserProvider \u63a5\u53e3:<\/h3>\n

\/\/ \u901a\u8fc7\u552f\u4e00\u6807\u793a\u7b26\u83b7\u53d6\u8ba4\u8bc1\u6a21\u578b\npublic function retrieveById($identifier);\n\/\/ \u901a\u8fc7\u552f\u4e00\u6807\u793a\u7b26\u548c remember token \u83b7\u53d6\u6a21\u578b\npublic function retrieveByToken($identifier, $token);\n\/\/ \u901a\u8fc7\u7ed9\u5b9a\u7684\u8ba4\u8bc1\u6a21\u578b\u66f4\u65b0 remember token\npublic function updateRememberToken(Authenticatable $user, $token);\n\/\/ \u901a\u8fc7\u7ed9\u5b9a\u7684\u51ed\u8bc1\u83b7\u53d6\u7528\u6237\uff0c\u6bd4\u5982 email \u6216\u7528\u6237\u540d\u7b49\u7b49\npublic function retrieveByCredentials(array $credentials);\n\/\/ \u8ba4\u8bc1\u7ed9\u5b9a\u7684\u7528\u6237\u548c\u7ed9\u5b9a\u7684\u51ed\u8bc1\u662f\u5426\u7b26\u5408\npublic function validateCredentials(Authenticatable $user, array $credentials);<\/code><\/pre>\n

Laravel<\/code> \u4e2d\u9ed8\u8ba4\u6709\u4e24\u4e2a user provider<\/strong> : DatabaseUserProvider<\/code> & EloquentUserProvider<\/code>.
DatabaseUserProvider<\/strong>
Illuminate\\Auth\\DatabaseUserProvider<\/code><\/p>\n

\u76f4\u63a5\u901a\u8fc7\u6570\u636e\u5e93\u8868\u6765\u83b7\u53d6\u8ba4\u8bc1\u6a21\u578b.<\/p>\n

EloquentUserProvider<\/strong>
Illuminate\\Auth\\EloquentUserProvider<\/code><\/p>\n

\u901a\u8fc7 eloquent \u6a21\u578b\u6765\u83b7\u53d6\u8ba4\u8bc1\u6a21\u578b<\/p>\n

\n\n

\u6839\u636e\u4e0a\u9762\u7684\u77e5\u8bc6\uff0c\u53ef\u4ee5\u77e5\u9053\u8981\u81ea\u5b9a\u4e49\u4e00\u4e2a\u8ba4\u8bc1\u5f88\u7b80\u5355\u3002<\/p>\n

\u81ea\u5b9a\u4e49 Provider<\/code><\/h2>\n

\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u8ba4\u8bc1\u6a21\u578b\uff0c\u5b9e\u73b0 Authenticatable \u63a5\u53e3\uff1b<\/p>\n

App\\Auth\\UserProvider.php<\/code><\/p>\n

<?php\n\nnamespace App\\Auth;\n\nuse App\\Models\\User;\nuse Illuminate\\Contracts\\Auth\\Authenticatable;\nuse Illuminate\\Contracts\\Auth\\UserProvider as Provider;\n\nclass UserProvider implements Provider\n{\n\n \/**\n * Retrieve a user by their unique identifier.\n * @param mixed $identifier\n * @return \\Illuminate\\Contracts\\Auth\\Authenticatable|null\n *\/\n public function retrieveById($identifier)\n {\n return app(User::class)::getUserByGuId($identifier);\n }\n\n \/**\n * Retrieve a user by their unique identifier and \"remember me\" token.\n * @param mixed $identifier\n * @param string $token\n * @return \\Illuminate\\Contracts\\Auth\\Authenticatable|null\n *\/\n public function retrieveByToken($identifier, $token)\n {\n return null;\n }\n\n \/**\n * Update the \"remember me\" token for the given user in storage.\n * @param \\Illuminate\\Contracts\\Auth\\Authenticatable $user\n * @param string $token\n * @return bool\n *\/\n public function updateRememberToken(Authenticatable $user, $token)\n {\n return true;\n }\n\n \/**\n * Retrieve a user by the given credentials.\n * @param array $credentials\n * @return \\Illuminate\\Contracts\\Auth\\Authenticatable|null\n *\/\n public function retrieveByCredentials(array $credentials)\n {\n if ( !isset($credentials['api_token'])) {\n return null;\n }\n\n return app(User::class)::getUserByToken($credentials['api_token']);\n }\n\n \/**\n * Rules a user against the given credentials.\n * @param \\Illuminate\\Contracts\\Auth\\Authenticatable $user\n * @param array $credentials\n * @return bool\n *\/\n public function validateCredentials(Authenticatable $user, array $credentials)\n {\n if ( !isset($credentials['api_token'])) {\n return false;\n }\n\n return true;\n }\n}\n<\/code><\/pre>\n

Authenticatable \u63a5\u53e3:<\/h3>\n

Illuminate\\Contracts\\Auth\\Authenticatable<\/code>
\nAuthenticatable \u5b9a\u4e49\u4e86\u4e00\u4e2a\u53ef\u4ee5\u88ab\u7528\u6765\u8ba4\u8bc1\u7684\u6a21\u578b\u6216\u7c7b\u9700\u8981\u5b9e\u73b0\u7684\u63a5\u53e3\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u9700\u8981\u7528\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u7c7b\u6765\u505a\u8ba4\u8bc1\uff0c\u9700\u8981\u5b9e\u73b0\u8fd9\u4e2a\u63a5\u53e3\u5b9a\u4e49\u7684\u65b9\u6cd5\u3002<\/p>\n

<?php\n.\n.\n.\n\/\/ \u83b7\u53d6\u552f\u4e00\u6807\u8bc6\u7684\uff0c\u53ef\u4ee5\u7528\u6765\u8ba4\u8bc1\u7684\u5b57\u6bb5\u540d\uff0c\u6bd4\u5982 id\uff0cuuid\npublic function getAuthIdentifierName();\n\/\/ \u83b7\u53d6\u8be5\u6807\u793a\u7b26\u5bf9\u5e94\u7684\u503c\npublic function getAuthIdentifier();\n\/\/ \u83b7\u53d6\u8ba4\u8bc1\u7684\u5bc6\u7801\npublic function getAuthPassword();\n\/\/ \u83b7\u53d6remember token\npublic function getRememberToken();\n\/\/ \u8bbe\u7f6e remember token\npublic function setRememberToken($value);\n\/\/ \u83b7\u53d6 remember token \u5bf9\u5e94\u7684\u5b57\u6bb5\u540d\uff0c\u6bd4\u5982\u9ed8\u8ba4\u7684 'remember_token'\npublic function getRememberTokenName();\n.\n.\n.<\/code><\/pre>\n

Laravel \u4e2d\u5b9a\u4e49\u7684 Authenticatable trait<\/code>\uff0c\u4e5f\u662f Laravel auth \u9ed8\u8ba4\u7684 User<\/code> \u6a21\u578b\u4f7f\u7528\u7684 trait\uff0c\u8fd9\u4e2a trait \u5b9a\u4e49\u4e86 User<\/code> \u6a21\u578b\u9ed8\u8ba4\u8ba4\u8bc1\u6807\u793a\u7b26\u4e3a 'id'\uff0c\u5bc6\u7801\u5b57\u6bb5\u4e3apassword<\/code>\uff0cremember token<\/code> \u5bf9\u5e94\u7684\u5b57\u6bb5\u4e3a remember_token<\/code> \u7b49\u7b49\u3002
\n\u200b
\n\u901a\u8fc7\u91cd\u5199 User<\/code> \u6a21\u578b\u7684\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u4fee\u6539\u4e00\u4e9b\u8bbe\u7f6e\u3002<\/p>\n

\u5b9e\u73b0\u81ea\u5b9a\u4e49\u8ba4\u8bc1\u6a21\u578b<\/h3>\n

App\\Models\\User.php<\/code><\/p>\n

<?php\n\nnamespace App\\Models;\n\nuse App\\Exceptions\\RestApiException;\nuse App\\Models\\Abstracts\\RestApiModel;\nuse Illuminate\\Contracts\\Auth\\Authenticatable;\n\nclass User extends RestApiModel implements Authenticatable\n{\n\n protected $primaryKey = 'guid';\n\n public $incrementing = false;\n\n protected $keyType = 'string';\n\n \/**\n * \u83b7\u53d6\u552f\u4e00\u6807\u8bc6\u7684\uff0c\u53ef\u4ee5\u7528\u6765\u8ba4\u8bc1\u7684\u5b57\u6bb5\u540d\uff0c\u6bd4\u5982 id\uff0cguid\n * @return string\n *\/\n public function getAuthIdentifierName()\n {\n return $this->primaryKey;\n }\n\n \/**\n * \u83b7\u53d6\u4e3b\u952e\u7684\u503c\n * @return mixed\n *\/\n public function getAuthIdentifier()\n {\n $id = $this->{$this->getAuthIdentifierName()};\n return $id;\n }\n\n public function getAuthPassword()\n {\n return '';\n }\n\n public function getRememberToken()\n {\n return '';\n }\n\n public function setRememberToken($value)\n {\n return true;\n }\n\n public function getRememberTokenName()\n {\n return '';\n }\n\n protected static function getBaseUri()\n {\n return config('api-host.user');\n }\n\n public static $apiMap = [\n 'getUserByToken' => ['method' => 'GET', 'path' => 'login\/user\/token'],\n 'getUserByGuId' => ['method' => 'GET', 'path' => 'user\/guid\/:guid'],\n ];\n\n \/**\n * \u83b7\u53d6\u7528\u6237\u4fe1\u606f (by guid)\n * @param string $guid\n * @return User|null\n *\/\n public static function getUserByGuId(string $guid)\n {\n try {\n $response = self::getItem('getUserByGuId', [\n ':guid' => $guid\n ]);\n } catch (RestApiException $e) {\n return null;\n }\n\n return $response;\n }\n\n \/**\n * \u83b7\u53d6\u7528\u6237\u4fe1\u606f (by token)\n * @param string $token\n * @return User|null\n *\/\n public static function getUserByToken(string $token)\n {\n try {\n $response = self::getItem('getUserByToken', [\n 'Authorization' => $token\n ]);\n } catch (RestApiException $e) {\n return null;\n }\n\n return $response;\n }\n}\n<\/code><\/pre>\n

\u4e0a\u9762 RestApiModel<\/code> \u662f\u6211\u4eec\u516c\u53f8\u5bf9 Guzzle<\/code> \u7684\u5c01\u88c5,\u7528\u4e8e php \u9879\u76ee\u5404\u4e2a\u7cfb\u7edf\u4e4b\u95f4 api<\/code> \u8c03\u7528. \u4ee3\u7801\u5c31\u4e0d\u65b9\u4fbf\u900f\u6f0f\u4e86.<\/p>\n

Guard \u63a5\u53e3<\/h2>\n

Illuminate\\Contracts\\Auth\\Guard<\/code><\/p>\n

Guard<\/code> \u63a5\u53e3\u5b9a\u4e49\u4e86\u67d0\u4e2a\u5b9e\u73b0\u4e86 Authenticatable<\/code> (\u53ef\u8ba4\u8bc1\u7684) \u6a21\u578b\u6216\u7c7b\u7684\u8ba4\u8bc1\u65b9\u6cd5\u4ee5\u53ca\u4e00\u4e9b\u5e38\u7528\u7684\u63a5\u53e3\u3002<\/p>\n

\/\/ \u5224\u65ad\u5f53\u524d\u7528\u6237\u662f\u5426\u767b\u5f55\npublic function check();\n\/\/ \u5224\u65ad\u5f53\u524d\u7528\u6237\u662f\u5426\u662f\u6e38\u5ba2\uff08\u672a\u767b\u5f55\uff09\npublic function guest();\n\/\/ \u83b7\u53d6\u5f53\u524d\u8ba4\u8bc1\u7684\u7528\u6237\npublic function user();\n\/\/ \u83b7\u53d6\u5f53\u524d\u8ba4\u8bc1\u7528\u6237\u7684 id\uff0c\u4e25\u683c\u6765\u8bf4\u4e0d\u4e00\u5b9a\u662f id\uff0c\u5e94\u8be5\u662f\u4e0a\u4e2a\u6a21\u578b\u4e2d\u5b9a\u4e49\u7684\u552f\u4e00\u7684\u5b57\u6bb5\u540d\npublic function id();\n\/\/ \u6839\u636e\u63d0\u4f9b\u7684\u6d88\u606f\u8ba4\u8bc1\u7528\u6237\npublic function validate(array $credentials = []);\n\/\/ \u8bbe\u7f6e\u5f53\u524d\u7528\u6237\npublic function setUser(Authenticatable $user);<\/code><\/pre>\n

StatefulGuard \u63a5\u53e3<\/h3>\n

Illuminate\\Contracts\\Auth\\StatefulGuard<\/code><\/p>\n

StatefulGuard<\/code> \u63a5\u53e3\u7ee7\u627f\u81ea Guard<\/code> \u63a5\u53e3\uff0c\u9664\u4e86 Guard<\/code> \u91cc\u9762\u5b9a\u4e49\u7684\u4e00\u4e9b\u57fa\u672c\u63a5\u53e3\u5916\uff0c\u8fd8\u589e\u52a0\u4e86\u66f4\u8fdb\u4e00\u6b65\u3001\u6709\u72b6\u6001\u7684 Guard<\/code>.<\/p>\n

\u65b0\u6dfb\u52a0\u7684\u63a5\u53e3\u6709\u8fd9\u4e9b\uff1a<\/p>\n

\/\/ \u5c1d\u8bd5\u6839\u636e\u63d0\u4f9b\u7684\u51ed\u8bc1\u9a8c\u8bc1\u7528\u6237\u662f\u5426\u5408\u6cd5\npublic function attempt(array $credentials = [], $remember = false);\n\/\/ \u4e00\u6b21\u6027\u767b\u5f55\uff0c\u4e0d\u8bb0\u5f55session or cookie\npublic function once(array $credentials = []);\n\/\/ \u767b\u5f55\u7528\u6237\uff0c\u901a\u5e38\u5728\u9a8c\u8bc1\u6210\u529f\u540e\u8bb0\u5f55 session \u548c cookie \npublic function login(Authenticatable $user, $remember = false);\n\/\/ \u4f7f\u7528\u7528\u6237 id \u767b\u5f55\npublic function loginUsingId($id, $remember = false);\n\/\/ \u4f7f\u7528\u7528\u6237 ID \u767b\u5f55\uff0c\u4f46\u662f\u4e0d\u8bb0\u5f55 session \u548c cookie\npublic function onceUsingId($id);\n\/\/ \u901a\u8fc7 cookie \u4e2d\u7684 remember token \u81ea\u52a8\u767b\u5f55\npublic function viaRemember();\n\/\/ \u767b\u51fa\npublic function logout();<\/code><\/pre>\n

Laravel<\/code> \u4e2d\u9ed8\u8ba4\u63d0\u4f9b\u4e86 3 \u4e2d guard<\/strong> \uff1aRequestGuard<\/code>\uff0cTokenGuard<\/code>\uff0cSessionGuard<\/code>.<\/p>\n

RequestGuard<\/h3>\n

Illuminate\\Auth\\RequestGuard<\/code><\/p>\n

RequestGuard \u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684 guard. RequestGuard \u662f\u901a\u8fc7\u4f20\u5165\u4e00\u4e2a\u95ed\u5305\u6765\u8ba4\u8bc1\u7684\u3002\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528 Auth::viaRequest<\/code> \u6dfb\u52a0\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684 RequestGuard.<\/p>\n

SessionGuard<\/h3>\n

Illuminate\\Auth\\SessionGuard<\/code><\/p>\n

SessionGuard \u662f Laravel web \u8ba4\u8bc1\u9ed8\u8ba4\u7684 guard.<\/p>\n

TokenGuard<\/h3>\n

Illuminate\\Auth\\TokenGuard<\/code><\/p>\n

TokenGuard \u9002\u7528\u4e8e\u65e0\u72b6\u6001 api \u8ba4\u8bc1\uff0c\u901a\u8fc7 token \u8ba4\u8bc1.<\/p>\n

\u5b9e\u73b0\u81ea\u5b9a\u4e49 Guard<\/code><\/h2>\n

App\\Auth\\UserGuard.php<\/code><\/p>\n

<?php\n\nnamespace App\\Auth;\n\nuse Illuminate\\Http\\Request;\nuse Illuminate\\Auth\\GuardHelpers;\nuse Illuminate\\Contracts\\Auth\\Guard;\nuse Illuminate\\Contracts\\Auth\\UserProvider;\n\nclass UserGuard implements Guard\n\n{\n use GuardHelpers;\n\n protected $user = null;\n\n protected $request;\n\n protected $provider;\n\n \/**\n * The name of the query string item from the request containing the API token.\n *\n * @var string\n *\/\n protected $inputKey;\n\n \/**\n * The name of the token \"column\" in persistent storage.\n *\n * @var string\n *\/\n protected $storageKey;\n\n \/**\n * The user we last attempted to retrieve\n * @var\n *\/\n protected $lastAttempted;\n\n \/**\n * UserGuard constructor.\n * @param UserProvider $provider\n * @param Request $request\n * @return void\n *\/\n public function __construct(UserProvider $provider, Request $request = null)\n {\n $this->request = $request;\n $this->provider = $provider;\n $this->inputKey = 'Authorization';\n $this->storageKey = 'api_token';\n }\n\n \/**\n * Get the currently authenticated user.\n * @return \\Illuminate\\Contracts\\Auth\\Authenticatable|null\n *\/\n public function user()\n {\n if(!is_null($this->user)) {\n return $this->user;\n }\n\n $user = null;\n\n $token = $this->getTokenForRequest();\n\n if(!empty($token)) {\n $user = $this->provider->retrieveByCredentials(\n [$this->storageKey => $token]\n );\n }\n\n return $this->user = $user;\n }\n\n \/**\n * Rules a user's credentials.\n * @param array $credentials\n * @return bool\n *\/\n public function validate(array $credentials = [])\n {\n if (empty($credentials[$this->inputKey])) {\n return false;\n }\n\n $credentials = [$this->storageKey => $credentials[$this->inputKey]];\n\n $this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials);\n\n return $this->hasValidCredentials($user, $credentials);\n }\n\n \/**\n * Determine if the user matches the credentials.\n * @param mixed $user\n * @param array $credentials\n * @return bool\n *\/\n protected function hasValidCredentials($user, $credentials)\n {\n return !is_null($user) && $this->provider->validateCredentials($user, $credentials);\n }\n\n \/**\n * Get the token for the current request.\n * @return string\n *\/\n public function getTokenForRequest()\n {\n $token = $this->request->header($this->inputKey);\n\n return $token;\n }\n\n \/**\n * Set the current request instance.\n *\n * @param \\Illuminate\\Http\\Request $request\n * @return $this\n *\/\n public function setRequest(Request $request)\n {\n $this->request = $request;\n\n return $this;\n }\n}\n<\/code><\/pre>\n

\u5728 AppServiceProvider \u7684 boot \u65b9\u6cd5\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\uff1a
App\\Providers\\AuthServiceProvider.php<\/code><\/p>\n

<?php\n.\n.\n.\n\/\/ auth:api -> token provider.\nAuth::provider('token', function() {\n return app(UserProvider::class);\n});\n\n\/\/ auth:api -> token guard.\n\/\/ @throw \\Exception\nAuth::extend('token', function($app, $name, array $config) {\n if($name === 'api') {\n return app()->make(UserGuard::class, [\n 'provider' => Auth::createUserProvider($config['provider']),\n 'request' => $app->request,\n ]);\n }\n throw new \\Exception('This guard only serves \"auth:api\".');\n});\n.\n.\n.<\/code><\/pre>\n
\n

\u5728 config\\auth.php<\/code>\u7684 guards \u6570\u7ec4\u4e2d\u6dfb\u52a0\u81ea\u5b9a\u4e49 guard<\/code>\uff0c\u4e00\u4e2a\u81ea\u5b9a\u4e49 guard \u5305\u62ec\u4e24\u90e8\u5206\uff1a driver<\/code> \u548c provider<\/code>.<\/p>\n<\/li>\n\n

\u8bbe\u7f6e config\\auth.php<\/code> \u7684 defaults.guard \u4e3a api<\/code>.<\/p>\n<\/li>\n<\/ul>\n\n

<?php\n\nreturn [\n\n \/*\n |--------------------------------------------------------------------------\n | Authentication Defaults\n |--------------------------------------------------------------------------\n |\n | This option controls the default authentication \"guard\" and password\n | reset options for your application. You may change these defaults\n | as required, but they're a perfect start for most applications.\n |\n *\/\n\n 'defaults' => [\n 'guard' => 'api',\n 'passwords' => 'users',\n ],\n\n \/*\n |--------------------------------------------------------------------------\n | Authentication Guards\n |--------------------------------------------------------------------------\n |\n | Next, you may define every authentication guard for your application.\n | Of course, a great default configuration has been defined for you\n | here which uses session storage and the Eloquent user provider.\n |\n | All authentication drivers have a user provider. This defines how the\n | users are actually retrieved out of your database or other storage\n | mechanisms used by this application to persist your user's data.\n |\n | Supported: \"session\
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 岁月 设计师:pinMode 返回首页
实付 29.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值