php - 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
来解密,它就不起作用并给我一个空值。
解决方案
我会看两个问题:
- 两个 CakePHP 应用程序的
Security.salt
值通常不同。从您的错误消息中,我认为它为空或未设置(即 null)。除非您Security.salt
在两个应用程序中设置为相同的值,cipher
否则将无法为您提供 null 或一些不正确的值。 - 正如评论中提到的@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()
推荐阅读
- assembly - 在emu8086中搜索字符串的每个字符并更改它如何结束无限循环?
- kotlin - 如何用模拟服务替换 Ktor 路由测试中的 Koin 服务注入
- apache-kafka - Kafka 流如何处理分布式数据
- c - Visual Studio中的Scanf不接受多种字符
- c# - 无法使用 ClickOnce 安装程序部署 System.Net.Http
- html - 如何在不使用 libcurl 的情况下从 localhost 服务器获取 URL
- java - hadoop mapreduce 和 s3 的多个输入文件夹
- javascript - Javascript闭包返回字符串而不是函数
- ruby-on-rails - Rails 裁剪图像的起点、宽度和高度
- macos - 了解 Mac 上 top 的输出