• postman 简单的实现接口登陆添加(php 生成token值验证)


      1 <?php
      2 
      3 namespace AppHttpControllersApi;
      4 
      5 use IlluminateHttpRequest;
      6 use AppHttpControllersController;
      7 use IlluminateSupportFacadesDB;
      8 
      9 class ApiController extends AppController
     10 {      
     11        public function api(Request $request){
     12              //接受name,pwd值 
     13              $name = $request->post('name');
     14              $pwd = $request->post('password');
     15 
     16             //sql 查询
     17             $res =  DB::table('user')->where("user",'=',"$name")->where('password','=',"$pwd")->first();
     18 
     19             if ($res){
     20            //生成token
     21                   $num = rand(1000,9999);
     22                   $tokenName = 'hello';
     23                   $time = date('YmdHis',time());
     24 
     25                   $arr = array(
     26                     $num,$tokenName,$time
     27                   );
     28                    sort($arr,SORT_STRING);
     29 
     30                    $str = implode($arr);
     31 
     32                    $strStr = sha1($str);
     33 
     34 
     35                 //判断是否存在文件  或者 当前时间-文件创建时间 如果大于7200  则 重新写入文件
     36                   if (!file_exists('token.txt') || time()-filemtime('token.txt') > 7200){
     37 
     38                       file_put_contents('token.txt',$strStr);
     39 
     40 
     41                   }else{
     42                  //否之读取文件token
     43                       $strs = file_get_contents('token.txt');
     44                   // 还剩多少秒过期
     45                       $ri = 7200 - (time()-filemtime('token.txt'));
     46                   //返回数组值
     47                       return [
     48                           'code' => 200,
     49                           'message' => '登陆成功',
     50                           'token'  => $strs,
     51                           'gtime'  => "token" .$ri . "秒后过期"
     52 
     53                       ];
     54                   }
     55 
     56 
     57 
     58             }else{
     59                 //否之登陆失败
     60                 return [
     61                     'code' => 300,
     62                     'message' => '登陆失败,请重新登陆'
     63 
     64                 ];
     65             }
     66 
     67        }
     68 
     69         //此为添加方法
     70        public function add(Request $request){
     71 
     72 
     73              //接收获取的token值
     74                $token = $request->post('token');
     75 
     76                
     77                 //将token值传入getAdd方法进行验证
     78                $res = $this->getAdd($token);
     79 
     80 
     81               //同样接收 对应值
     82                $name = $request->post('name');
     83 
     84                $password = $request->post('password');
     85               
     86                //转为为数组
     87                $arr = [
     88                    'user' => $name,
     89                    'password' => $password
     90                ];
     91 
     92 
     93                 //判断返回值 ture 则进行添加 否之 返回 添加失败
     94                if ($res){
     95 
     96                   $res1 =  DB::table('user')->insert($arr);
     97    
     98                   if ($res1){
     99 
    100                       return [
    101                           'code' => 200,
    102                           'message' => '添加成功',
    103                       ];
    104                   }else{
    105 
    106                       return [
    107                           'code' => 300,
    108                           'message' => '添加失败'
    109                       ];
    110                   }
    111                 
    112                }else{
    113                    //token值验证失败  返回对应内容
    114                    return [
    115                        'code' => 400,
    116                        'message' => 'token值不正确,请重新获取'
    117                    ];
    118 
    119 
    120                }
    121 
    122 
    123 
    124 
    125 
    126        }
    127        //检测传入token值是否正确
    128         public function getAdd($token){
    129   //读取本地 文件token值
    130                $tokens = file_get_contents('token.txt');
    131 
    132 
    133                 if ($tokens == $token){
    134 
    135                     return 1;
    136                 }else{
    137 
    138                     return false;
    139                 }
    140 
    141 
    142         }
    143      //自己定义测试方法  当然也可选择 postman进行测试
    144        public function postMan(){
    145 
    146              $url = "http://www.laraval2.com/api/api";
    147 
    148              $urls = "http://www.laraval2.com/api/add";
    149 
    150            $arrLogin = [
    151                'name' => '2',
    152                'password' => '2',
    153 
    154            ];
    155 
    156              $arrAdd = [
    157                  'name' => '6',
    158                  'password' => '2',
    159                  'token' => '8e07f3548818817d2cd05e6c7c0fb3c5f17aefcb',
    160 
    161              ];
    162 
    163             //调用父级控制器的 getCurl方法 发送数据到客户端
    164             $res =  $this->getCurl($url,'post',$arrLogin);
    165              //接受返回值,并转为数组形式
    166              $arr = json_decode($res,true);
    167            //打印    
    168             var_dump($arr);
    169 
    170 
    171        }
    172 
    173 
    174 }

    对应的父级控制器 AppController.php :  Curl方法

     1        public function getCurl($url,$month='post',$can=null){
     2 
     3              $ch = curl_init();
     4 
     5              curl_setopt($ch,CURLOPT_URL,$url);
     6              curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
     7 
     8              curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
     9 
    10 
    11              if ($month=='post') {
    12                  curl_setopt($ch, CURLOPT_POST, 1);
    13                  curl_setopt($ch, CURLOPT_POSTFIELDS, $can);
    14 
    15              }
    16              $data = curl_exec($ch);
    17 
    18 
    19              curl_close($ch);
    20 
    21              return $data;
    22 
    23 
    24 
    25        }
  • 相关阅读:
    activiti 自定义用户
    Activiti6 应用安装 activiti-admin,activiti-app,activiti-rest
    jsoup获取标签下的文本(去除子标签的)
    关于idea跳过错误编译的理解, 跳过报错的代码启动项目去debug测试其他正常的代码
    mybatis 使用参数
    AnnotatedElementUtils.findMergedAnnotation作用
    tomcat中AuthenticatorBase简单的安全认证
    tomcat跨域请求过滤器CorsFilter使用的预检preFlight及其他过滤器
    tomcat中SSIPrintenv.java漏洞修复
    Gitlab+Jenkins Pipeline+Docker+k8s+Helm自动化部署实践(干货!)
  • 原文地址:https://www.cnblogs.com/zhaoguiyuan/p/11208650.html
Copyright © 2020-2023  润新知