소스 검색

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

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