利用PHP默认函数生成Token

白俊遥博客


login.php


$encryptStr = $adminInfo["admin_id"]."|".substr($adminInfo['password'],0,5)."".time();
$access_token = encrypt_openssl($encryptStr);

//公司名称
$crop = WorkCorpModel::where('id',1)->field('corp_name')->find();
return output(200,'登录成功',['token' => $access_token,'permission' => $role['rule'],'crop_name'=>$crop->corp_name]);


common.php

//encrypt_openssl新版加密
function encrypt_openssl($str,$localIV = '', $encryptKey = ''){
   if(empty($localIV)){
       $localIV = config('crypt.iv');
   }
   if(empty($encryptKey)){
       $encryptKey = config('crypt.key');
   }
   return openssl_encrypt($str, 'AES-128-CBC',$encryptKey,0,$localIV);
}
//decrypt_openssl新版解密
function decrypt_openssl($str,$localIV = '',$encryptKey = ''){
   if(empty($localIV)){
       $localIV = config('crypt.iv');
   }
   if(empty($encryptKey)){
       $encryptKey = config('crypt.key');
   }
   return openssl_decrypt($str, 'AES-128-CBC', $encryptKey, 0, $localIV);
}




config.php

//加密字符串
'crypt' => [
   'key' => 'hmV1K8fIP9vhCMTq',
   'iv' => 'qvYtf3ucuzvV7tcV'
],



登录页解密


// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
function getLoginAdmin(){
   $access_token = \think\Cookie::get('token') ? \think\Cookie::get('token') : input('token');
   if(empty($access_token)){
       $access_token = request()->header('X-Token');
   }
   if(empty($access_token)){
       return false;
   }
   $decryptStr = decrypt_openssl($access_token);
   if(!$decryptStr){
       return false;
   }
   $adminData = explode('|',$decryptStr);
   if(count($adminData) != 3){
       return false;
   }
   $admin_id = $adminData[0];
   $adminModel = new \app\common\model\AdminModel();
   $admin = $adminModel->getAdminByID($admin_id);
   if(!$admin){
       return false;
   }

   //判断用户密码是否有修改
   if(substr($admin['password'],0,5) != $adminData[1]){
       return false;
   }
   return $admin;
}


adminBase.php

$this->admin = $this->getLoginAdmin();
if(!$this->admin){
   echo output(401,'管理员未登录',[],false,true);exit;
}


白俊遥博客
请先登录后发表评论
  • 最新评论
  • 总共0条评论