Browse Source

add:实名三项服务

lileilei 2 years ago
parent
commit
6850633583

+ 1 - 0
health-code.iml

@@ -26,6 +26,7 @@
     <orderEntry type="inheritedJdk" />
     <orderEntry type="sourceFolder" forTests="false" />
     <orderEntry type="library" name="Maven: cn.hutool:hutool-all:5.7.0" level="project" />
+    <orderEntry type="library" name="Maven: cn.hutool:hutool-all:5.7.0" level="project" />
     <orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-starter-test:2.4.5" level="project" />
     <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.4.5" level="project" />
     <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:2.4.5" level="project" />

+ 44 - 0
src/main/java/com/yixin/ms/controller/RealNameAuthController.java

@@ -0,0 +1,44 @@
+package com.yixin.ms.controller;
+
+
+import com.yixin.ms.boot.restful.ServiceException;
+import com.yixin.ms.model.dto.IrsTripSendMsgDTO;
+import com.yixin.ms.model.enums.TokenEnum;
+import com.yixin.ms.server.RealNameServer;
+import com.yixin.ms.server.dto.RealNameCtidAuthDTO;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+
+/**
+ * @author lileilei
+ */
+@Slf4j
+@RequiredArgsConstructor(onConstructor = @__(@Autowired))
+@Api(tags = "实名三项 API")
+@RestController
+@RequestMapping("/real/name")
+public class RealNameAuthController {
+    /**
+     * 服务对象
+     */
+    private final RealNameServer realNameServer;
+
+    @ApiOperation("实名三项验证")
+    @PostMapping("/ctidAuth")
+    public String ctidAuth(@RequestBody RealNameCtidAuthDTO dto) {
+        if (TokenEnum.codeOf(dto.getToken()) == null) {
+            throw new ServiceException(700, "token有误,请检查");
+        }
+
+        //三方接口原输出内容返回
+        return realNameServer.ctidAuth(dto);
+    }
+}

+ 94 - 0
src/main/java/com/yixin/ms/server/RealNameServer.java

@@ -0,0 +1,94 @@
+package com.yixin.ms.server;
+
+import cn.hutool.cache.CacheUtil;
+import cn.hutool.cache.impl.TimedCache;
+import com.alibaba.fastjson.JSON;
+import com.google.common.collect.Maps;
+import com.yixin.ms.boot.restful.ServiceException;
+import com.yixin.ms.boot.uitls.DESUtil;
+import com.yixin.ms.server.dto.*;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+import org.springframework.web.client.RestTemplate;
+
+import java.util.HashMap;
+
+/**
+ * @author: lileilei
+ * @date: 2022/9/4 10:49
+ * @description: 实名三项服务
+ */
+@Component
+@Slf4j
+public class RealNameServer {
+    private static final String TOKEN_HEADERS_KEY = "real:name:token_session_id";
+
+    @Value("${real.name.appid}")
+    private String appid;
+    @Value("${real.name.appsecret}")
+    private String appsecret;
+    @Value("${real.name.deskey}")
+    private String deskey;
+    @Value("${real.name.tokenUrl}")
+    private String getTokenUrl;
+    @Value("${real.name.ctidAuthUrl}")
+    private String ctidAuthUrl;
+
+    @Autowired
+    private RestTemplate restTemplate;
+
+    /**
+     * 请求密钥有效时间为 7200s
+     *
+     * @see <a href="https://www.hutool.cn/docs/#/cache/TimedCache">定时缓存</a>
+     */
+    private static TimedCache<String, String> tokenCache = CacheUtil.newTimedCache(7200 * 1000);
+
+    public String getToken() {
+        String token = tokenCache.get(TOKEN_HEADERS_KEY, false);
+        if (token != null) {
+            return token;
+        }
+
+        try {
+            //获取token
+            String body = restTemplate.postForObject(getTokenUrl, new RealNameTokenDTO().setBody(new RealNameTokenBodyDTO().setAppid(appid).setSecret(appsecret)), String.class);
+            log.info("实名三项获取token信息响应内容:{}", body);
+            token = JSON.parseObject(body).getJSONObject("body").get("token").toString();
+            tokenCache.put(TOKEN_HEADERS_KEY, token);
+        } catch (Exception e) {
+            throw new ServiceException("实名三项三方服务异常");
+        }
+
+        return token;
+    }
+
+    /**
+     * 实名三项验证
+     *
+     * @param dto
+     * @return
+     */
+    public String ctidAuth(RealNameCtidAuthDTO dto) {
+        RealNameAuthBodyDTO authBodyDTO = new RealNameAuthBodyDTO();
+        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.setOther(new RealNameAuthOtherDTO().setFaceImg(dto.getFaceImg()));
+
+//        log.info("实名三项验证请求入参:{}", JSON.toJSONString(authBodyDTO));
+
+        String body = null;
+        try {
+            body = restTemplate.postForObject(ctidAuthUrl, authBodyDTO, String.class);
+            log.info("实名三项验证响应内容:{}", body);
+        } catch (Exception e) {
+            throw new ServiceException("实名三项三方服务异常");
+        }
+
+        return body;
+    }
+}

+ 19 - 0
src/main/java/com/yixin/ms/server/dto/RealNameAuthBodyDTO.java

@@ -0,0 +1,19 @@
+package com.yixin.ms.server.dto;
+
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+/**
+ * @author: lileilei
+ * @date: 2022/9/4 11:19
+ * @description:
+ */
+@Data
+@Accessors(chain = true)
+public class RealNameAuthBodyDTO {
+    private String token;
+
+    private String body;
+
+    private RealNameAuthOtherDTO other;
+}

+ 15 - 0
src/main/java/com/yixin/ms/server/dto/RealNameAuthOtherDTO.java

@@ -0,0 +1,15 @@
+package com.yixin.ms.server.dto;
+
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+/**
+ * @author: lileilei
+ * @date: 2022/9/4 11:19
+ * @description:
+ */
+@Data
+@Accessors(chain = true)
+public class RealNameAuthOtherDTO {
+    private String faceImg;
+}

+ 21 - 0
src/main/java/com/yixin/ms/server/dto/RealNameCtidAuthDTO.java

@@ -0,0 +1,21 @@
+package com.yixin.ms.server.dto;
+
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+/**
+ * @author: lileilei
+ * @date: 2022/9/4 11:19
+ * @description:
+ */
+@Data
+@Accessors(chain = true)
+public class RealNameCtidAuthDTO {
+    private String name;
+
+    private String idcard;
+
+    private String faceImg;
+
+    private String token;
+}

+ 16 - 0
src/main/java/com/yixin/ms/server/dto/RealNameTokenBodyDTO.java

@@ -0,0 +1,16 @@
+package com.yixin.ms.server.dto;
+
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+/**
+ * @author: lileilei
+ * @date: 2022/9/4 11:20
+ * @description:
+ */
+@Data
+@Accessors(chain = true)
+public class RealNameTokenBodyDTO {
+    private String appid;
+    private String secret;
+}

+ 15 - 0
src/main/java/com/yixin/ms/server/dto/RealNameTokenDTO.java

@@ -0,0 +1,15 @@
+package com.yixin.ms.server.dto;
+
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+/**
+ * @author: lileilei
+ * @date: 2022/9/4 11:19
+ * @description:
+ */
+@Data
+@Accessors(chain = true)
+public class RealNameTokenDTO {
+    private RealNameTokenBodyDTO body;
+}

+ 9 - 0
src/main/resources/application-dev.yml

@@ -152,3 +152,12 @@ scenic:
   verifyUrl: https://test.d6.com.cn/data/notify/demo/verify
   queryUrl: https://test.d6.com.cn/data/notify/demo/query
   consumeUrl: https://test.d6.com.cn/data/notify/demo/consume
+
+#实名三项服务
+real:
+  name:
+    appid: jzz0d565aa964665b96fc430b2d2b901
+    appsecret: f3353caa32e246488ad72164ab0f8a4b
+    deskey: 255784349b4c4096b6b703f2e0521572
+    tokenUrl: http://175.24.254.149:8916/auth/api/token
+    ctidAuthUrl: http://175.24.254.149:8916/auth/api/ctidAuth

+ 9 - 0
src/main/resources/application-prod.yml

@@ -149,3 +149,12 @@ scenic:
   queryUrl: https://api.d6.com.cn/openapi/v1/query
   consumeUrl: https://api.d6.com.cn/openapi/v1/consume
 
+#实名三项服务
+real:
+  name:
+    appid: jzz0d565aa964665b96fc430b2d2b901
+    appsecret: f3353caa32e246488ad72164ab0f8a4b
+    deskey: 255784349b4c4096b6b703f2e0521572
+    tokenUrl: http://175.24.254.149:8916/auth/api/token
+    ctidAuthUrl: http://175.24.254.149:8916/auth/api/ctidAuth
+

+ 9 - 0
src/main/resources/application-test.yml

@@ -160,3 +160,12 @@ scenic:
   queryUrl: https://api.d6.com.cn/openapi/v1/query
   consumeUrl: https://api.d6.com.cn/openapi/v1/consume
 
+#实名三项服务
+real:
+  name:
+    appid: jzz0d565aa964665b96fc430b2d2b901
+    appsecret: f3353caa32e246488ad72164ab0f8a4b
+    deskey: 255784349b4c4096b6b703f2e0521572
+    tokenUrl: http://175.24.254.149:8916/auth/api/token
+    ctidAuthUrl: http://175.24.254.149:8916/auth/api/ctidAuth
+