Ver código fonte

优化配置,避免测试服务抢微信的accessKey

ximinghao 3 meses atrás
pai
commit
9a37b7fa51

+ 25 - 0
src/main/java/com/skyversation/xjcy/config/ServiceConfiguration.java

@@ -0,0 +1,25 @@
+package com.skyversation.xjcy.config;
+
+import com.skyversation.xjcy.service.WeChatService;
+import com.skyversation.xjcy.service.WxAuthServiceMock;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+
+@Configuration
+public class ServiceConfiguration {
+
+    // 当 app.wechat.enabled 为 true 时,注入全功能服务
+    @Bean
+    @ConditionalOnProperty(name = "app.wechat.enabled", havingValue = "true")
+    public WeChatService fullWechatService() {
+        return new WeChatService();
+    }
+
+    // 当 app.wechat.enabled 为 false 或不存在时,注入降级服务 (matchIfMissing = true 确保了默认行为)
+    @Bean
+    @ConditionalOnProperty(name = "app.wechat.enabled", havingValue = "false", matchIfMissing = true)
+    public WeChatService noopWechatService() {
+        return new WxAuthServiceMock();
+    }
+}

+ 18 - 2
src/main/java/com/skyversation/xjcy/service/WeChatService.java

@@ -14,6 +14,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.util.LinkedMultiValueMap;
 import org.springframework.util.MultiValueMap;
 
+import java.nio.charset.StandardCharsets;
 import java.time.LocalDateTime;
 import java.util.HashMap;
 import java.util.List;
@@ -21,7 +22,6 @@ import java.util.Map;
 import java.util.Objects;
 
 @SuppressWarnings("SpellCheckingInspection")
-@Service
 public class WeChatService {
     @Getter
     @AllArgsConstructor
@@ -75,12 +75,28 @@ public class WeChatService {
                 body,
                 null
         );
+
+        if(Objects.requireNonNull(res.getHeaders().get(HttpHeaders.CONTENT_TYPE)).contains("application/json")){
+            if (res.getBody() != null) {
+                String json = new String(res.getBody(), StandardCharsets.UTF_8);
+                JSONObject jsonObject = JSONObject.parseObject(json);
+                String errcode = jsonObject.getString("errcode");
+                if (errcode != null&&errcode.equals("40001")) {
+                    refreshAccessToken();
+                    res = HttpUtil.requestPostWithJson(
+                            WX_ACODE_PATH + getAccessToken(),
+                            body,
+                            null
+                    );
+                }
+            }
+        }
+
         HttpHeaders headers = new HttpHeaders();
         copyHeader(headers, res.getHeaders(), HttpHeaders.CONTENT_TYPE);
         copyHeader(headers, res.getHeaders(), HttpHeaders.CONTENT_LENGTH);
 
         headers.add(HttpHeaders.CACHE_CONTROL,"public, max-age=3600");
-        headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN,"*");
         return new ResponseEntity<>(res.getBody(), headers, res.getStatusCode());
     }
 

+ 7 - 3
src/main/java/com/skyversation/xjcy/mock/WxAuthServiceMock.java → src/main/java/com/skyversation/xjcy/service/WxAuthServiceMock.java

@@ -1,12 +1,16 @@
-package com.skyversation.xjcy.mock;
+package com.skyversation.xjcy.service;
 
-import com.skyversation.xjcy.service.WeChatService;
+import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
 
-@Service
 public class WxAuthServiceMock extends WeChatService {
     @Override
     public Result wxLogin(String code) {
         return new Result(true,null,"abc1234567890defabc1234567890def","");
     }
+
+    @Override
+    public ResponseEntity<byte[]> createWxacode(String path, String scene) {
+        return null;
+    }
 }

+ 14 - 3
src/main/resources/application.yml

@@ -1,6 +1,7 @@
 server:
   port: 10020
-  servlet.context-path: /xjcy/
+  servlet:
+    context-path: /xjcy/
 spring:
   application:
     name: xjcy
@@ -36,8 +37,18 @@ app:
     message:
       key: ${ALIBABA_CLOUD_ACCESS_KEY_ID:any}
       secret: ${ALIBABA_CLOUD_ACCESS_KEY_SECRET:any}
-
     endpoint: ${ALIBABA_CLOUD_ENDPOINT:dysmsapi.aliyuncs.com}
     code:
       sign: ${XJCY_ALIBABA_SIGN:any}
-      template-code: ${XJCY_ALIBABA_CODE_TEMPLATE:any}
+      template-code: ${XJCY_ALIBABA_CODE_TEMPLATE:any}
+---
+spring:
+  config:
+    activate:
+      on-profile: "dev"
+app:
+  wechat:
+    appid: ungiven
+    secret-key: ungiven
+    enable: false
+#   微信小程序的鉴权要求统一的access token维护服务,所以在测试时不提供鉴权信息避免破坏正式环境的运行