|
|
@@ -0,0 +1,74 @@
|
|
|
+package com.etotem.cfc.controller.huawei;
|
|
|
+
|
|
|
+import com.etotem.cfc.service.huawei.HuaweiSubscriberSignature;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 华为 Health Kit 订阅通知回调端点(spec 2026-11-23)。
|
|
|
+ *
|
|
|
+ * <p>路径固定为 {@code /healthkit/notifications}(华为要求全小写结尾),不在 {@code /api/} 前缀下,
|
|
|
+ * 故不经过 JWT 拦截(华为服务器无法携带我方 token)。连通性测试 Huawei 发
|
|
|
+ * {@code CONNECTION_VERIFY_EVENT$VERIFY} 通知,本端点返回 204 即视为连通成功。</p>
|
|
|
+ *
|
|
|
+ * <p>验签策略(可选):配置了 subscriber Secret 时校验 {@code x-notification-signature}
|
|
|
+ *(HMAC-SHA256 + Base64),验签失败返回 404;未配置 Secret 时跳过验签(便于先跑通连通性)。</p>
|
|
|
+ *
|
|
|
+ * <p>本次只回 204 + 记日志,真实通知的业务处理(按 openId 拉取数据)后续排期。</p>
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@RestController
|
|
|
+public class HealthKitNotificationController {
|
|
|
+
|
|
|
+ /** 华为开发者联盟订阅者卡片的 Base64 Secret;为空则跳过验签。 */
|
|
|
+ @Value("${huawei.health.subscriber-secret:}")
|
|
|
+ private String subscriberSecret;
|
|
|
+
|
|
|
+ @PostMapping("/healthkit/notifications")
|
|
|
+ public ResponseEntity<Void> notifications(
|
|
|
+ @RequestBody(required = false) Map<String, Object> body,
|
|
|
+ @RequestHeader(value = "x-notification-signature", required = false) String signature) {
|
|
|
+ String openId = body == null ? null : str(body.get("openId"));
|
|
|
+ String eventType = body == null ? null : str(body.get("eventType"));
|
|
|
+ String eventTime = body == null ? null : str(body.get("eventTime"));
|
|
|
+
|
|
|
+ boolean accepted = shouldAccept(subscriberSecret, signature, openId, eventType, eventTime);
|
|
|
+ if (!accepted) {
|
|
|
+ log.warn("[healthkit] 订阅通知验签失败, 拒绝: eventType={}, openId={}", eventType, openId);
|
|
|
+ return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isConnectionVerify(eventType)) {
|
|
|
+ log.info("[healthkit] 连通性测试通知通过: eventType={}, openId={}", eventType, openId);
|
|
|
+ } else {
|
|
|
+ // 业务处理(按 openId 定位成员并拉取数据)后续排期;本次仅记录
|
|
|
+ log.info("[healthkit] 收到订阅通知(待业务处理): eventType={}, openId={}, eventTime={}, metaData={}",
|
|
|
+ eventType, openId, eventTime, body == null ? null : body.get("metaData"));
|
|
|
+ }
|
|
|
+ return ResponseEntity.noContent().build(); // 204
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 是否接受该通知:未配置 Secret→跳过验签放行;配置了→验签通过才放行。
|
|
|
+ * 静态便于单测(不依赖 Spring 上下文)。
|
|
|
+ */
|
|
|
+ public static boolean shouldAccept(String secret, String signature,
|
|
|
+ String openId, String eventType, String eventTime) {
|
|
|
+ if (secret == null || secret.trim().isEmpty()) return true;
|
|
|
+ return HuaweiSubscriberSignature.verify(secret, openId, eventType, eventTime, signature);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 是否为连通性测试事件。 */
|
|
|
+ public static boolean isConnectionVerify(String eventType) {
|
|
|
+ return "CONNECTION_VERIFY_EVENT$VERIFY".equals(eventType);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String str(Object o) {
|
|
|
+ return o == null ? null : String.valueOf(o);
|
|
|
+ }
|
|
|
+}
|