thinkphpでAPIインターフェースデータセキュリティソリューションの認証サインのユニークなサポートを学ぶ
バックグラウンド
各クライアントリクエストサインの一意性を確保するために、それは1回しか使用できないため、毎回送信されるサインが一意であるかどうかをコードで判断する必要があります
redisキャッシュと組み合わせる
参照redisキャッシュ
think \ cache \ driver \ Redisを使用します;
APIモジュールCommon.phpのコントローラー共通クラス
<?php
namespace app\api\controller;
use app\common\lib\ApiAuth;
use app\common\lib\Time;
use think\Controller;
use app\common\lib\exception\ApiException;
use app\common\lib\Aes;
use think\cache\driver\Redis;
/*
* api接口模块的公共控制器
*/
class Common extends Controller
{
public function _initialize()
{
$this->checkRequestAuth();
}
//验证方法
/*
* 检查app每一次提交的数据是否合法
*/
public function checkRequestAuth()
{
//验证header头的信息
$header = request()->header();
//halt($header);
//sign 客户端工程师加密,服务端工程师解密
//基础数据校验
//如果sign不存在,报错
if (empty($header['sign'])) {
throw new ApiException('签名不存在!');
}
if (empty($header['apptype'])) {
throw new ApiException('客户端不存在!');
}
//验证请求的app客户端是否合法
if (!in_array($header['apptype'], config('app.apptypes'))) {
throw new ApiException('客户端不合法!', 400);
}
//校验sign
$header_result = ApiAuth::checkSign($header);
if (!$header_result) {
throw new ApiException('签名不合法!', 401);
}
//为了保证sign的唯一性,我们使用redis缓存,保证用过的sign,不可以再次使用
$redis = new Redis;
$redis->set($header['sign'], $header['sign'],config('app.api_sign_redis_time'));
}
}
Api署名検証クラスApiAuth
ApiAuth.php
<?php
namespace app\common\lib;
use app\common\lib\Aes;
use think\cache\driver\Redis;
class ApiAuth
{
/*
* 生成签名
*/
public static function setSign($data = [])
{
//1 把数组按照字段你排序
ksort($data);
//2 将数组更改为拼接字符串的格式
$sign_str = http_build_query($data);
//3 通过aes加密
return (new Aes())->encrypt($sign_str);
}
//校验签名sign
public static function checkSign($data)
{
//解密
$str = (new Aes())->decrypt($data['sign']);
if (!$str) {
return false;
}
//将字符串你转成数组格式
parse_str($str, $arr);
//比较常用变量的值是否一致
if (!is_array($arr) || $arr['name'] != 'qipa250') {
return false;
}
//校验sign有效期 由于解析后的日期是13位的,除以1000后得到10位数
//两个时间再相减, 超过设置的有效
if ((time() - ceil($arr['time'] / 1000)) > config('app.api_sign_expire_time')) {
return false;
}
//验证sign的唯一性,若是存在,则表示已请求过,重复请求
$redis = new Redis;
if ($redis->get($data['sign'])) {
return false;
}
return true;
}
}
アプリの構成
app.php set api_sign_redis_time
署名redisキャッシュの有効期間を延長します
<?php
return [
'admin_password_pre' => '_qipa250',//后台管理员密码加密后缀
'aeskey' => 'QiPa250',//aes 密钥,服务端和客户端保持一致
'aesiv' => '12345678901234567890123456789012',//aes iv,服务端和客户端保持一致
'apptypes' => ['ios', 'android', 'wechat'],
'api_sign_expire_time' => 10,//sign签名有效期为10s,
'api_sign_redis_time' => 20,//sign签名redis有效期为20s,必须大于api_sign_expire_time,才生效,小于的话,会重复请求,不保证sign请求的唯一性
];
郵便配達員のリクエスト
初めて成功しました
。redisで表示すると、キー値はすでに存在します
再度リクエストすると、すでに使用されているため例外が報告され、同じサインを繰り返しリクエストすることはできません。