当前位置:网站首页>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 migrate3. Generate client authorization code notes :secret Save up
php artisan passport:install4. 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
边栏推荐
- Automatic upgrading of database structure in rainbow
- 数据中台落地实施之法
- Deit learning notes
- JS copy picture to clipboard read clipboard
- Interpreting the practical application of maker thinking and mathematics curriculum
- SSM 整合
- 解析机器人科技发展观对社会研究论
- 【雅思口语】安娜口语学习记录 Part2
- Implementation method of data platform landing
- Low success rate of unit test report
猜你喜欢
随机推荐
Interface as a parameter (interface callback)
Rainbow version 5.6 was released, adding a variety of installation methods and optimizing the topology operation experience
国标GB28181协议视频平台EasyGBS新增拉流超时配置
BiSeNet的特点
Implementation method of data platform landing
如何理解分布式架构和微服务架构呢
Opencv learning notes 1 -- several methods of reading images
Interview questions (CAS)
机器人教育在动手实践中的真理
Through the "last mile" of legal services for the masses, fangzheng Puhua labor and personnel law self-service consulting service platform has been frequently "praised"
OpenVSCode云端IDE加入Rainbond一体化开发体系
Opencv learning note 5 - gradient calculation / edge detection
SSM 整合
【雅思口语】安娜口语学习记录 Part3
在Rainbond中实现数据库结构自动化升级
Analyzing the influence of robot science and technology development concept on Social Research
2-3查找樹
Grpc, oauth2, OpenSSL, two-way authentication, one-way authentication and other column directories
Iptables' state module (FTP service exercise)
Lua 编程学习笔记









