当前位置:网站首页>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('我口袋只剩玫瑰一片,此行山高又路远。');
}边栏推荐
- Dat of deep learning
- [mathematical modeling - Summary of planning model] | matlab solution
- 网络模型及协议
- tf.truncated_ Normal() usage
- 全校软硬件基础设施一站式监控 ,苏州大学以时序数据库替换 PostgreSQL
- Bracket nesting problem (recommended Collection)
- php 实现从1累加到100的算法
- cpu和gpu已过时,npu和apu的时代开始
- IDEA2020.3.1不能打开(双击不能打开),但可以通过idea.bat打开。
- Ali II: how to quickly query a table with tens of millions of data?
猜你喜欢

括号嵌套问题(建议收藏)

Portable power fast charging scheme 30W automatic pressure rise and fall PD fast charging

Sentinel vs Hystrix 到底怎么选?

Dat of deep learning

第十八章:2位a~b进制中均位奇观探索,指定整数的 3x+1 转化过程,指定区间验证角谷猜想,探求4份黑洞数,验证3位黑洞数

基于SSM选课信息管理系统

Realization of online shopping mall system based on JSP

想要做好软件测试,可以先了解AST、SCA和渗透测试

【数学建模-规划模型总结】| MATLAB求解

在 Istio 服务网格内连接外部 MySQL 数据库
随机推荐
【程序员必备】七夕表白攻略:”月遇从云,花遇和风,晚上的夜空很美“。(附源码合集)
Where can Lora and nb-iot be used
Kbpc1510-asemi large chip 15A rectifier bridge kbpc1510
Apply for SSL certificate, configure SSL certificate for domain name, and deploy server; Download and installation of SSL certificate
6-40v input fixed 5V 3.3V output 1.1a current 23-5 package
leetcode-462.最少移动次数使数组元素相等
深度学习之DAT
电商运营小白,如何快速入门学习数据分析?
Basic line chart: the most intuitive presentation of data trends and changes
在 Istio 服务网格内连接外部 MySQL 数据库
Six years of automated testing from scratch, I don't regret turning development to testing
Moco V2: further upgrade of Moco series
Zkevm: summary of zkevm and L1 by Mina's CEO
测试工作不受重视?学长:你应该换位思考
waf详解
IDEA2020.3.1不能打开(双击不能打开),但可以通过idea.bat打开。
Bond network mode configuration
Derivation of linear regression principle
Dat of deep learning
Portable power fast charging scheme 30W automatic pressure rise and fall PD fast charging