php访问信息签名,PHP API 接口访问之签名验证

设计思路:1、前后端商定统一的加密规则;2、后端配置文件保存固定的验证字符串;3、对前端传的加密sign进行合法性验证、时效验证、唯一性验证;

代码如下:

1、验证前端传过来的sign(抛出异常的接口是我自己框架封装的接口,没封装可以改成Exception)

/**

* Created by PhpStorm.

* User: hjx(2896751917@qq.com)

* Date: 2019/2/21

*/

namespace app\api\controller;

use app\common\lib\exception\ApiException;

use app\common\lib\extend\Aes;

use think\Cache;

use think\Controller;

use think\Request;

class Common extends Controller

{

protected $apiToken = null;

public function __construct(Request $request = null)

{

parent::__construct($request);

$this->verifyToken();

}

/**

* 访问API时进行sign验证

* @return bool

* @throws ApiException

*/

public function verifyToken() {

$data = input('param.');

$validate = validate('Common');

if(!$validate->check($data)) {

throw new ApiException($validate->getError(), 400);

}

$str = (new Aes())->decrypt($data['sign']);

if(!$str) {

throw new ApiException('sign不合法', 401);

}

list($t1,$t2) = explode('&',$str);

//sign失效时间

if(time() - $t1 > 600) {

throw new ApiException('sign已过期', 401);

}

//验证规则

if($t2 != config('api.CorpId')) {

throw new ApiException('sign不合法', 401);

}

//验证sign唯一性

if(Cache::get($data['sign'])) {

throw new ApiException('sign已使用过', 401);

}

Cache::set($data['sign'],1,3600);

return true;

}

}

2、各个控制器都继承上面的Common控制器即可

3、后台加密规则

/**

* Created by PhpStorm.

* User: hjx

* Date: 2019/3/12

* Time: 10:41

*/

namespace app\api\controller;

use app\common\lib\extend\Aes;

class Test

{

/**

* token创建规则,以后前端得按照这个规则进行

* @return \app\common\lib\extend\HexString

*/

public function createToken() {

$str = time().'&'.config('api.CorpId');

return (new Aes())->encrypt($str);

}

}

后台加密只是示例的加密规则,可以给前端按照这种加密方式进行加密得到sign

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值