1.加密方式

token = md5(time+salt)

(salt 约定: ECCBC87E4B5CE2FE28308FD9F2A8BAF3; time时间戳, 10分钟过期)

 

2.Demo

http://search.checheng.com/query?time=1476092844&token=2822b05a9f1303c4ef52264abfdc3b84

    const TOKEN_SALT = 'ECCBC87E4B5CE2FE28308FD9F2A8BAF3'; //api 接口盐值    
    private static $debug =false; //是否调试
    public function beforeAction($action)
    {
        #echo md5('1490943609'.'ECCBC87E4B5CE2FE28308FD9F2A7BAF3');exit;#573ea66a0ea56a76520ff53da8e5c041
        $time = Yii::$app->request->get('time');
        $token = Yii::$app->request->get('token');
        self::$debug =Yii::$app->request->get('debug','false');
        $result = [
            'data' =>[],
            'msg' =>'',
            'status' =>1,
        ];
        //判断验证
        if(empty($time) || empty($token)){
            $result['status'] =0;
            $result['msg'] ='缺少验证';
        }
        elseif(time()-600>$time){
                $result['status'] =0;
                $result['msg'] ='请求超时';
            }elseif($token!=md5($time.self::TOKEN_SALT)){
                $result['status'] =0;
                $result['msg'] ='token错误';
        }
        if($result['status']==0){
                self::returnResult($result);
        }else{
                return parent::beforeAction($action); // TODO: Change the autogenerated stub
        }
    }