codecasts / laravel-jwt
为Laravel 5.4+提供最简单的JWT身份验证提供商
Requires
- php: >=7.0.0
- ext-openssl: *
- illuminate/support: >=5.4.0
- lcobucci/jwt: ^3.2
Requires (Dev)
- mockery/mockery: ^0.9.9
- orchestra/testbench: ^3.4
- phpunit/phpunit: ^6.1
README
Laravel JWT
此包为Laravel提供开箱即用的JWT API身份验证。
安装。
您可以通过运行以下命令来安装此包:
composer require codecasts/laravel-jwt
设置。
为了将此包设置到您的应用程序中,实际上只需要最少的配置。
1) 服务提供者。
将此包的服务提供者添加到您的config/app.php
文件中的providers
部分以注册它
由于Laravel 5.5的自动发现包功能,您可以跳过此步骤。
'providers' => [ // ... other providers omitted Codecasts\Auth\JWT\ServiceProvider::class, ],
2) 配置文件。
注册服务提供者后,运行以下命令发布配置文件(config/jwt.php
)。
php artisan vendor:publish --provider="Codecasts\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
)提供了一个非常有用的敏感值,当您的应用程序不是完全的API时: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']; }
刷新令牌。
令牌可以通过两种方式刷新:自动检测或手动。
如果您没有向刷新方法传递任何参数,守卫将查找请求体中的 Authorization
标头或 token
字段。
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);
通过可验证方法。
如果您所有用户都将具有相同的自定义声明,您可以在您的用户模型(或您正在使用的任何可验证模型)上设置默认自定义声明方法。
如果模型上存在 customJWTClaims()
方法,则这些声明将被自动包含在内。
class User extends Model implements Authenticatable { public function customJWTClaims() { return [ 'email' => $this->email, 'name' => $this->name, ]; } }
贡献
有关详细信息,请参阅 CONTRIBUTING。