인터페이스 와 h5 의 데이터 처리 (암호 화 및 검사 및 감정 권)
#
'mcrypt'=>[
'is_open'=> true, # true
'key' => 'mcrypt',
'iv' => '00000000'
],
# app_id app_secret
'api_allow_access'=>[
'123456' => 'asdfghjkl',
],
하면, 만약, 만약...open = true 로그 인 또는 기타 제출 한 데 이 터 를 클 라 이언 트 암호 화 를 통 해 appid 가입 apiparam,app_secret 는 json 뒤에 연결 되 어 MD5 로 배열 되 어 있 습 니 다.
#
private function _makeClientSign(&$api_param)
{
$api_auth = Yii::$app->params['API_AUTH'];
#
$api_param['app_id'] = $api_auth['api_id'];
#
ksort($api_param);
# json
$json = json_encode($api_param);
#json + api_secret
$json = $json.$api_auth['api_secret'];
return md5($json);
}
yii 프레임 워 크 의 param 설정 파일 을 통 해 이동 할 경 로 를 설정 합 니 다.
#api
'url'=>[
#
'domain' => '',
#
'api_host' => '',
#
'api_list' => [
# url
'getVcodeUrl' => '',
#
'login' => '',
#
'goodsList' => '',
#
'goodsDetial' => '',
# session
'shopCartList' => '',
#
'getShopInfo' => '',
]
],
/ / api 요청 보 내기
public function sendApiRequest( $conf_name ,$api_param = [])
{
$api_url = $this->buildApiUrl($conf_name);
$mcrypt = Yii::$app->params['mcrypt'];
#
$api_des_data['sign'] = $this->_makeClientSign($api_param);
#
if($mcrypt['is_open'] == true){
if(!empty($api_param)){
$api_des_data['data'] = Des::encode(json_encode($api_param),$mcrypt['key'],$mcrypt['iv']);
}else{
$api_des_data['data'] = [];
}
}else{
$api_des_data['data'] = $api_param;
}
return $this->CurlPost($api_url , $api_des_data);
}
그리고 curlPOST 를 통 해 api 페이지 에 제출 합 니 다.
tp5 프레임 워 크 에서 도 마찬가지 로 설정
//
'mcrypt'=>[
'is_open'=> true, # true
'key' => 'mcrypt',
'iv' => '00000000'
],
그리고 구조 방법 initlize () 를 만 듭 니 다.
; api/common iniyialize public
# TODO
public $not_check_sign =[
'publics/getvcodeurl'
];
public function _initialize()
{
parent::_initialize();
$api_des = config('api.mcrypt');
$data = request()->post('data');
if($api_des['is_open'] == true){
if(!empty($data)){
$data_arr = json_decode(Des::decode($data,$api_des['key'],$api_des['iv']),true);
}else{
$data_arr = [];
}
$this->_data = $data_arr;
}else{
$this->_data = request()->post();
}
#
$clientSign = request()->post('sign');
#
$this->_inspectionSign($this->_data,$clientSign);
}
/ / 체크
private function _inspectionSign($data,$clientSign)
{
$controller = request()->controller();
$action = request()->action();
$url = $controller.'/'.$action;
if(in_array(strtolower($url),$this->not_check_sign)){
$serverSign = $this->_makeServerSign($data);
if($clientSign != $serverSign){
$this->error(' ');
}
}
}
/ / 서버 서명 생 성
private function _makeServerSign($data)
{
#
ksort($data);
# api_id
if(empty($data['app_id'])){
$this->error(' ');
}
# app_id app_secret
$api_allow_access = config('api.api_allow_access');
# app_id :array_key_exisets()
if(!array_key_exists($data['app_id'],$api_allow_access)){
$this->error(' ');
}
# json
$json = json_encode($data);
return md5($json.$api_allow_access[$data['app_id']]);
}
** ** : iv , ;
tp5 Des 클래스 는 다음 과 같 습 니 다.
8) {
return $source;
}
$len = strlen($source);
for ($i = $len - 1; $i >= $len - $num; $i--) {
if (ord(substr($source, $i, 1)) != $num) {
return $source;
}
}
$source = substr($source, 0, -$num);
return $source;
}
}