[PHP高可用后端]②⑥--sign有效时间处理

[PHP高可用后端]②⑥--sign有效时间处理_第1张图片
image.png

app.php

 '_#sing_ty',//密码加密言
    'aeskey' => 'sgg45747ss223455',//aes密钥,服务端和客户端必须保持一致
    'apptypes'=>[
        'ios',
        'android',
    ],
    'app_sign_time'=>6000,
];

Time.php

IAuth.php

encryt($string);
        return $string;
    }

    /**
     * 检查sign是否正常
     * @param string $sign
     * @param $data
     * @return boolean
     */
    public static function checkSignPass($data)
    {
        $str = (new Aes())->decrypt($data['sign']);
        if (empty($str)) {
            return false;
        }
        //diid=xx&app_type=3
        parse_str($str, $arr);

        /**
         * array (size=2)
         * 'did' => string '12345dg' (length=7)
         * 'version' => string '1' (length=1)
         */
        //halt($arr);

        if (!is_array($arr) || empty($arr['did'])
            || $arr['did'] != $data['did']
        ) {
            return false;
        }
        if ((time() - ceil($arr['time'] / 1000)) > config('app.app_sign_time')) {
            return false;
        }
        return true;
    }
}

Common.php

checkRequestAuth();
    }

    public function checkRequestAuth()
    {
        $headers = request()->header();
//        $this->testAes();

        //sign加密需要客户端工程师 解密 服务端工程师
        // 1 headers body 仿照sign 做参数的加解密
        // 2 加密数据步骤客户端服务端工程师共同约定
        // 3


        //基础检验
        if (empty($headers['sign'])) {
            throw new ApiException('sign不存在', 400);
        }
        if (!in_array($headers['app_type'], config('app.apptypes'))) {
            throw new ApiException('app_type不合法', 400);
        }

        if (!IAuth::checkSignPass($headers)) {
            throw new ApiException('授权码sign失败', 401);
        }

        $this->headers = $headers;


    }

    public function testAes()
    {
        $data = [
            'did' => '12345dg',
            'version' => 1,
            'time' => Time::get13TimeStamp(),
        ];

        //col9j6cqegAKiiey3IrXWi5kTf508OkMmPu9zrTihQ8IVnKDb7Rin03dOqY2qLWP
        //echo IAuth::setSign($data);exit;
        //exit;
    }


}
[PHP高可用后端]②⑥--sign有效时间处理_第2张图片
image.png

你可能感兴趣的:([PHP高可用后端]②⑥--sign有效时间处理)