Browse Source

fix:实名三项token机制更新

lileilei 2 years ago
parent
commit
7821f97a9f
1 changed files with 14 additions and 3 deletions
  1. 14 3
      src/main/java/com/yixin/ms/server/RealNameServer.java

+ 14 - 3
src/main/java/com/yixin/ms/server/RealNameServer.java

@@ -46,9 +46,9 @@ public class RealNameServer {
      */
     private static TimedCache<String, String> tokenCache = CacheUtil.newTimedCache(7200 * 1000);
 
-    public String getToken() {
+    public String getToken(Boolean cleanSession) {
         String token = tokenCache.get(TOKEN_HEADERS_KEY, false);
-        if (token != null) {
+        if (token != null && !cleanSession) {
             return token;
         }
 
@@ -76,7 +76,7 @@ public class RealNameServer {
         HashMap<Object, Object> bodyMap = Maps.newHashMap();
         bodyMap.put("name", dto.getName());
         bodyMap.put("idcard", dto.getIdcard());
-        authBodyDTO.setToken(this.getToken()).setBody(DESUtil.encrypt(JSON.toJSONString(bodyMap), deskey));
+        authBodyDTO.setToken(this.getToken(false)).setBody(DESUtil.encrypt(JSON.toJSONString(bodyMap), deskey));
         authBodyDTO.setOther(new RealNameAuthOtherDTO().setFaceImg(dto.getFaceImg()));
 
 //        log.info("实名三项验证请求入参:{}", JSON.toJSONString(authBodyDTO));
@@ -85,6 +85,17 @@ public class RealNameServer {
         try {
             body = restTemplate.postForObject(ctidAuthUrl, authBodyDTO, String.class);
             log.info("实名三项验证响应内容:{}", body);
+
+            String code = JSON.parseObject(body).getJSONObject("header").get("code").toString();
+            //token 过期标识 TODO 硬编码处理
+            if (code.equals("1000")) {
+                String token = this.getToken(true);
+                log.info("实名三项验证过期token:{}, 重新获取token: {}", authBodyDTO.getToken(), token);
+                authBodyDTO.setToken(token);
+                //token过期重新发起调用
+                body = restTemplate.postForObject(ctidAuthUrl, authBodyDTO, String.class);
+            }
+
         } catch (Exception e) {
             throw new ServiceException("实名三项三方服务异常");
         }