• laravel框架基础建设:接口验签封装


    我们提供接口给到外部(后端)调用的时候,为保证接口安全,需要在接口中进行验签校验(目前验签规则很多种,具体根据每个公司而定)
    1.创建中间件:php artisan make:middleware ApiCheckSign.php,Kernel.php添加中间件
    2.实现

    
    
    namespace App\Http\Middleware;
    
    use App\common\SystemCode;
    use App\common\SystemMessage;
    use App\common\tools\response\RespResult;
    use Closure;
    use Illuminate\Http\Request;
    
    class ApiCheckSign
    {
        /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
         * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
         */
        public function handle(Request $request, Closure $next)
        {
            $params = $request->all();
            $token = $request->header("token");
            $timeStamp = $request->header("timestamp");
            $appId = $request->header("appId");
            if (empty($token) || empty($timeStamp) || empty($appId)){
                throw new \Exception(SystemMessage::SYSTEM_ERROR_API_PARAMS_NULL,SystemCode::SYSTEM_ERROR_API_PARAMS_NULL);//测试抛出异常
            }
            ksort($params);//将参数进行排序
            $paramsJson = json_encode($params,256);
            $appSecrect = function () use ($appId){//通过appid动态获取相关appSecrect
                $data = [//模拟通过数据库用appid获取appSecrect
                    "1"=>"0a0ed22b8d8e5de466fca2e2e6d33137",
                    "2"=>"0a0ed22b8d8e5de466fca2e2e6d33137",
                    "3"=>"0a0ed22b8d8e5de466fca2e2e6d33137",
                    "4"=>"0a0ed22b8d8e5de466fca2e2e6d33137",
                    "5"=>"0a0ed22b8d8e5de466fca2e2e6d33137",
                ];
                $appSecrect = isset($data[$appId])?$data[$appId]:'';
                if ($appSecrect === ''){
                    throw new \Exception(SystemMessage::SYSTEM_ERROR_APPID,SystemCode::SYSTEM_ERROR_APPID);//测试抛出异常
                }
                return $data[$appId];
            };
            $checkToken = md5($appId.$appSecrect().$paramsJson.$timeStamp);//签名
            if($token != $checkToken){
                return RespResult::result(SystemCode::SYSTEM_ERROR_TOKEN_VERIF_FAIL,SystemMessage::SYSTEM_ERROR_TOKEN_VERIF_FAIL,[]);
            }
            //验签通过
            return $next($request);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    3.使用方法

    Route::patch('demo/emailFile',[\App\Http\Controllers\DemoController::class,'emailFile'])->middleware("Validate")->middleware("ApiCheckSign");
    
    • 1
  • 相关阅读:
    Coursera自然语言处理专项课程04:Natural Language Processing with Attention Models笔记 Week02
    vulnhub blogger: 1
    使用dbeaver连接GaussDB数据库(集中式)
    Redis教程
    JVM优化之垃圾收集底层算法实现
    Strimzi Kafka Bridge(桥接)实战之三:自制sdk(golang版本)
    【开发工具】使用瑞萨CS+ for CC创建lib和使用lib
    文档在线预览(三)将word、txt、ppt、excel、图片转成pdf来实现在线预览
    指针和数组笔试题讲解(3)
    Docker(12)CIG容器重量级监控系统
  • 原文地址:https://blog.csdn.net/wangtianyou343/article/details/126935747