当前位置:网站首页>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 .');
}边栏推荐
- UFS Clk Gate介绍
- Uncaught TypeError: $(...). Onmousenter is not a function JS error, solution:
- SDL2 Opengl遇到的坑
- 6年从零开始的自动化测试之路,开发转测试我不后悔...
- In PHP, you can use the abs() function to turn negative numbers into positive numbers
- 中国数据库 OceanBase 入选 Forrester Translytical 数据平台报告
- Why are more and more users of Bing search?
- The B2B2C multi merchant system has rich functions and is very easy to open
- Idea2020.3.1 cannot be opened (double click cannot be opened), but it can be opened through idea.bat.
- 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
猜你喜欢

booking.com缤客上海面经

Dat of deep learning

《opencv学习笔记》-- 霍夫变换

Idea2020.3.1 cannot be opened (double click cannot be opened), but it can be opened through idea.bat.

用GaussDB(for Redis)存画像,推荐业务轻松降本60%

Testing is not valued? Senior: you should think in another position

Bond network mode configuration

Why are more and more users of Bing search?

基于JSP实现网上商城系统

cpu和gpu已过时,npu和apu的时代开始
随机推荐
Div setting height does not take effect
PHP implements the algorithm of adding from 1 to 100
php 查找 session 存储文件位置的方法
redux
Three solutions: when clicking the user to exit the login, press the back button of the browser, and you can still see the previous login page.
WAF details
Chinese database oceanbase was selected into the Forrester translational data platform report
The PHP Eval () function can run a string as PHP code
Connect external MySQL databases in istio Service Grid
Summary of senior report development experience: understand this and do not make bad reports
booking.com缤客上海面经
MySQL index failure scenarios and Solutions
PHP method to find the location of session storage file
IDEA2020.3.1不能打开(双击不能打开),但可以通过idea.bat打开。
Realization of online shopping mall system based on JSP
【单片机仿真项目】外部中断0控制8个发光二极管闪烁
Aike AI frontier promotion (7.18)
Three ways of redis cluster
基于移位寄存器的同步FIFO
In PHP, you can use the abs() function to turn negative numbers into positive numbers