ソースを参照

中间件-修改验签(参数)中间件

sunxbiao 1 年間 前
コミット
b551f8b97b
共有2 個のファイルを変更した6 個の追加6 個の削除を含む
  1. 5 5
      app/common/middleware/SignatureMiddleware.php
  2. 1 1
      app/controller/api/Auth.php

+ 5 - 5
app/common/middleware/SignatureMiddleware.php

@@ -12,13 +12,13 @@ class SignatureMiddleware
12
         // 1. 获取加密签名
12
         // 1. 获取加密签名
13
         $sign = $request->header('X-Sign');
13
         $sign = $request->header('X-Sign');
14
         if (!$sign) {
14
         if (!$sign) {
15
-            return $this->reject('Missing signature', 401);
15
+            return $this->reject('缺少签名', 401);
16
         }
16
         }
17
 
17
 
18
         // 2. 读取私钥
18
         // 2. 读取私钥
19
         $privateKey = file_get_contents(env('RSA_PRIVATE_KEY_PATH'));
19
         $privateKey = file_get_contents(env('RSA_PRIVATE_KEY_PATH'));
20
         if (!$privateKey) {
20
         if (!$privateKey) {
21
-            return $this->reject('Server key error', 500);
21
+            return $this->reject('服务器密钥错误', 500);
22
         }
22
         }
23
 
23
 
24
         // 3. 解密数据
24
         // 3. 解密数据
@@ -26,7 +26,7 @@ class SignatureMiddleware
26
         openssl_private_decrypt(base64_decode($sign), $decrypted, $privateKey);
26
         openssl_private_decrypt(base64_decode($sign), $decrypted, $privateKey);
27
 
27
 
28
         if (!$decrypted || !strpos($decrypted, ':')) {
28
         if (!$decrypted || !strpos($decrypted, ':')) {
29
-            return $this->reject('Invalid signature', 403);
29
+            return $this->reject('无效签名', 403);
30
         }
30
         }
31
 
31
 
32
         // 4. 分离随机数和时间戳
32
         // 4. 分离随机数和时间戳
@@ -34,13 +34,13 @@ class SignatureMiddleware
34
 
34
 
35
         // 5. 验证时间有效性(5分钟内)
35
         // 5. 验证时间有效性(5分钟内)
36
         if (abs(time() - $timestamp / 1000) > 300) {
36
         if (abs(time() - $timestamp / 1000) > 300) {
37
-            return $this->reject('Request expired', 403);
37
+            return $this->reject('请求已过期', 403);
38
         }
38
         }
39
 
39
 
40
         // 6. 防重放攻击(检查nonce唯一性)
40
         // 6. 防重放攻击(检查nonce唯一性)
41
         $cacheKey = 'nonce_' . $nonce;
41
         $cacheKey = 'nonce_' . $nonce;
42
         if (Cache::has($cacheKey)) {
42
         if (Cache::has($cacheKey)) {
43
-            return $this->reject('Repeated request', 403);
43
+            return $this->reject('重复请求', 403);
44
         }
44
         }
45
         Cache::set($cacheKey, 1, 300); // 5分钟缓存
45
         Cache::set($cacheKey, 1, 300); // 5分钟缓存
46
 
46
 

+ 1 - 1
app/controller/api/Auth.php

@@ -1354,7 +1354,7 @@ class Auth extends BaseController
1354
             'code' => 200,
1354
             'code' => 200,
1355
             'msg' => 'success',
1355
             'msg' => 'success',
1356
             'data' => [
1356
             'data' => [
1357
-                'public_key' => $publicKey
1357
+                'publicKey' => $publicKey
1358
             ]
1358
             ]
1359
         ]);
1359
         ]);
1360
     }
1360
     }