当前位置:网站首页>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
边栏推荐
- The single value view in Splunk uses to replace numeric values with text
- Kotlin combines flatmap for filtering and zip merge operators
- 一文了解如何源码编译Rainbond基础组件
- Opencv learning notes 1 -- several methods of reading images
- Lua programming learning notes
- 如何理解分布式架构和微服务架构呢
- PVTV2--Pyramid Vision TransformerV2学习笔记
- 国标GB28181协议视频平台EasyGBS新增拉流超时配置
- 探索STEAM艺术设计中的创造力
- Vulnerability recurrence fastjson deserialization
猜你喜欢
一种适用于应用频繁测试下快速查看Pod的日志的方法(grep awk xargs kuberctl)
Wang Zijian: is the NFT of Tencent magic core worth buying?
The single value view in Splunk uses to replace numeric values with text
Practice of combining rook CEPH and rainbow, a cloud native storage solution
Pvtv2--pyramid vision transformer V2 learning notes
2-3查找樹
Improve the delivery efficiency of enterprise products (1) -- one click installation and upgrade of enterprise applications
Analysis of maker education in innovative education system
在 Rainbond 中一键安装高可用 Nacos 集群
Opencv learning notes II - basic image operations
随机推荐
Several ways of lambda used in functions in kotlin (higher-order functions)
Open3d ISS key points
Coquette data completes the cloud native transformation through rainbow to realize offline continuous delivery to customers
Using helm to install rainbow in various kubernetes
Lua programming learning notes
使用BiSeNet实现自己的数据集
What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit
Give full play to the wide practicality of maker education space
National standard gb28181 protocol video platform easygbs adds streaming timeout configuration
Infix keyword infix expression and the use of generic extension function in kotlin
2-3 lookup tree
Interface as a parameter (interface callback)
Analysis of maker education in innovative education system
Ebpf cilium practice (2) - underlying network observability
The reified keyword in kotlin is used for generics
Basic use of CTF web shrink template injection nmap
Merge sort and non comparison sort
Xcit learning notes
[quick start of Digital IC Verification] 14. Basic syntax of SystemVerilog learning 1 (array, queue, structure, enumeration, string... Including practical exercises)
Rsync remote synchronization