Pārlūkot izejas kodu

Merge remote-tracking branch 'origin/develop' into develop

# Conflicts:
#	src/main/java/com/smppw/analysis/client/FundApi.java
mozuwen 1 gadu atpakaļ
vecāks
revīzija
57174043e9

+ 0 - 5
src/main/java/com/smppw/analysis/client/FundPerformanceApi.java

@@ -82,9 +82,4 @@ public class FundPerformanceApi {
         Map<String, Object> data = this.service.rank(req);
         return ResultVo.ok(data);
     }
-
-    @GetMapping("str")
-    public String str() {
-        return "ok";
-    }
 }

+ 40 - 0
src/main/java/com/smppw/analysis/client/TestApi.java

@@ -0,0 +1,40 @@
+package com.smppw.analysis.client;
+
+import cn.hutool.core.map.MapUtil;
+import cn.hutool.crypto.SignUtil;
+import com.smppw.analysis.infrastructure.config.AnalysisProperty;
+import com.smppw.common.pojo.ResultVo;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.List;
+import java.util.Map;
+
+@RestController
+@RequestMapping("/v1/")
+public class TestApi {
+    private final AnalysisProperty property;
+
+    public TestApi(AnalysisProperty property) {
+        this.property = property;
+    }
+
+    /**
+     * 生成签名,测试用的
+     *
+     * @param params 请求参数,包含appKey
+     * @return /
+     */
+    @GetMapping("sign")
+    public ResultVo<String> generateSign(@RequestParam Map<String, Object> params) {
+        String appKey = MapUtil.getStr(params, "appKey");
+        params.entrySet().removeIf(e -> "appKey".equals(e.getKey()));
+        List<AnalysisProperty.AppSign> appSigns = this.property.getAppSigns();
+        String secret = appSigns.stream().filter(e -> appKey.equalsIgnoreCase(e.getAppKey()))
+                .map(AnalysisProperty.AppSign::getAppSecret).findFirst().orElse(null);
+        String sign = SignUtil.signParamsMd5(params, secret);
+        return ResultVo.ok(sign);
+    }
+}

+ 5 - 3
src/main/java/com/smppw/analysis/infrastructure/components/ApiSignInterceptor.java

@@ -38,7 +38,7 @@ public class ApiSignInterceptor implements HandlerInterceptor {
         }
         if (!this.property.getEnableSign()) {
             if (logger.isDebugEnabled()) {
-                logger.debug("所有接口不需要签名验证!");
+                logger.debug("接口不需要签名验证!");
             }
             return HandlerInterceptor.super.preHandle(request, response, handler);
         }
@@ -53,7 +53,8 @@ public class ApiSignInterceptor implements HandlerInterceptor {
             return false;
         }
         String appKey = request.getParameter("appKey");
-        AnalysisProperty.AppSign appSign = this.property.getAppSigns().stream().filter(e -> appKey.equals(e.getAppKey())).findFirst().orElse(null);
+        AnalysisProperty.AppSign appSign = this.property.getAppSigns().stream()
+                .filter(e -> appKey.equals(e.getAppKey())).findFirst().orElse(null);
         if (appSign == null) {
             this.writeFailJson(response, "appKey非法");
             return false;
@@ -68,7 +69,8 @@ public class ApiSignInterceptor implements HandlerInterceptor {
         }
         String appSecret = appSign.getAppSecret();
         Map<String, String[]> parameterMap = request.getParameterMap();
-        String paramsSign = SignUtil.signParamsMd5(parameterMap, appSecret, timestamp);
+        parameterMap.entrySet().removeIf(next -> "appKey".equalsIgnoreCase(next.getKey()));
+        String paramsSign = SignUtil.signParamsMd5(parameterMap, appSecret);
         if (!sign.equalsIgnoreCase(paramsSign)) {
             this.writeFailJson(response, "签名验证失败");
             return false;

+ 1 - 1
src/main/java/com/smppw/analysis/infrastructure/config/AnalysisConfig.java

@@ -17,7 +17,7 @@ public class AnalysisConfig implements WebMvcConfigurer {
     @Override
     public void addInterceptors(@NonNull InterceptorRegistry registry) {
         if (this.property.getEnableSign()) {
-            registry.addInterceptor(new ApiSignInterceptor(this.property)).addPathPatterns();
+            registry.addInterceptor(new ApiSignInterceptor(this.property)).addPathPatterns().excludePathPatterns("/v1/sign");
         }
     }
 }

+ 13 - 0
src/main/java/com/smppw/analysis/infrastructure/utils/CommonUtils.java

@@ -2,11 +2,14 @@ package com.smppw.analysis.infrastructure.utils;
 
 import cn.hutool.core.collection.ListUtil;
 import cn.hutool.core.util.StrUtil;
+import cn.hutool.crypto.SignUtil;
 import cn.hutool.json.JSONUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 public class CommonUtils {
     private static final Logger logger = LoggerFactory.getLogger(CommonUtils.class);
@@ -44,4 +47,14 @@ public class CommonUtils {
         }
         return null;
     }
+
+    public static void main(String[] args) {
+        String appSecret = "abcdef";
+        String timestamp = "1691991341095";
+        Map<String, Object> params = new HashMap<>();
+        params.put("secId", "MF00005JMD");
+        params.put("indexIds", ListUtil.of("IN00000008", "IN0000000M"));
+        String sign = SignUtil.signParamsMd5(params, appSecret, timestamp);
+        System.out.println(sign);
+    }
 }

+ 1 - 1
src/main/resources/application-toc.yaml

@@ -69,7 +69,7 @@ smppw:
       cache-type: redis # 缓存类型,支持redis和memory;如果是redis必须配置redis
       app-signs:
         - app-key: ppw-toc
-          app-secret: xxxxxx
+          app-secret: abcdef
           enabled: true
         - app-key: ppw-tob
       enable-sign: false

+ 2 - 2
src/main/resources/application.yaml

@@ -70,10 +70,10 @@ smppw:
     analysis:
       py-url: https://pymaster-test.simuwang.com/  # python服务调用地址
       cache-type: redis # 缓存类型,支持redis和memory;如果是redis必须配置redis
-      enable-sign: false
+      enable-sign: true
       app-signs:
         - app-key: ppw-toc
-          app-secret: xxxxxx
+          app-secret: abcdef
           enabled: true
           timeout: 360000 # 6分钟
         - app-key: ppw-tob