当前位置:网站首页>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
边栏推荐
- Pvtv2--pyramid vision transformer V2 learning notes
- Virtual address space
- Analyzing the influence of robot science and technology development concept on Social Research
- Coquette data completes the cloud native transformation through rainbow to realize offline continuous delivery to customers
- 漏洞複現-Fastjson 反序列化
- Snyk dependency security vulnerability scanning tool
- The single value view in Splunk uses to replace numeric values with text
- 提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
- Lua 编程学习笔记
- 接口作为参数(接口回调)
猜你喜欢

opencv学习笔记一——读取图像的几种方法

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

DeiT学习笔记

In go language, function is a type

Using helm to install rainbow in various kubernetes

What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit

Coquette data completes the cloud native transformation through rainbow to realize offline continuous delivery to customers

Opencv learning note 3 - image smoothing / denoising

Rainbond结合NeuVector实践容器安全管理

Famine cloud service management script
随机推荐
A method for quickly viewing pod logs under frequent tests (grep awk xargs kuberctl)
Low success rate of unit test report
One click installation of highly available Nacos clusters in rainbow
[hard core science popularization] working principle of dynamic loop monitoring system
Rainbow version 5.6 was released, adding a variety of installation methods and optimizing the topology operation experience
It's too true. There's a reason why I haven't been rich
The use of generics and vararg variable parameters in kotlin
下载和安装orcale database11.2.0.4
Go语言中,函数是一种类型
CCTV is so warm-hearted that it teaches you to write HR's favorite resume hand in hand
Improve the delivery efficiency of enterprise products (1) -- one click installation and upgrade of enterprise applications
The field value in Splunk subquery fuzzy matching CSV is*
Infix keyword infix expression and the use of generic extension function in kotlin
漏洞複現-Fastjson 反序列化
Using helm to install rainbow in various kubernetes
Practice of combining rook CEPH and rainbow, a cloud native storage solution
The truth of robot education in hands-on practice
PVTV2--Pyramid Vision TransformerV2学习笔记
2-3 lookup tree
MES系统,是企业生产的必要选择