多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
demo使用步骤 1.修改配置文件 2.执行demo类中的main方法 一、配置文件 com.nlp.ctiddemo.config.CtidConfig ``` // 服务器地址 public static String serverUrl = "https://open.digitalcitizen.com.cn:7280/idauth"; // 应用id public static String appId = ""; // 应用密钥 public static String appSecret = ""; // 应用加密密钥 public static String key = ""; ``` 二、demo入口 com.nlp.ctiddemo.app.Demo 1.身份证2项信息demo ``` public static void runDemo() { VerifyService verifyService = new VerifyService(); String authMode = "0x40"; String name = ""; String number = ""; String apply = verifyService.apply(authMode, null); System.out.println("申请接口返回的数据:" + apply); CommonResponse applyResponse = JSON.parseObject(apply, CommonResponse.class); String bizSerialNum = (String) ((Map) applyResponse.getData()).get("bizSerialNum"); String request = verifyService.request(bizSerialNum, authMode, null, name, number); System.out.println("认证接口返回的数据:" + request); } ``` 2.人脸+身份证2项信息 ``` public static void runDemo2() { VerifyService verifyService = new VerifyService(); String authMode = "0x42"; String name = "; String number = ""; String photoData = ""; String apply = verifyService.apply(authMode, null); System.out.println("申请接口返回的数据:" + apply); CommonResponse applyResponse = JSON.parseObject(apply, CommonResponse.class); String bizSerialNum = (String) ((Map) applyResponse.getData()).get("bizSerialNum"); String request = verifyService.request(bizSerialNum, authMode, photoData, name, number); System.out.println("认证接口返回的数据:" + request); } ``` 三、接口调用服务方法 1.获取token方法 com.nlp.ctiddemo.service.TokenService.getAccessToken() ``` public CommonResponse getAccessToken(String appId, String appSecret) { CommonResponse commonResponse = tokenMap.get(appId + appSecret); try { if (commonResponse != null) { Map<String, Object> data = (Map<String, Object>) commonResponse.getData(); Long expireTime = (Long) data.get("expireTime"); if (expireTime == null || expireTime < System.currentTimeMillis()) { commonResponse = null; } } } catch (Exception e) { commonResponse = null; } if (commonResponse != null) { return commonResponse; } Map<String, String> data = new HashMap<>(); data.put("appId", appId); data.put("appSecret", appSecret); String response = HttpUtil.post(CtidConfig.serverUrl + "/auth/getCode", JSON.toJSONString(data), null); commonResponse = JSON.parseObject(response, CommonResponse.class); if ("0".equals(commonResponse.getCode())) { tokenMap.put(appId + appSecret, commonResponse); } return commonResponse; } ``` 2.身份认证申请接口 com.nlp.ctiddemo.service.VerifyService.apply() ``` public String apply(String authMode, String deviceId) { String accessToken = tokenService.getAccessToken(); Map<String, String> headers = new HashMap<>(); headers.put("Authorization", accessToken); headers.put("appId", CtidConfig.appId); Map<String, String> body = new HashMap<>(); if (StringUtils.isNotBlank(deviceId)) { body.put("deviceId", deviceId); } String jsonData = JSON.toJSONString(body); String encryptString = AESUtil.encryptAES(jsonData, CtidConfig.key); String post = HttpUtil.post(CtidConfig.serverUrl + "/validateIdentity/apply/" + authMode, encryptString, headers); return AESUtil.decryptAES(post, CtidConfig.key); } ``` 3.身份认证请求接口 com.nlp.ctiddemo.service.VerifyService.request() ``` public String request(String bizSerialNum, String authMode, String photoData, String name, String number) { String accessToken = tokenService.getAccessToken(); Map<String, String> headers = new HashMap<>(); headers.put("Authorization", accessToken); headers.put("appId", CtidConfig.appId); Map<String, String> body = new HashMap<>(); body.put("bizSerialNum", bizSerialNum); body.put("authMode", authMode); body.put("name", name); body.put("number", number); if (StringUtils.isNotBlank(photoData)) { body.put("photoData", photoData); } String jsonData = JSON.toJSONString(body); String encryptString = AESUtil.encryptAES(jsonData, CtidConfig.key); String post = HttpUtil.post(CtidConfig.serverUrl + "/validateIdentity/request/" + authMode, encryptString, headers); return AESUtil.decryptAES(post, CtidConfig.key); } ```