zoukankan      html  css  js  c++  java
  • api数据安全之sign校验



    1:前端:根据key,将当前13位时间戳通过aes加密生成的密文放到header请求头的sign属性中
    2:后端:接收header头的sign属性的值,通过aes解密,得到13位时间戳。如果时间戳的时间是五秒之前的或者大于当前时间。
        则sign错误不予放行;
     
     
    php代码
     
    common.php
    <?php
    namespace appapicontroller;
    use thinkController;
    use appcommonlibIAuth;
    use thinkFacadeCache;
    // API模块 公共的控制器
    class Common extends Controller
    {
        public $headers = '';
        // 初始化的方法
        public function __construct(){
            // 校验sign值
            $this->checkRequestAuth();
            parent::__construct();
        }
        // 检查每次app请求的数据是否合法
        public function checkRequestAuth(){
            // 实现需要获取headers的数据
            $headers = request()->header();
            
            // 基础参数校验
            if(empty($headers['sign'])){
                AException('sign不存在', 401);
            }
            if(!IAuth::checkSignPass($headers['sign'])){
                AException('sign校验失败', 401);
            }
    
            // 通过,存入缓存(实现sign的使用即失效
            Cache::set($headers['sign'], 1, config('IAuth.sign_cache_time'));
            // 存到成员属性中,供子类调用
            $this->headers = $headers;
        }
        
    }

    IAuth.php

    <?php
    /** .-------------------------------------------------------------------
     * | Author: OkCoder <1046512080@qq.com>
     * | Git: https://www.gitee.com/okcoder
     * | Copyright (c) 2012-2019, www.i5920.com. All Rights Reserved.
     * '-------------------------------------------------------------------*/
    
    namespace appcommonlib;
    use thinkFacadeCache;
    class IAuth
    {
        /**
         * 设置密码
         * @param string $data 字符串
         * @return string
         */
        public static function setPassword($str)
        {
           return md5($str.config('IAuth.password_salt'));
        }
    
        /**
         * 模拟前端工程师加密。生成每次请求的sign
         * @param string $data 字符串
         * @return string
         */
        public static function setSign($time = '')
        {
            if(empty($time)) $time = Time::get13TimeStamp();
            // 加密
            $res = Aes::encode($time);
            return $res;
        }
        /*
        * 检测每次生成的sign
        */
        public static function checkSignPass($sign){
            // 解密
            $str = Aes::decode($sign);
    
            // 数据规则
            if(!$str || !is_numeric($str)){
                return false;
            }
            // 测试时间戳,如果时间戳是五秒前的则错误
            $now = Time::get13TimeStamp();
            if( $now - $str > config('IAuth.sign_time') ||  $str > $now){
                return false;
            }
            // 如果缓存中有该sign,即sign使用过,不放行
            if(Cache::get($sign)){
                return false;
            }
            return true;
        }
    
    }

    Aes.php

    <?php
    /** .-------------------------------------------------------------------
     * | Author: OkCoder <1046512080@qq.com>
     * | Git: https://www.gitee.com/okcoder
     * | Copyright (c) 2012-2019, www.i5920.com. All Rights Reserved.
     * '-------------------------------------------------------------------*/
    
    namespace appcommonlib;
    class Aes
    {
        /**
         * 解密字符串
         * @param string $data 字符串
         * @return string
         */
        public static function decode($str)
        {
            // AES模式不用vi偏移值
            return openssl_decrypt(base64_decode($str),"AES-128-ECB",config('Aes.key'),OPENSSL_RAW_DATA);
        }
    
        /**
         * 加密字符串
         * @param string $data 字符串
         * @return string
         */
        public static function encode($str)
        {
            //密钥 须是16位
            return base64_encode(openssl_encrypt($str,"AES-128-ECB",config('Aes.key'),OPENSSL_RAW_DATA));
        }
    
    }
  • 相关阅读:
    for循环删除数组中的元素crash问题
    iOS判断字符串中含不含有汉字
    iOS 拨打电话(解决openURL延迟和不同方法比较)
    ios oc单例宏定义
    iOS UIBezierPath简单实用
    iOS视图切割圆角
    iOS 内购集成与遇到的坑,添加新内购项目
    iOS工程中创建pch文件
    四舍五入的方法
    ScrollView定时器复用
  • 原文地址:https://www.cnblogs.com/cl94/p/12498860.html
Copyright © 2011-2022 走看看