kino / laravel-jwt
为 Laravel 5.4+ 提供的简单JWT身份验证提供程序
Requires
- php: >=7.0.0
- ext-openssl: *
- illuminate/support: >=5.4.0
- lcobucci/jwt: ^3.2
This package is not auto-updated.
Last update: 2024-09-20 20:21:16 UTC
README
此软件包为 Laravel 提供了使用 JWT 进行开箱即用的 API 身份验证。
安装。
您可以通过运行以下命令来安装此软件包:
composer require kino/laravel-jwt
设置。
为了将此软件包设置到您的应用程序中,实际上只需要进行最小配置。
1) 服务提供者。
将此软件包的 Service Provider 添加到您的 config/app.php
文件的 providers
部分
'providers' => [ // ... other providers omitted Kino\Auth\JWT\ServiceProvider::class, ],
2) 配置文件。
在注册 Service Provider 后,通过运行以下命令发布配置文件(config/jwt.php
)
php artisan vendor:publish --provider="Kino\Auth\JWT\ServiceProvider"
3) 生成密钥。
为了使此软件包正常工作,您需要单独的密钥(不要使用应用程序密钥)。
此软件包提供了一个可以用于生成强大密钥的命令。
通过运行以下命令获取新的密钥:
php artisan jwt:generate
然后,将生成的密钥内容复制到您的 .env
文件中。
注意:密钥生成过程不会自动将其设置在您的 .env
文件中,请手动操作。
4) 设置守卫。
为了自动使用 JWT 令牌验证您的路由,您需要将守卫驱动程序更改为 jwt
在 config/auth.php
中设置您想要保护的相应守卫组
如果您有名为 api
的默认守卫组,则您的 auth.php
应该如下所示:
'guards' => [ // ... other guards omitted. 'api' => [ 'driver' => 'jwt', // this is the line you need to change. 'provider' => 'users', ], ],
这样,我们就准备好使用它了。
使用。
此软件包旨在使用起来非常简单。
以下模板可用于设置您现有的身份验证控制器和资源。
注意:此软件包的完整使用示例将在达到 1.0 版本时添加到此软件包中。
保护路由。
此软件包完全集成到 Laravel 身份验证中。
默认配置(config/jwt.php
)提供了一个非常有用的敏感值:'middleware_match'
通过“不是完全的 API”,我的意思是 JWT 守卫不是默认的。
在这种情况下,为了使用 auth
中间件,必须将配置键 middleware_match
设置为 true。
此配置键允许非受保护的路由正常工作。
请注意,此选项将匹配中间件组名称与守卫名称。
在这种情况下,'api' 中间件组将始终使用
api
守卫。同样,'web' 中间件组将始终使用web
守卫。
如果您不使用此值,则需要在使用 auth
中间件时使用后缀,例如 auth:api
。
发布和续订令牌。
对于发布令牌,实际上不需要特殊的类,您只需从 IoC 创建 Guard 当前实现并从中工作即可。
查看示例。
** 在以下示例中,所有 Guard 实例都是从 Illuminate\Contracts\Auth\Guard
注入的 ** ** 在以下示例中,所有 Request 实例都是从 Illuminate\Http\Request
注入的 **
用户实例的令牌。
应在此方法注册用户和其他特殊情况下使用。
public function tokenFromUser(Guard $auth) { // generating a token from a given user. $user = SomeUserModel::find(12); // logs in the user $auth->login($user); // get and return a new token $token = $auth->issue(); return $token; }
凭据的令牌。
应在此方法注册用户和其他特殊情况下使用。
public function tokenFromCredentials(Guard $auth, Request $request) { // get some credentials $credentials = $request->only(['email', 'password']); if ($auth->attempt($credentials)) { return $token = $auth->issue(); } return ['Invalid Credentials']; }
刷新令牌。
令牌可以通过两种方式刷新:自动检测或手动。
如果你没有将任何参数传入refresh方法,Guard将会在请求的头部或请求体中的token
字段上查找Authorization
头部。
public function refreshToken(Guard $auth) { // auto detecting token from request. $token = $auth->refresh(); // manually passing the token to be refreshed. $token = $auth->refresh($oldToken); return $token; }
自定义声明。
当然,支持自定义声明。
你可以通过两种方式来设置它们。
通过显式传递。
$customClaims = [ 'custom1' => 'value1', 'custom2' => 'value2', ]; // when issuing $auth->issue($customClaims); // when refreshing // custom claims are the second parameter as the first one is the // old token $auth->refresh(null, $customClaims);
通过Authenticatable方法。
如果你的所有用户都将具有相同的自定义声明,你可以在User模型(或你使用的任何其他Authenticatable)上设置默认自定义声明方法。
如果模型中存在customJWTClaims()
方法,这些声明将被自动包含在内。
class User extends Model implements Authenticatable { public function customJWTClaims() { return [ 'email' => $this->email, 'name' => $this->name, ]; } }