当前位置:网站首页>laravel8 实现接口鉴权封装使用JWT
laravel8 实现接口鉴权封装使用JWT
2022-07-26 03:49:00 【陈卿诺语】
1.下载composer依赖包
composer require firebase/php-jwt2.在App文件下新建Server文件夹,新建JwtServer类
<?php
namespace App\Server;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
class JwtServer
{
public static function createToken($userId)
{
$key = "example_key";
$payload = array(
"iss" => "http://example.org",
"aud" => "http://example.com",
"iat" => 1356999524,
"nbf" => 1357000000,
'user_id'=>$userId
);
$jwt = JWT::encode($payload, $key, 'HS256');
return $jwt;
}
public static function decodeToken($jwt){
$key = "example_key";
$decoded = JWT::decode($jwt, new Key($key, 'HS256'));
if ($decoded){
return $decoded;
}else{
return false;
}
}
}3.创建中间件
php artisan make:middleware CheckApiLogin4.在中间件中写入 ,将token放入header中传输
public function handle(Request $request, Closure $next)
{
//将token放入header中传输
$token=$request->header('token');
if (!$token){
return response()->json(['code'=>500,'msg'=>'token不存在','data'=>[]]);
}
$userId=JwtServer::decodeToken($token);
if ($userId){
//将加密id解密传入下一步
$request->attributes->add(['user_id'=>$userId->user_id]);
}else{
return response()->json(['code'=>500,'msg'=>'token过期','data'=>[]]);
}
return $next($request);
}5.注册中间件
'checkApiLogin' => \App\Http\Middleware\CheckApiLogin::class6.在路由中使用中间件
Route::group(['middleware'=>'checkApiLogin'],function (){
});
//生成token
Route::get('createtoken',[Token::class,'createtoken']);
//获取token中的userId
Route::get('decodeToken',[Token::class,'decodeToken']);
Route::group(['middleware'=>'checkApiLogin'],function (){
Route::get('Token',[Token::class,'Token']);
});7.进行检测
//生成token
public function createtoken(){
$userId = request()->get('userId');
$token = JwtServer::createToken($userId);
Redis::setex('token',120,$token);
dd($token);
}
//获取id
public function decodeToken()
{
$token = Redis::get('token');
$userId = JwtServer::decodeToken($token);
$userId = json_decode(json_encode($userId),true);
dd($userId['user_id']);
}
public function Token(){
dd('我口袋只剩玫瑰一片,此行山高又路远。');
}边栏推荐
- Multi merchant mall system function disassembly lecture 15 - platform side member label
- Tactile intelligent sharing-rk3568 application in scenic spot navigation robot
- Asemi rectifier bridge gbu1510 parameters, gbu1510 specifications, gbu1510 package
- Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network
- 让百度收录,爬虫自己网站
- [stl] priority queue priority_ queue
- How Lora wireless gateway can quickly realize end-to-cloud transmission
- 9-20v input peak charging current 3A dual lithium switch type charging chip sc7102
- Navicat connects to MySQL database on Cloud Server
- Leetcode-462. make the array elements equal with the minimum number of moves
猜你喜欢

Offline data warehouse from 0 to 1 - phase I resource purchase configuration

booking.com缤客上海面经
![[unity3d shader] character projection and reflection](/img/00/d0d994d88475ea590dc5cb60a6ad65.png)
[unity3d shader] character projection and reflection

Tactile intelligent sharing-rk3568 application in scenic spot navigation robot

Offline data warehouse from 0 to 1-stage II software installation

Sersync/lsync real-time synchronization
![[stl] priority queue priority_ queue](/img/79/d13913cbb9d98f936a9501633b38bf.png)
[stl] priority queue priority_ queue

《opencv学习笔记》-- 重映射

Multi merchant mall system function disassembly lecture 15 - platform side member label

【数学建模-规划模型总结】| MATLAB求解
随机推荐
[class and object instances in kotlin]
Use VRRP technology to realize gateway equipment redundancy, with detailed configuration experiments
Realization of online shopping mall system based on JSP
研发了 5 年的时序数据库,到底要解决什么问题?
sersync/lsync实时同步
Leetcode-202. happy number
booking.com缤客上海面经
2020 AF-RCNN: An anchor-free convolutional neural network for multi-categoriesagricultural pest det
File upload error: current request is not a multipart request
C language preprocessing instructions and makefile script explanation
微信小程序实现音乐播放器(4)(使用pubsubjs实现页面间通信)
Ali II: how to quickly query a table with tens of millions of data?
Three ways of redis cluster
赶紧进来!!!用c语言基础知识几十行代码写一个猜数字小游戏
Visio: how do Gantt charts merge cells? Solution: overwrite cells
[stl] priority queue priority_ queue
【单片机仿真项目】外部中断0控制8个发光二极管闪烁
General test case writing specification
Basic line chart: the most intuitive presentation of data trends and changes
UFS Clk Gate介绍