Explorar el Código

feat(recommend): 数仓同步 Controller(POST /api/recommend/rule/sync + Token鉴权)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
jintao.geng hace 1 mes
padre
commit
2dab2fa5f1

+ 52 - 0
mango-application/src/main/java/com/mangoo/rating/recommend/app/controller/RuleSyncController.java

@@ -0,0 +1,52 @@
+package com.mangoo.rating.recommend.app.controller;
+
+import com.mangoo.rating.recommend.AjaxResult;
+import com.mangoo.rating.recommend.annotation.ApiLog;
+import com.mangoo.rating.recommend.config.RecommendProperties;
+import com.mangoo.rating.recommend.enums.BusinessType;
+import com.mangoo.rating.recommend.request.recommend.RuleSyncRequest;
+import com.mangoo.rating.recommend.service.RuleSyncService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestHeader;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.annotation.Resource;
+
+/**
+ * 数仓规则同步接口(内部,Token 鉴权)
+ *
+ * @author gengjintao
+ * @date 2026/06/25
+ */
+@Slf4j
+@Api(tags = "内部-数仓规则同步接口")
+@RestController
+@RequestMapping("/api/recommend/rule")
+public class RuleSyncController {
+
+    @Resource
+    private RuleSyncService ruleSyncService;
+
+    @Resource
+    private RecommendProperties recommendProperties;
+
+    @ApiOperation("数仓推送全量规则同步")
+    @PostMapping("/sync")
+    @ApiLog(title = "数仓规则同步", businessType = BusinessType.IMPORT)
+    public AjaxResult sync(@RequestHeader(value = "X-Sync-Token", required = false) String token,
+                           @Validated @RequestBody RuleSyncRequest request) {
+        // Token 鉴权:防止内部同步接口被外部乱调写脏数据
+        String expected = recommendProperties.getSyncToken();
+        if (expected == null || !expected.equals(token)) {
+            log.warn("数仓同步鉴权失败,token 不匹配");
+            return AjaxResult.error("鉴权失败");
+        }
+        return ruleSyncService.sync(request);
+    }
+}

+ 1 - 0
mango-application/src/main/resources/application.yml

@@ -350,6 +350,7 @@ rating-app:
 recommend:
   default-efficiency: 1
   batch-max: 50
+  sync-token: ${RECOMMEND_SYNC_TOKEN:change-me-sync-token}
   time-limit-mapping:
     "普通": 1
     "快速": 2

+ 3 - 0
mango-common/src/main/java/com/mangoo/rating/recommend/config/RecommendProperties.java

@@ -29,4 +29,7 @@ public class RecommendProperties {
      * 例:「普通: 1」「快速: 2」「闪评: 3」「7个工作日: 1」「1个工作日: 3」
      */
     private Map<String, Integer> timeLimitMapping = new LinkedHashMap<>();
+
+    /** 数仓同步接口鉴权 Token(请求头 X-Sync-Token) */
+    private String syncToken = "change-me-sync-token";
 }