当前位置:网站首页>Laravel8 uses passport login and JWT (generate token)
Laravel8 uses passport login and JWT (generate token)
2022-07-07 08:30:00 【Procedure ape trying to move bricks】
1. install passport plug-in unit
composer require laravel/passport
or
composer require laravel/passport "^9.0"
2. Execute migration file , Generate data table , preservation token data
php artisan migrate
3. Generate client authorization code notes :secret Save up
php artisan passport:install
4. Modify the interface account model
Introduce in the model
use Laravel\Passport\HasApiTokens;
use Illuminate\Foundation\Auth\User as AuthUser;
5. modify config/auth.php In the document api To configure
6. The validity of the token
stay app/Proivders/AuthServiceProvide.php In the document boot Method add validity period
use Laravel\Passport\Passport;
public function boot()
{
$this->registerPolicies();
// token The period of validity of the certification 2 Hours
Passport::tokensExpireIn(now()->addHour(2));
// Refresh token The period of validity of the certification 30 God
Passport::refreshTokensExpireIn(now()->addDays(30));
}
7. solve auth There is no login in the interface attempt problem
Don't forget here
8. To realize the login , And pay attention to , If login is unsuccessful , The data returned conforms to restful standard
9. Interface token Generate
Generate interface validation token Generate after successful login
Attach code for reference
public function login(Request $request)
{
// verification
$validator = Validator::make($request->all(),[
'username'=>'required',
'password'=>'required',
],[
'username.required'=>' Account number cannot be empty ',
'password.required'=>' The password cannot be empty ',
]);
// See if it passes
if ($validator->fails()){
return ['code'=>500,'msg'=>$validator->errors()->first()];
}
// Query login
$bool = auth()->guard('apiweb')->attempt($request->all());
if ($bool){
// Generate token
// Get the user model object
$userModel = auth()->guard('apiweb')->user();
// Determine whether the current user interface exceeds 2000 Time
/* if ($userModel->clicks > 2000){
return ['code'=>500,'msg'=>' The number of visits on that day has reached the maximum '];
} */
// Generate token Save a copy of the server Return a copy to the customer
$token = $userModel->createToken('api')->accessToken;
// Let the current request add 1
// $userModel->increment('clicks');
$data = [
'expire'=>7200,
'token' => $token
];
return ['code'=>200,'msg'=>' Login successful ','data'=>$data];
}else{
return ['code'=>500,'msg'=>' Login failed '];
}
}
Interface test tool Used here postman Measured token
11 . Interface security verification
Used jwt To verify
Bind in the defined route jwt Authentication middleware
test stay Headers in Join in Authorization Content by Bearer + Space + token
边栏推荐
- Opencv learning notes II - basic image operations
- Le système mes est un choix nécessaire pour la production de l'entreprise
- Opencv learning note 3 - image smoothing / denoising
- 归并排序和非比较排序
- Ebpf cilium practice (1) - team based network isolation
- Thirteen forms of lambda in kotlin
- Splunk中single value视图使用将数值替换为文字
- Rsync remote synchronization
- eBPF Cilium实战(2) - 底层网络可观测性
- [paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
猜你喜欢
Vulnerability recurrence easy_ tornado
Openvscode cloud ide joins rainbow integrated development system
利用 Helm 在各类 Kubernetes 中安装 Rainbond
Using nocalhost to develop microservice application on rainbow
Rainbond 5.6 版本发布,增加多种安装方式,优化拓扑图操作体验
AVL平衡二叉搜索树
【雅思口语】安娜口语学习记录 Part2
Rainbow 5.7.1 supports docking with multiple public clouds and clusters for abnormal alarms
Ebpf cilium practice (1) - team based network isolation
BiSeNet的特点
随机推荐
Rsync remote synchronization
【Go ~ 0到1 】 第七天 获取时间戳,时间比较,时间格式转换,Sleep与定时器
National standard gb28181 protocol video platform easygbs adds streaming timeout configuration
机器人教育在动手实践中的真理
Thirteen forms of lambda in kotlin
在Rainbond中一键部署高可用 EMQX 集群
DeiT学习笔记
[kuangbin]专题十五 数位DP
Golang 编译约束/条件编译 ( // +build <tags> )
Snyk 依赖性安全漏洞扫描工具
Rainbow combines neuvector to practice container safety management
Kotlin combines flatmap for filtering and zip merge operators
Improve the delivery efficiency of enterprise products (1) -- one click installation and upgrade of enterprise applications
What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit
2-3查找樹
Application of slip ring of shipborne radar antenna
Rainbond 5.6 版本发布,增加多种安装方式,优化拓扑图操作体验
Don't stop chasing the wind and the moon. Spring mountain is at the end of Pingwu
Understanding of out covariance, in inversion and invariance in kotlin
发挥创客教育空间的广泛实用性