workman服务端开发模式-应用开发-gateway的onWebSocketConnect开发

一、onWebSocketConnect安全开发一

为什么叫安全开发一,是因为还有两种情况没有考虑好,需要实测中进行修改。第一种情况:个人如果打开两个窗口,访问同一个系统或个人如果打开两种浏览器,两个窗口同时访问一个系统,改怎么解决。第二种情况:如果有人用工具的情况下,怎么才能断开连接。安全一的代码如下:

复制代码
    /**
     * 专门处理安全的
     * User: 龙哥·三年风水
     * Date: 2024/12/16
     * Time: 17:03
     * @ param $client_id
     * @ param $data
     */
    public static function onWebSocketConnect($client_id,$data){
        // 没有指定参数的情况下
        if(!isset($data['get']['token']) || !isset($data['get']['extremity_type']) || !isset($data['get']['login_time'])){
            //释放当前连接
            Gateway::destoryClient($client_id);
            exit;
        }
        $token = $data['get']['token'];
        $extremityType = $data['get']['extremity_type'];
        $loginTime = $data['get']['login_time'];
        // 有参数且参数值不合法的情况下
        if(empty($token) || !in_array($extremityType,[1,2,3]) || preg_match('/^\d{10}$/', $loginTime) == false){
            //释放当前连接
            Gateway::destoryClient($client_id);
            exit;
        }
        // 读取Redis配置及Redis连接
        $config = parse_ini_file('conf.ini', true);
        $redis = new Client("redis://{$config['redis']['host']}:{$config['redis']['port']}");
        $redis->auth($config['redis']['password']);
        // 平台端验证
        if($extremityType == 1){
            $redis->select((int)$config['redis']['login_ip']);
            // 验证IP是否合法
            $redis->get($token,function($clientIp) use ($client_id,$data){
                if(empty($clientIp)){
                    //释放当前连接
                    Gateway::destoryClient($client_id);
                    exit;
                }
                if($clientIp != $data['server']['HTTP_X_REAL_IP']){
                    //释放当前连接
                    Gateway::destoryClient($client_id);
                    exit;
                }
            });
            // 验证是否登录
            $redis->select((int)$config['redis']['token_platform']);
            $authorization = sha1(sha1($token).$loginTime);
            $redis->get('token_'.$authorization,function ($tokenValue) use ($client_id,$config){
                if(empty($tokenValue)){
                    //释放当前连接
                    Gateway::destoryClient($client_id);
                    exit;
                }
                Gateway::bindUid($client_id,$tokenValue);
                Gateway::joinGroup($client_id,'platform'.$config['redis']['token_platform']);
            });

        }
    }

二、提前说明

明天将完成流程图及规则说明后再次开发websocket安全。

相关推荐
钺商科技21 分钟前
【6.1.2 漫画分布式事务技术选型】
分布式
帅次40 分钟前
系统分析师-计算机系统-输入输出系统
人工智能·分布式·深度学习·神经网络·架构·系统架构·硬件架构
Shartin2 小时前
Can201-Introduction to Networking: Application Layer应用层
服务器·开发语言·php
乌云暮年3 小时前
Linux常用命令
linux·服务器·ssh·php
钺商科技5 小时前
【6.1.3 漫画分布式锁】
分布式
apihz5 小时前
通用图片搜索-搜狗源免费API接口使用指南
android·java·python·php·音视频
博睿谷IT99_5 小时前
华为数据通信网络基础
开发语言·华为·php·华为认证
亲爱的非洲野猪9 小时前
Spring Cloud Gateway介绍 - -基础概念,简单工作原理和配置示例
java·spring boot·gateway
阿俊-全栈开发17 小时前
crmeb多门店对接拉卡拉支付小程序聚合收银台集成全流程详解
小程序·php·拉卡拉聚合收银台·拉卡拉三方支付
Bug退退退1231 天前
RabbitMQ 之消息积压
分布式·rabbitmq