Explorar o código

身份证验证

wangqifan %!s(int64=3) %!d(string=hai) anos
pai
achega
f9ee4c293b
Modificáronse 1 ficheiros con 69 adicións e 14 borrados
  1. 69 14
      src/main/java/com/izouma/nineth/service/IdentityAuthService.java

+ 69 - 14
src/main/java/com/izouma/nineth/service/IdentityAuthService.java

@@ -3,6 +3,8 @@ package com.izouma.nineth.service;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.fastjson.serializer.SerializerFeature;
+import com.aliyun.oss.common.utils.HttpUtil;
+import com.aliyuncs.utils.HttpsUtils;
 import com.github.kevinsawicki.http.HttpRequest;
 import com.izouma.nineth.annotations.RedisLock;
 import com.izouma.nineth.domain.IdentityAuth;
@@ -13,9 +15,12 @@ import com.izouma.nineth.exception.BusinessException;
 import com.izouma.nineth.repo.IdentityAuthRepo;
 import com.izouma.nineth.repo.UserRepo;
 import com.izouma.nineth.utils.DateTimeUtils;
+import com.izouma.nineth.utils.HttpUtils;
 import com.izouma.nineth.utils.JpaUtils;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.http.HttpResponse;
+import org.apache.http.util.EntityUtils;
 import org.springframework.core.env.Environment;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageRequest;
@@ -46,7 +51,8 @@ public class IdentityAuthService {
     private CacheService                  cacheService;
 
     public Page<IdentityAuth> all(PageQuery pageQuery) {
-        return identityAuthRepo.findAll(JpaUtils.toSpecification(pageQuery, IdentityAuth.class), JpaUtils.toPageRequest(pageQuery));
+        return identityAuthRepo
+                .findAll(JpaUtils.toSpecification(pageQuery, IdentityAuth.class), JpaUtils.toPageRequest(pageQuery));
     }
 
     public void apply(IdentityAuth identityAuth) {
@@ -124,22 +130,69 @@ public class IdentityAuthService {
 //            }
 //        }
 //    }
+    public void validateV2(String name, String phone, String idno) {
+        String host = "https://mobilecert.market.alicloudapi.com";
+        String path = "/mobile3MetaSimple";
+        String method = "GET";
+        String appcode = "af29c2d37c4f415fac930d82f01fb559";
+        Map<String, String> headers = new HashMap<String, String>();
+        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
+        headers.put("Authorization", "APPCODE " + appcode);
+        Map<String, String> querys = new HashMap<String, String>();
+        querys.put("identifyNum", idno);
+        querys.put("mobile", phone);
+        querys.put("userName", name);
+
+
+        try {
+            /**
+             * 重要提示如下:
+             * HttpUtils请从
+             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
+             * 下载
+             *
+             * 相应的依赖请参照
+             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
+             */
+            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
+            System.out.println(response.toString());
+            //获取response的body
+            JSONObject jsonObject = JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
+            log.info("validate {} {} \n{}", name, idno, JSON.toJSONString(jsonObject, SerializerFeature.PrettyFormat));
+            if (jsonObject.getInteger("code") != 200) {
+                String msg = jsonObject.getString("message");
+                throw new BusinessException(msg);
+            } else {
+                JSONObject data = jsonObject.getJSONObject("data");
+                Integer bizCode = Optional.ofNullable(data.getInteger("bizCode")).orElse(3);
+                if (bizCode != 1) {
+                    throw new BusinessException("不匹配");
+                } else {
+                    log.info("{} {} {} 实名认证通过", name, phone, idno);
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
     public void validate(String name, String phone, String idno) {
-        String body = HttpRequest.post("https://zid.market.alicloudapi.com/idcheck/Post")
-                .header("Authorization", "APPCODE b48bc8f6759345a79ae20a951f03dabe")
-                .contentType(HttpRequest.CONTENT_TYPE_FORM)
-                .form("cardNo", idno)
-                .form("realName", name)
+        String body = HttpRequest.get("https://mobilecert.market.alicloudapi.com/mobile3MetaSimple")
+                .header("Authorization", "APPCODE af29c2d37c4f415fac930d82f01fb559")
+                .contentType("text/html; charset=utf-8")
+                .form("identifyNum", idno)
+                .form("userName", name)
+                .form("mobile", phone)
                 .body();
         JSONObject jsonObject = JSONObject.parseObject(body);
         log.info("validate {} {} \n{}", name, idno, JSON.toJSONString(jsonObject, SerializerFeature.PrettyFormat));
-        if (jsonObject.getInteger("error_code") != 0) {
-            String msg = jsonObject.getString("reason");
+        if (jsonObject.getInteger("code") != 200) {
+            String msg = jsonObject.getString("message");
             throw new BusinessException(msg);
         } else {
-            JSONObject data = jsonObject.getJSONObject("result");
-            boolean isOK = Optional.ofNullable(data.getBoolean("isok")).orElse(Boolean.FALSE);
-            if (!isOK) {
+            JSONObject data = jsonObject.getJSONObject("data");
+            Integer bizCode = Optional.ofNullable(data.getInteger("bizCode")).orElse(3);
+            if (bizCode != 1) {
                 throw new BusinessException("不匹配");
             } else {
                 log.info("{} {} {} 实名认证通过", name, phone, idno);
@@ -225,8 +278,10 @@ public class IdentityAuthService {
         User user = userRepo.findById(identityAuth.getUserId()).orElseThrow(new BusinessException("用户不存在"));
         if (user.getAuthStatus() == AuthStatus.SUCCESS) {
             result.put("status", AuthStatus.SUCCESS);
-        } else if (!Pattern.matches("[1-9]{1}[0-9]{5}(19|20)[0-9]{2}((0[1-9]{1})|(1[0-2]{1}))((0[1-9]{1})|([1-2]{1}[0-9]{1}|(3[0-1]{1})))[0-9]{3}[0-9x]{1}", identityAuth.getIdNo()
-                .toLowerCase())) {
+        } else if (!Pattern
+                .matches("[1-9]{1}[0-9]{5}(19|20)[0-9]{2}((0[1-9]{1})|(1[0-2]{1}))((0[1-9]{1})|([1-2]{1}[0-9]{1}|(3[0-1]{1})))[0-9]{3}[0-9x]{1}", identityAuth
+                        .getIdNo()
+                        .toLowerCase())) {
             result.put("status", AuthStatus.FAIL);
             result.put("reason", "身份证格式错误");
         } else {
@@ -246,7 +301,7 @@ public class IdentityAuthService {
                     result.put("reason", "同一身份证注册超过3个");
                 } else {
                     try {
-                        validate(identityAuth.getRealName(), user.getPhone(), identityAuth.getIdNo());
+                        validateV2(identityAuth.getRealName(), user.getPhone(), identityAuth.getIdNo());
                         result.put("status", AuthStatus.SUCCESS);
                     } catch (Exception e) {
                         log.error("自动实名出错", e);