|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+<?php
|
|
|
2
|
+
|
|
|
3
|
+namespace app\common\middleware;
|
|
|
4
|
+
|
|
|
5
|
+use think\facade\Cache;
|
|
|
6
|
+use think\facade\Log;
|
|
|
7
|
+use think\Request;
|
|
|
8
|
+use think\Response;
|
|
|
9
|
+
|
|
|
10
|
+class SignatureMiddleware
|
|
|
11
|
+{
|
|
|
12
|
+ public function handle(Request $request, \Closure $next)
|
|
|
13
|
+ {
|
|
|
14
|
+ // 获取必要参数
|
|
|
15
|
+ $essential = $request->only(['timestamp', 'nonce', 'signature']);
|
|
|
16
|
+
|
|
|
17
|
+ // 验证必要参数
|
|
|
18
|
+ if (count(array_filter($essential)) !== 3) {
|
|
|
19
|
+ return $this->errorResponse('参数不完整', 400);
|
|
|
20
|
+ }
|
|
|
21
|
+
|
|
|
22
|
+ // 验证时间戳(5分钟内有效)
|
|
|
23
|
+ if (abs(time() - (int)$essential['timestamp']) > 300) {
|
|
|
24
|
+ return $this->errorResponse('请求已过期', 400);
|
|
|
25
|
+ }
|
|
|
26
|
+
|
|
|
27
|
+ // 验证nonce唯一性
|
|
|
28
|
+ $nonceKey = 'nonce:' . $essential['nonce'];
|
|
|
29
|
+ if (Cache::has($nonceKey)) {
|
|
|
30
|
+ return $this->errorResponse('重复请求', 400);
|
|
|
31
|
+ }
|
|
|
32
|
+
|
|
|
33
|
+ // 构造签名数据(与前端完全一致)
|
|
|
34
|
+ $signData = $request->param();
|
|
|
35
|
+ unset($signData['signature']);
|
|
|
36
|
+
|
|
|
37
|
+ // 按键名排序
|
|
|
38
|
+ ksort($signData);
|
|
|
39
|
+
|
|
|
40
|
+ // 生成签名字符串(与前端相同格式)
|
|
|
41
|
+ $signContent = '';
|
|
|
42
|
+ foreach ($signData as $key => $value) {
|
|
|
43
|
+ $signContent .= "{$key}={$value}&";
|
|
|
44
|
+ }
|
|
|
45
|
+ $signContent = rtrim($signContent, '&');
|
|
|
46
|
+
|
|
|
47
|
+ // 记录原始签名内容(用于调试)
|
|
|
48
|
+ Log::debug("Sign Content: " . $signContent);
|
|
|
49
|
+
|
|
|
50
|
+ // 获取公钥
|
|
|
51
|
+ $publicKey = openssl_pkey_get_public(
|
|
|
52
|
+ file_get_contents(env('RSA_PUBLIC_KEY_PATH'))
|
|
|
53
|
+ );
|
|
|
54
|
+
|
|
|
55
|
+ if (!$publicKey) {
|
|
|
56
|
+ Log::error("公钥加载失败");
|
|
|
57
|
+ return $this->errorResponse('系统错误', 500);
|
|
|
58
|
+ }
|
|
|
59
|
+
|
|
|
60
|
+ // 计算签名的MD5值(与前端一致)
|
|
|
61
|
+ $md5Hash = md5($signContent);
|
|
|
62
|
+ Log::debug("MD5 Hash: " . $md5Hash);
|
|
|
63
|
+
|
|
|
64
|
+ // 解码前端签名(前端使用公钥加密)
|
|
|
65
|
+ $signature = base64_decode($essential['signature']);
|
|
|
66
|
+
|
|
|
67
|
+ // 使用公钥解密签名
|
|
|
68
|
+ $decrypted = '';
|
|
|
69
|
+ $success = openssl_public_decrypt($signature, $decrypted, $publicKey);
|
|
|
70
|
+
|
|
|
71
|
+ if (!$success) {
|
|
|
72
|
+ Log::error("签名解密失败: " . openssl_error_string());
|
|
|
73
|
+ return $this->errorResponse('签名验证失败', 403);
|
|
|
74
|
+ }
|
|
|
75
|
+
|
|
|
76
|
+ Log::debug("解密结果: " . $decrypted);
|
|
|
77
|
+
|
|
|
78
|
+ // 比较解密后的值与MD5哈希
|
|
|
79
|
+ if ($decrypted !== $md5Hash) {
|
|
|
80
|
+ Log::error("签名验证失败: 期望 {$md5Hash}, 实际 {$decrypted}");
|
|
|
81
|
+ return $this->errorResponse('签名验证失败', 403);
|
|
|
82
|
+ }
|
|
|
83
|
+
|
|
|
84
|
+ // 记录已使用的nonce(5分钟过期)
|
|
|
85
|
+ Cache::set($nonceKey, 1, 300);
|
|
|
86
|
+
|
|
|
87
|
+ return $next($request);
|
|
|
88
|
+ }
|
|
|
89
|
+
|
|
|
90
|
+ private function errorResponse(string $message, int $code): Response
|
|
|
91
|
+ {
|
|
|
92
|
+ return json([
|
|
|
93
|
+ 'code' => $code,
|
|
|
94
|
+ 'msg' => $message,
|
|
|
95
|
+ 'data' => null
|
|
|
96
|
+ ])->code($code);
|
|
|
97
|
+ }
|
|
|
98
|
+}
|