laravel passport
安装
- 安装
composer require laravel/passport
- 执行迁移
php artisan migrate
- 生成postman密钥
php artisan passport:install
- 创建密码客户端
php artisan passport:client --password --name='eleadmin'
Password grant client created successfully. Client ID: 1 Client secret: nagoLf8hjym4iGpaTSeUD7XbEWtLt0ohfKdGD2og
使用
注册路由和配置参数
app/Providers/AuthServiceProvider.php
文件修改
use Laravel\Passport\Passport;
public function boot()
{
.
.
.
// Passport 的路由
Passport::routes();
// access_token 过期时间
Passport::tokensExpireIn(now()->addDays(15));
// refreshTokens 过期时间
Passport::refreshTokensExpireIn(now()->addDays(30));
.
.
.
}
修改用户模型
use Laravel\Passport\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* 通过给定的username获取用户实例
*
* @param string $username
* @return \App\Models\User
*/
public function findForPassport($username)
{
return $this->where('username', $username)->first();
}
}
请求
post /oauth/token
form-data
client_id:1
client_secret:nagoLf8hjym4iGpaTSeUD7XbEWtLt0ohfKdGD2og
grant_type:password
username:test
password:123456