Laravel 安装
通过 composer 安装
运行以下命令安装最新版本:
composer require tymon/jwt-auth
如果遇到类似下面的错误:
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Root composer.json requires tymon/jwt-auth ^0.5.12 -> satisfiable by tymon/jwt-auth[0.5.12].
- tymon/jwt-auth 0.5.12 requires illuminate/support ~5.0 -> found illuminate/support[v5.0.0, ..., 5.8.x-dev] but these were not loaded, likely because it conflicts with another require.
You can also try re-running composer require with an explicit version constraint, e.g. "composer require tymon/jwt-auth:*" to figure out if any version is installable, or "composer require tymon/jwt-auth:^2.1" if you know which you need.
Installation failed, reverting ./composer.json and ./composer.lock to their original content.
请运行以下命令安装 1.0
版本:
composer require tymon/jwt-auth "^1.0"
添加服务提供者(Laravel 5.4 或以下)
将服务提供者添加到 config/app.php
配置文件的 providers
数组中,如下所示:
'providers' => [
...
Tymon\JWTAuth\Providers\LaravelServiceProvider::class,
]
发布配置文件
运行以下命令发布包配置文件:
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"
您现在应该有一个 config/jwt.php
文件,它允许您配置这个包的基础。
生成密钥
我自带了一个助手命令来帮您生成密钥:
php artisan jwt:secret
这将使用 JWT_SECRET=foobar
类似的内容来更新您的 .env
文件。
这将用于签署您 token 的密钥。具体如何发生取决于您选择使用的算法。