当前位置:网站首页>Laravel8 implements interface authentication encapsulation using JWT
Laravel8 implements interface authentication encapsulation using JWT
2022-07-26 03:54:00 【Chenqing Nuo language】
1. download composer Dependency package
composer require firebase/php-jwt2. stay App New under file Server Folder , newly build JwtServer class
<?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. Create middleware
php artisan make:middleware CheckApiLogin4. Write... In the middleware , take token Put in header Transmission of
public function handle(Request $request, Closure $next)
{
// take token Put in header Transmission of
$token=$request->header('token');
if (!$token){
return response()->json(['code'=>500,'msg'=>'token non-existent ','data'=>[]]);
}
$userId=JwtServer::decodeToken($token);
if ($userId){
// Encrypt id Decrypt incoming next step
$request->attributes->add(['user_id'=>$userId->user_id]);
}else{
return response()->json(['code'=>500,'msg'=>'token Be overdue ','data'=>[]]);
}
return $next($request);
}5. Register middleware
'checkApiLogin' => \App\Http\Middleware\CheckApiLogin::class6. Use middleware in routing
Route::group(['middleware'=>'checkApiLogin'],function (){
});
// Generate token
Route::get('createtoken',[Token::class,'createtoken']);
// obtain token Medium userId
Route::get('decodeToken',[Token::class,'decodeToken']);
Route::group(['middleware'=>'checkApiLogin'],function (){
Route::get('Token',[Token::class,'Token']);
});7. To test
// Generate token
public function createtoken(){
$userId = request()->get('userId');
$token = JwtServer::createToken($userId);
Redis::setex('token',120,$token);
dd($token);
}
// obtain 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(' I only have a rose in my pocket , This trip is high and far away .');
}边栏推荐
- Find my technology | the Internet of things asset tracking market has reached US $6.6 billion, and find my helps the market develop
- Dracoo master
- 深度学习之SuperViT
- waf详解
- Asemi rectifier bridge gbu1510 parameters, gbu1510 specifications, gbu1510 package
- PHP 对象转换数组
- [create interactive dice roller application]
- Chapter 18: explore the wonders of the mean in the 2-bit a~b system, specify the 3x+1 conversion process of integers, specify an interval to verify the angular Valley conjecture, explore the number of
- ACM mm 2022 | end to end multi granularity comparative learning for video text retrieval
- The convolution kernel is expanded to 51x51, and the new CNN architecture slak counterattacks the transformer
猜你喜欢

【云原生】谈谈老牌消息中间件ActiveMQ的理解

Can't the container run? The Internet doesn't have to carry the blame

UFS Clk Gate介绍

Data elements

Uncaught TypeError: $(...).onmouseenter is not a function js错误,解决办法:

Worked overtime for a week to develop a reporting system. This low code free it reporting artifact is very easy to use

资深报表开发经验总结:明白这一点,没有做不好的报表

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

File upload error: current request is not a multipart request

Introduction to UFS CLK gate
随机推荐
redux
全校软硬件基础设施一站式监控 ,苏州大学以时序数据库替换 PostgreSQL
三种解决:在点击用户退出登录时,再按浏览器的后退按钮,仍然能够看到之前登录的页面。
深度学习之DAT
Brief tutorial for soft exam system architecture designer | case analysis and problem solving skills
How to choose sentinel vs hystrix?
Bond network mode configuration
[MCU simulation project] external interrupt 0 controls 8 LED flashes
php 实现从1累加到100的算法
电商运营小白,如何快速入门学习数据分析?
1311_硬件设计_ICT概念、应用以及优缺点学习小结
Zkevm: summary of zkevm and L1 by Mina's CEO
中国数据库 OceanBase 入选 Forrester Translytical 数据平台报告
软考 系统架构设计师 简明教程 | 案例分析解题技巧
redux
Chinese database oceanbase was selected into the Forrester translational data platform report
cpu和gpu已过时,npu和apu的时代开始
Data elements
【云原生】谈谈老牌消息中间件ActiveMQ的理解
oracle 11g “密码延迟验证”特性