【问题标题】:How to authenticate the post using curl?如何使用 curl 对帖子进行身份验证?
【发布时间】:2015-01-07 21:59:09
【问题描述】:

我有两个站点,一个是 a.com,另一个是 b.com这样站点 b.com 会在确保帖子来自站点 a.com 后做出响应。如何获取此信息?

网站 a.com 中的代码

<?php
  $some_data = array(
    'message' =--> 'Hello World',
    'name' => 'Chad'
  ); 

  $curl = curl_init();
  // You can also set the URL you want to communicate with by doing this:
  // $curl = curl_init('http://localhost/echoservice');

  // We POST the data
  curl_setopt($curl, CURLOPT_POST, 1);
  // Set the url path we want to call
  curl_setopt($curl, CURLOPT_URL, 'http://localhost/b.com'); 
  // Make it so the data coming back is put into a string
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  // Insert the data
  curl_setopt($curl, CURLOPT_POSTFIELDS, $some_data);

  // You can also bunch the above commands into an array if you choose using: curl_setopt_array

  // Send the request
  $result = curl_exec($curl);
  // Free up the resources $curl is using
  curl_close($curl);

  echo $result;
?>

B.com 中的代码

//I want to check here that the request was from a.com ,if it is ensured then i want to do //the rest of the work
  echo 'Your message was: ' . $_REQUEST["message"] . ' and your name is: ' . $_REQUEST["name"];
?

【问题讨论】:

  • 使用SSL,则可以验证两端的证书。
  • 创建两个站点都知道的哈希值(随机字符串)也有助于提高安全性。

标签: php curl


【解决方案1】:

您可以检查$_SERVER['REFERER'] 属性,但它非常不可靠/不安全。

更好的方法是使用 Basic Auth 或类似的东西设置 B 站点,当您从站点 A 发出请求时,您可以对其进行身份验证。然后您可以将基本身份验证添加到从 A 到 B 的 curl 请求. B 检查身份验证,如果正确则继续进行其余处理。

【讨论】:

    【解决方案2】:

    $_SERVER['REMOTE_ADDR'] 将是解决方案

    if($_SERVER['REMOTE_ADDR']=="IP OF A.com"){
    
    //exec code
    }else{
    log_error($_SERVER['REMOTE_ADDR'] has tried to access B.com at date());//that's an ex .
    }
    

    【讨论】:

    • 在简单的情况下它可以工作。但是,当您开始在任何一侧添加多个服务器、到处添加代理或负载均衡器等时,它会变得非常复杂。基于令牌的身份验证可能更灵活且更易于扩展。
    【解决方案3】:

    实现这一目标的最简单方法是创建一个站点 a.com 知道并且站点 b.com 知道的密钥。

    然后您可以通过 curl 将密钥从一台服务器传递到另一台服务器,只要知道其他人知道密钥是什么,他们就无法访问它(假设您以这种方式编程)。

    这是大多数 API 的工作方式,例如 Facebook、Twitter、Linkedin 等。

    您的帖子数据将如下所示,例如 (a.com):

    $some_data = array(
        'message' =--> 'Hello World',
        'name' => 'Chad',
        'key' => '4h9rj8wj49tj0wgj0ejwrkw0jt0ekv0ijspxodxk9rje0rg9tskvep9rrgt9wkrgte'
    ); 
    

    然后在b.com 上,您只需这样做:

    if(!isset($_POST['key']) && $_POST['key'] != '4h9rj8wj49tj0wgj0ejwrkw0jt0ekv0ijspxodxk9rje0rg9tskvep9rrgt9wkrgte'){
        die("Invalid Key");
    }
    

    【讨论】:

      【解决方案4】:

      您可以使用公共/私人配对系统。一个简单的版本是这样的:

      //a.com
      
      $keys  = array(
         'publicKey1' => 'privateKey1',
         'publicKey2' => 'privateKey2',
          //...
         'ksjdlfksjdlf' => '989384kjd90903@kjskdjdsd'
      );
      $publicKeys = array_keys($keys);
      //get a random key from pool
      $publicKey  = $publicKeys[rand(0, count($publicKeys))];
      $privateKey = $keys[$publicKey];
      
      //your data...
      $some_data = array(
         'message' => 'Hello World',
         'name' => 'Chad'
      );     
      
      /*generate a verification code from data...*/
      //add public key to data
      $some_data['key'] = $publicKey;
      //sort data (to always generate same verification code regardless of params order)
      uksort($some_data);
      
      //generate code with your private key
      $verificationKey = sha1($privateKey . http_build_query($some_data) . $privateKey);
      
      //add verification code to sent data
      $some_data['verification_code'] = $verificationKey;
      
      //send data
      curl_exec(...);
      

      在 b.com 上:

      $keys = "same keys that exist on a.com";
      if (!isset($_POST['key']) || !isset($_POST['verification_code']) || !isset($keys[$_POST['key'])) {
          //do something to handle invalid request
      }
      
      $verificationKey  = $_POST['verification_code'];    
      $privateKey       = $keys[$_POST['key']];
      
      //remove verification code from data
      unset($_POST['verification_code']);
      //sort data
      uksort($_POST);
      
      $checkKey = sha1($privateKey . http_build_query($_POST) . $privateKey);
      
      //validate key
      if ($checkKey != $verificationKey) {
         //handle invalid data
      }
      
      //verified. do something with $_POST
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-21
        • 2019-02-01
        • 2011-04-22
        • 2017-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多