当前位置:网站首页>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
边栏推荐
- 在 Rainbond 中一键安装高可用 Nacos 集群
- Rainbow combines neuvector to practice container safety management
- One click installation of highly available Nacos clusters in rainbow
- How to realize the high temperature alarm of the machine room in the moving ring monitoring system
- eBPF Cilium实战(1) - 基于团队的网络隔离
- 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"
- MES system is a necessary choice for enterprise production
- [quick start of Digital IC Verification] 10. Verilog RTL design must know FIFO
- Snyk 依赖性安全漏洞扫描工具
- Standard function let and generic extension function in kotlin
猜你喜欢

Battery and motor technology have received great attention, but electric control technology is rarely mentioned?

Input of mathematical formula of obsidan

Practice of combining rook CEPH and rainbow, a cloud native storage solution

在Rainbond中一键部署高可用 EMQX 集群

Learn how to compile basic components of rainbow from the source code

2 - 3 arbre de recherche

Réplication de vulnérabilité - désrialisation fastjson

探索STEAM艺术设计中的创造力
![[quick start of Digital IC Verification] 13. SystemVerilog interface and program learning](/img/d8/ffc1b7527f0269cecb2946ab402a2e.png)
[quick start of Digital IC Verification] 13. SystemVerilog interface and program learning

Merge sort and non comparison sort
随机推荐
OpenVSCode云端IDE加入Rainbond一体化开发体系
Learn how to compile basic components of rainbow from the source code
Pvtv2--pyramid vision transformer V2 learning notes
rsync远程同步
Open3d ISS key points
[quick start of Digital IC Verification] 14. Basic syntax of SystemVerilog learning 1 (array, queue, structure, enumeration, string... Including practical exercises)
[paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
Analyzing the influence of robot science and technology development concept on Social Research
如何理解分布式架构和微服务架构呢
Lua programming learning notes
Wang Zijian: is the NFT of Tencent magic core worth buying?
Iptables' state module (FTP service exercise)
Merge sort and non comparison sort
Application of slip ring of shipborne radar antenna
Ebpf cilium practice (1) - team based network isolation
Interpreting the practical application of maker thinking and mathematics curriculum
【无标题】
[hard core science popularization] working principle of dynamic loop monitoring system
MES系統,是企業生產的必要選擇
【Go ~ 0到1 】 第七天 获取时间戳,时间比较,时间格式转换,Sleep与定时器