首页 > 解决方案 > Cakephp 2.x Security::cipher 不提供跨两个应用程序的结果

问题描述

我有 2 个应用程序相互通信。第一个应用程序是执行交易的应用程序,另一个应用程序是一个设置应用程序,用于控制第一个应用程序的系统设置。

到达第一个应用程序的登录页面后,我通过curl内部调用 WebService config.php,它将与第二个应用程序通信并返回相应的值。现在,我的问题是,该值是使用Security::cipher()函数加密的,并且是使用设置应用程序内的模块加密的。当我尝试解密它时,没有提示错误,甚至错误日志文件中也没有记录错误。我怀疑当我从中解密时WebServicesController不会读取安全组件。我试图把App::uses('Security','Utility')代码放在上面。这是我的编码方式:

第一个应用程序

curl_setopt($ch, CURLOPT_URL, Configure::read('TMSWebServices.Url').'getSystemSetting.json');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

$result_json = curl_exec($ch);
curl_close($ch);
$result = json_decode($result_json, true);
debug($result); exit;

第二个应用

App::uses('AuthComponent', 'Controller/Component');
App::uses('UsersController', 'Controller');
App::uses('Security','Utility');

class WebServicesController extends AppController {
 public $name = 'WebServices';
 public $uses = array('Tenant', 'TbNextId', 'ReportMaster' );
 public $components = array('RequestHandler');

 public function getSystemSetting(){
    $this->loadModel('SystemSetting');
    $results = $this->SystemSetting->find($type, $params);
    $return_value = array();
    $return_value = $results['SystemSetting'];
   foreach($return_value['Config'] as $key=>$value){
        if ($value['ENCRYPT_IND'] == 'Y'){
             $encryptedValue = $return_value['Config'][$key]['SYSTEM_SETTING_VALUE'];
         //decrypt the value
           $decryptedValue = Security::cipher($encryptedValue, 
          Configure::read('Security.salt')); // the problem starts here
           $return_value['Config'][$key]['SYSTEM_SETTING_VALUE'] = $decryptedValue;
        }
   }

 }

}
$this->set(array(
                'return_value' => $return_value,
                '_serialize' => array('return_value')
            ));

当我尝试在 $return_value 上设置简单的值时,通信正常。但是如果我使用Security::cipher来解密,它就不起作用并给我一个空值。在此处输入图像描述

标签: phpweb-servicescurlcakephpcakephp-2.x

解决方案


我会看两个问题:

  1. 两个 CakePHP 应用程序的Security.salt通常不同。从您的错误消息中,我认为它为空或未设置(即 null)。除非您Security.salt在两个应用程序中设置为相同的值,cipher否则将无法为您提供 null 或一些不正确的值。
  2. 正如评论中提到的@ndm,您正在创建一个平面数组(不是关联数组),所以我想知道您的代码是如何进入foreach循环的。

看下面的代码和我的评论:

// ... 
$return_value = array(); // we have ourselves an empty array
$return_value[] = $results['SystemSetting']; // we assign a value to $return_value[0]
foreach($return_value['Config'] as $key=>$value){ // no such key 'Config' in the array
// loop code follows, but execution flow shouldn't enter here...

虽然我们正在使用它,但我强烈建议您不要使用Security.salt,因为它只是一个弱 XOR 密码,尝试使用Security.encrypt()Security.decrypt()


推荐阅读