【类 型】:fix

【主	题】:token 验证
【描	述】:
	[原因]:try jwt函数的解码 防止非法token 导致的页面错误
	[过程]:
	[影响]:
【结	束】

# 类型 包含:
# feat:新功能(feature)
# fix:修补bug
# docs:文档(documentation)
# style: 格式(不影响代码运行的变动)
# refactor:重构(即不是新增功能,也不是修改bug的代码变动)
# test:增加测试
# chore:构建过程或辅助工具的变动
This commit is contained in:
szdot 2024-05-31 17:18:07 +08:00
parent 9060bc0c27
commit 04d3cf1eb8
2 changed files with 41 additions and 17 deletions

View File

@ -18,18 +18,36 @@ class CheckController extends PublicController
parent::_initialize();
//解构文件头里面的token
$server = isset($_SERVER) ? $_SERVER : "";
$token = $server['HTTP_TOKEN'];
$token = isset($server['HTTP_TOKEN']) ? $server['HTTP_TOKEN'] : null;
$jwtKey = C('jwtKey'); // jwt密钥
$jwt = JWT::decode($token, new Key($jwtKey, 'HS256')); // 使用密钥和 HS256 算法对 JWT 进行解码
$res_token = (array) $jwt; // 将解码后的对象转换为数组
//token过期
if (empty($res_token)) {
echo json_encode(array('status' => -1, 'msg' => '帐号认证过期!'));
try {
$jwt = JWT::decode($token, new Key($jwtKey, 'HS256')); // 使用密钥和 HS256 算法对 JWT 进行解码
$res_token = (array) $jwt; // 将解码后的对象转换为数组
// token过期
if (empty($res_token)) {
echo json_encode(array('status' => -1, 'msg' => '帐号认证过期!'));
exit();
}
// token检测通过 获取用户id
$this->openid = $res_token['openid'];
$this->session_key = $res_token['session_key'];
} catch (\UnexpectedValueException $e) {
// 捕获JWT解码错误
echo json_encode(array('status' => -1, 'msg' => 'Token 无效: ' . $e->getMessage()));
exit();
} catch (\DomainException $e) {
// 捕获JWT解码错误
echo json_encode(array('status' => -1, 'msg' => 'Token 解码失败: ' . $e->getMessage()));
exit();
} catch (\Exception $e) {
// 捕获其他可能的错误
echo json_encode(array('status' => -1, 'msg' => '未知错误: ' . $e->getMessage()));
exit();
}
//token检测通过 获取用户id
$this->openid = $res_token['openid'];
$this->session_key = $res_token['session_key'];
}
public function index()
{
echo json_encode(array('status' => 1, 'msg' => '认证通过'));
}
/**
* @description: 获取用户手机号 并将手机号写入数据库 并给前端返回 隐藏中间4位的手机号

View File

@ -16,7 +16,6 @@ class PublicController extends Controller
{
header("Access-Control-Allow-Origin: " . C('LimitApi')); //请求域名限制
header('Access-Control-Allow-Headers:Token'); //token请求头
//token验证
// token 验证
$server = isset($_SERVER) ? $_SERVER : "";
$token = isset($server['HTTP_TOKEN']) ? $server['HTTP_TOKEN'] : null;
@ -26,15 +25,22 @@ class PublicController extends Controller
exit();
}
$jwtKey = C('jwtKey'); // jwt密钥
$jwt = JWT::decode($token, new Key($jwtKey, 'HS256')); // 使用密钥和 HS256 算法对 JWT 进行解码
$res_token = (array) $jwt; // 将解码后的对象转换为数组
//token过期
if (empty($res_token)) {
echo json_encode(array('status' => -1, 'msg' => '帐号认证过期!'));
try {
// 使用密钥和 HS256 算法对 JWT 进行解码
$jwt = JWT::decode($token, new Key($jwtKey, 'HS256'));
$res_token = (array) $jwt; // 将解码后的对象转换为数组
// token过期
if (empty($res_token)) {
echo json_encode(array('status' => -1, 'msg' => '帐号认证过期!'));
exit();
}
// token验证通过 获取shop_id
$this->tokenShop_id = $res_token['shop_id'];
} catch (Exception $e) {
// 捕获解码过程中可能的异常,并返回错误信息
echo json_encode(array('status' => -1, 'msg' => 'Token 无效: ' . $e->getMessage()));
exit();
}
// token验证通过 获取shop_id
$this->tokenShop_id = $res_token['shop_id'];
}
/**
* @description: 总管理员 可接收任何shop_id 非总管理员 只可以调用自身shop_id 否则会中断