desmart / jwt-auth
为 Laravel 简单实现的 JWT 身份验证。
1.0.4
2017-11-24 09:52 UTC
Requires
- php: >=7.0.0
- illuminate/database: 5.4.*
- illuminate/http: 5.4.*
- illuminate/support: 5.4.*
- lcobucci/jwt: ^3.2
Requires (Dev)
- phpunit/phpunit: ~5.0
- squizlabs/php_codesniffer: ^2.7
This package is not auto-updated.
Last update: 2024-09-15 01:11:42 UTC
README
为 Laravel 简单实现的 JWT 身份验证。
安装
使用 Composer 安装包
composer require desmart/jwt-auth
在 config/app.php
中注册包的服务提供者
'providers' => [
(...)
DeSmart\JWTAuth\ServiceProvider::class,
],
配置
该包附带一个配置文件。为了发布它,请运行以下命令
php artisan vendor:publish
配置文件允许您更改一些选项。请确保检查它。
用法
该包允许
- 验证用户身份,
- 使用路由中间件验证用户是否已验证。
用户身份验证
首先,将 TokenRefreshMiddleware
添加为全局中间件或中间件组(在 app/Http/Kernel.php
中)。它将为响应添加 Authorization
头。此头将包含 JWT 令牌,在成功验证后。
然后,将 \DeSmart\JWTAuth\Auth\Guard
注入到您的身份验证类中并验证用户(凭证验证取决于您)。
public function authenticateUser(\DeSmart\JWTAuth\Auth\Guard $auth, $user) {
if (true === $this->validateCredentails($user)) {
$auth->loginUser($user);
}
}
令牌验证
一旦用户已验证,对您应用程序的每次请求都应包含在成功验证后获得的令牌的 Authorization
头。
将 AuthMiddleware
添加到您的 $routeMiddleware
数组(在 app/Http/Kernel.php
中)
protected $routeMiddleware = [
(...)
'auth' => \DeSmart\JWTAuth\Middleware\AuthMiddleware::class,
];
现在,只需使用 auth
路由中间件来检查用户是否已验证。