• php用token做登录认证


    作用

    PHP 使用token验证可有效的防止非法来源数据提交访问,增加数据操作的安全性

    实例:

    第一种:

    1. /** 第一步:生成token */
    2. public function CreateToken($userid) {
    3. //用户名、此时的时间戳,并将过期时间拼接在一起
    4. $time = time();
    5. $end_time = time() + 86400;//过期时间
    6. $info = $userid . '.' . $time . '.' . $end_time; //设置token过期时间为一天
    7. //根据以上信息信息生成签名(密钥为 SIGNATURE 自定义全局常量)
    8. $signature = hash_hmac('md5', $info, SIGNATURE);
    9. //最后将这两部分拼接起来,得到最终的Token字符串
    10. return $token = $info . '.' . $signature;
    11. }
    12. /** 第二步:验证token */
    13. public function check_token($token)
    14. {
    15. /**** api传来的token ****/
    16. if(!isset($token) || empty($token))
    17. {
    18. $msg['code']='400';
    19. $msg['msg']='非法请求';
    20. return json_encode($msg,JSON_UNESCAPED_UNICODE);
    21. }
    22. //对比token
    23. $explode = explode('.',$token);//以.分割token为数组
    24. if(!empty($explode[0]) && !empty($explode[1]) && !empty($explode[2]) && !empty($explode[3]) )
    25. {
    26. $info = $explode[0].'.'.$explode[1].'.'.$explode[2];//信息部分
    27. $true_signature = hash_hmac('md5',$info,'siasqr');//正确的签名
    28. if(time() > $explode[2])
    29. {
    30. $msg['code']='401';
    31. $msg['msg']='Token已过期,请重新登录';
    32. return json_encode($msg,JSON_UNESCAPED_UNICODE);
    33. }
    34. if ($true_signature == $explode[3])
    35. {
    36. $msg['code']='200';
    37. $msg['msg']='Token合法';
    38. return json_encode($msg,JSON_UNESCAPED_UNICODE);
    39. }
    40. else
    41. {
    42. $msg['code']='400';
    43. $msg['msg']='Token不合法';
    44. return json_encode($msg,JSON_UNESCAPED_UNICODE);
    45. }
    46. }
    47. else
    48. {
    49. $msg['code']='400';
    50. $msg['msg']='Token不合法';
    51. return json_encode($msg,JSON_UNESCAPED_UNICODE);
    52. }
    53. }

    第二种:

    1、密钥设置

    1. public $secretKey = 'hgakdfkljalfdjlk';//私钥
    2. public $termValidity = 60;//有效时常(秒)

    2、调用方式

    1. $info = ['user_id' => 1];
    2. //生成密钥
    3. $this->CreateToken( $info);
    4. //校验密钥
    5. $this->CheckToken($token);

     3、基础方法

    1. /**
    2. *功能:生成token
    3. *参数一:需要解密的密文
    4. *参数二:密钥
    5. */
    6. function CreateToken($data = []) {
    7. if(empty($data)){
    8. $data['code'] = '400';
    9. $data['message'] = '非法请求';
    10. return $data;
    11. }
    12. $data['request_time'] = time();//当前时间
    13. $data['term_validity'] = $this->termValidity;//过期时间
    14. $dataJson = json_encode( $data );
    15. //根据以上信息信息生成签名
    16. return $this->passport_encrypt($dataJson, $this->secretKey);
    17. }
    18. /**
    19. *功能:校验token
    20. *参数一:需要解密的密文
    21. *参数二:密钥
    22. */
    23. function CheckToken($token) {
    24. if (!isset($token) || empty($token)) {
    25. $data['code'] = '400';
    26. $data['message'] = '非法请求';
    27. return $data;
    28. }
    29. $explode =$this->passport_decrypt($token, $this->secretKey); //解析token
    30. $expArr = json_decode($explode,true);
    31. $checkToken = $this->passport_encrypt(json_encode($expArr), $this->secretKey);
    32. $requestTime = $expArr['request_time'];
    33. $termValidity = $expArr['term_validity'];
    34. $checkTime = $requestTime+$termValidity;
    35. //对比token
    36. if($token != $checkToken){
    37. $data['code'] = '400';
    38. $data['message'] = '非法请求';
    39. return $data;
    40. }
    41. $time = time();
    42. if ($time > $checkTime) {
    43. $data['code'] = '401';
    44. $data['message'] = 'Token已过期,请重新登录';
    45. return $data;
    46. }
    47. $data['code'] = '200';
    48. $data['message'] = '合法Token';
    49. return $data;
    50. }
    51. /*
    52. *功能:对字符串进行加密处理
    53. *参数一:需要加密的内容
    54. *参数二:密钥
    55. */
    56. function passport_encrypt($str,$key = 'secretKey'){ //加密函数
    57. return base64_encode($this->passport_key($str,$key));
    58. }
    59. /*
    60. *功能:对字符串进行解密处理
    61. *参数一:需要解密的密文
    62. *参数二:密钥
    63. */
    64. function passport_decrypt($str,$key = 'secretKey'){ //解密函数
    65. return $this->passport_key(base64_decode($str),$key);
    66. }
    67. /*
    68. *辅助函数
    69. */
    70. function passport_key($str,$encrypt_key){
    71. $encrypt_key=md5($encrypt_key);
    72. $ctr=0;
    73. $tmp='';
    74. for($i=0;$i<strlen($str);$i++){
    75. $ctr=$ctr==strlen($encrypt_key)?0:$ctr;
    76. $tmp.=$str[$i] ^ $encrypt_key[$ctr++];
    77. }
    78. return $tmp;
    79. }

  • 相关阅读:
    [设计模式] 浅谈SOLID设计原则
    Spring的IOC原理
    数组-扩展运算符的应用、Array.of()、find()、findIndex()、fill()、includes()、flat(),flatMap()
    [Linux]文件描述符(万字详解)
    spring5.3 十二:spring配置类解析源码解析
    【无标题】
    长春市OLED透明拼接屏:高清晰度和透明度的双重优势
    PG守护进程(Postmaster)——辅助进程PgStat主流程
    Nginx Note02——事件驱动模型
    三、图片的几何变换
  • 原文地址:https://blog.csdn.net/qq_20869933/article/details/133201967