PHP 使用token验证可有效的防止非法来源数据提交访问,增加数据操作的安全性
- /** 第一步:生成token */
- public function CreateToken($userid) {
- //用户名、此时的时间戳,并将过期时间拼接在一起
- $time = time();
- $end_time = time() + 86400;//过期时间
- $info = $userid . '.' . $time . '.' . $end_time; //设置token过期时间为一天
- //根据以上信息信息生成签名(密钥为 SIGNATURE 自定义全局常量)
- $signature = hash_hmac('md5', $info, SIGNATURE);
- //最后将这两部分拼接起来,得到最终的Token字符串
- return $token = $info . '.' . $signature;
- }
-
-
-
-
- /** 第二步:验证token */
- public function check_token($token)
- {
- /**** api传来的token ****/
- if(!isset($token) || empty($token))
- {
- $msg['code']='400';
- $msg['msg']='非法请求';
- return json_encode($msg,JSON_UNESCAPED_UNICODE);
- }
- //对比token
- $explode = explode('.',$token);//以.分割token为数组
- if(!empty($explode[0]) && !empty($explode[1]) && !empty($explode[2]) && !empty($explode[3]) )
- {
- $info = $explode[0].'.'.$explode[1].'.'.$explode[2];//信息部分
- $true_signature = hash_hmac('md5',$info,'siasqr');//正确的签名
- if(time() > $explode[2])
- {
- $msg['code']='401';
- $msg['msg']='Token已过期,请重新登录';
- return json_encode($msg,JSON_UNESCAPED_UNICODE);
- }
- if ($true_signature == $explode[3])
- {
- $msg['code']='200';
- $msg['msg']='Token合法';
- return json_encode($msg,JSON_UNESCAPED_UNICODE);
- }
- else
- {
- $msg['code']='400';
- $msg['msg']='Token不合法';
- return json_encode($msg,JSON_UNESCAPED_UNICODE);
- }
- }
- else
- {
- $msg['code']='400';
- $msg['msg']='Token不合法';
- return json_encode($msg,JSON_UNESCAPED_UNICODE);
- }
- }
- public $secretKey = 'hgakdfkljalfdjlk';//私钥
- public $termValidity = 60;//有效时常(秒)
- $info = ['user_id' => 1];
-
- //生成密钥
- $this->CreateToken( $info);
-
- //校验密钥
- $this->CheckToken($token);
-
- /**
- *功能:生成token
- *参数一:需要解密的密文
- *参数二:密钥
- */
- function CreateToken($data = []) {
- if(empty($data)){
- $data['code'] = '400';
- $data['message'] = '非法请求';
- return $data;
- }
-
- $data['request_time'] = time();//当前时间
- $data['term_validity'] = $this->termValidity;//过期时间
- $dataJson = json_encode( $data );
-
- //根据以上信息信息生成签名
- return $this->passport_encrypt($dataJson, $this->secretKey);
- }
-
-
- /**
- *功能:校验token
- *参数一:需要解密的密文
- *参数二:密钥
- */
- function CheckToken($token) {
-
- if (!isset($token) || empty($token)) {
- $data['code'] = '400';
- $data['message'] = '非法请求';
- return $data;
- }
-
- $explode =$this->passport_decrypt($token, $this->secretKey); //解析token
- $expArr = json_decode($explode,true);
- $checkToken = $this->passport_encrypt(json_encode($expArr), $this->secretKey);
-
- $requestTime = $expArr['request_time'];
- $termValidity = $expArr['term_validity'];
- $checkTime = $requestTime+$termValidity;
- //对比token
- if($token != $checkToken){
- $data['code'] = '400';
- $data['message'] = '非法请求';
- return $data;
- }
- $time = time();
- if ($time > $checkTime) {
- $data['code'] = '401';
- $data['message'] = 'Token已过期,请重新登录';
- return $data;
- }
- $data['code'] = '200';
- $data['message'] = '合法Token';
- return $data;
- }
- /*
- *功能:对字符串进行加密处理
- *参数一:需要加密的内容
- *参数二:密钥
- */
- function passport_encrypt($str,$key = 'secretKey'){ //加密函数
- return base64_encode($this->passport_key($str,$key));
- }
-
- /*
- *功能:对字符串进行解密处理
- *参数一:需要解密的密文
- *参数二:密钥
- */
- function passport_decrypt($str,$key = 'secretKey'){ //解密函数
- return $this->passport_key(base64_decode($str),$key);
- }
-
- /*
- *辅助函数
- */
- function passport_key($str,$encrypt_key){
- $encrypt_key=md5($encrypt_key);
- $ctr=0;
- $tmp='';
- for($i=0;$i<strlen($str);$i++){
- $ctr=$ctr==strlen($encrypt_key)?0:$ctr;
- $tmp.=$str[$i] ^ $encrypt_key[$ctr++];
- }
- return $tmp;
- }