新增人脸核身解密功能

This commit is contained in:
gy b
2023-10-11 18:11:32 +08:00
parent 87eb8c6cdf
commit d40fbccf24
6 changed files with 143 additions and 55 deletions
@@ -26,8 +26,7 @@ import com.ruoyi.system.service.ISysMenuService;
* @author ruoyi * @author ruoyi
*/ */
@RestController @RestController
public class SysLoginController public class SysLoginController {
{
@Autowired @Autowired
private SysLoginService loginService; private SysLoginService loginService;
@@ -37,7 +36,7 @@ public class SysLoginController
@Autowired @Autowired
private SysPermissionService permissionService; private SysPermissionService permissionService;
@Autowired @Autowired
private IdentityAuthenticationService identityAuthenticationService; IdentityAuthenticationService identityAuthenticationService;
/** /**
* 登录方法 * 登录方法
@@ -46,20 +45,17 @@ public class SysLoginController
* @return 结果 * @return 结果
*/ */
@PostMapping("/login") @PostMapping("/login")
public AjaxResult login(@RequestBody LoginBody loginBody) public AjaxResult login(@RequestBody LoginBody loginBody) {
{
AjaxResult ajax = AjaxResult.success(); AjaxResult ajax = AjaxResult.success();
// 生成令牌 // 生成令牌
String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
loginBody.getUuid()); loginBody.getUuid());
ajax.put(Constants.TOKEN, token); ajax.put(Constants.TOKEN, token);
//判断该用户是否已经实名认证(certificationStatus1已认证0未认证)
// IdentityAuthentication identityAuthentication = new IdentityAuthentication(); IdentityAuthentication identityAuthentication=new IdentityAuthentication();
// identityAuthentication.setUserName(loginBody.getUsername()); identityAuthentication.setUserName(loginBody.getUsername());
// IdentityAuthentication identityAuthenticationselect = identityAuthenticationService.selectIdentityAuthentication(identityAuthentication); String status = identityAuthenticationService.checkIsAuthentication(identityAuthentication);
// ajax.put("certificationStatusName", identityAuthenticationselect.getCertificationStatusName()); ajax.put("certificationStatus", status);
// ajax.put("certificationStatus", identityAuthenticationselect.getCertificationStatus());
return ajax; return ajax;
} }
@@ -69,8 +65,7 @@ public class SysLoginController
* @return 用户信息 * @return 用户信息
*/ */
@GetMapping("getInfo") @GetMapping("getInfo")
public AjaxResult getInfo() public AjaxResult getInfo() {
{
SysUser user = SecurityUtils.getLoginUser().getUser(); SysUser user = SecurityUtils.getLoginUser().getUser();
// 角色集合 // 角色集合
Set<String> roles = permissionService.getRolePermission(user); Set<String> roles = permissionService.getRolePermission(user);
@@ -89,8 +84,7 @@ public class SysLoginController
* @return 路由信息 * @return 路由信息
*/ */
@GetMapping("getRouters") @GetMapping("getRouters")
public AjaxResult getRouters() public AjaxResult getRouters() {
{
Long userId = SecurityUtils.getUserId(); Long userId = SecurityUtils.getUserId();
List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId); List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
return AjaxResult.success(menuService.buildMenus(menus)); return AjaxResult.success(menuService.buildMenus(menus));
@@ -3,7 +3,6 @@ package com.ruoyi.web.controller.wisdomarbitrate;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.wisdomarbitrate.domain.CaseApplication;
import com.ruoyi.wisdomarbitrate.domain.IdentityAuthentication; import com.ruoyi.wisdomarbitrate.domain.IdentityAuthentication;
import com.ruoyi.wisdomarbitrate.service.IdentityAuthenticationService; import com.ruoyi.wisdomarbitrate.service.IdentityAuthenticationService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -15,7 +14,7 @@ import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
@RequestMapping("/identityAuthentication") @RequestMapping("/identityAuthentication")
public class IdentityAuthenticationController extends BaseController { public class IdentityAuthenticationController extends BaseController {
@Autowired @Autowired
private IdentityAuthenticationService identityAuthenticationService; private IdentityAuthenticationService identityAuthenticationService;
@@ -23,8 +22,7 @@ public class IdentityAuthenticationController extends BaseController {
* 获取EIDtoken * 获取EIDtoken
*/ */
@PostMapping("/selectIdentityAuthenticaEIDtoken") @PostMapping("/selectIdentityAuthenticaEIDtoken")
public AjaxResult selectIdentityAuthenticaEIDtoken() public AjaxResult selectIdentityAuthenticaEIDtoken() {
{
JSONObject tokenResult = identityAuthenticationService.selectIdentityAuthenticaEIDtoken(); JSONObject tokenResult = identityAuthenticationService.selectIdentityAuthenticaEIDtoken();
return success(tokenResult); return success(tokenResult);
} }
@@ -33,22 +31,10 @@ public class IdentityAuthenticationController extends BaseController {
* 小程序人脸核身后查询身份认证结果 * 小程序人脸核身后查询身份认证结果
*/ */
@PostMapping("/selectIdentityAuthenticaRespon") @PostMapping("/selectIdentityAuthenticaRespon")
public AjaxResult selectIdentityAuthenticaRespon(@Validated @RequestBody IdentityAuthentication ientityAuthentication) public AjaxResult selectIdentityAuthenticaRespon(@Validated @RequestBody IdentityAuthentication ientityAuthentication) {
{ AjaxResult checkResult = identityAuthenticationService.selectIdentityAuthenticaRespon(ientityAuthentication);
// String username = this.getUsername(); return checkResult;
// Long userId = this.getUserId();
// ientityAuthentication.setUserId(userId);
// ientityAuthentication.setUserName(username);
JSONObject checkResult = identityAuthenticationService.selectIdentityAuthenticaRespon( ientityAuthentication);
return success(checkResult);
} }
} }
@@ -166,4 +166,4 @@ identityAuthentication:
credentialSecretId: AKID3xfHgroY4MQHvLXUXMwIQL1UjmbBX1Tv credentialSecretId: AKID3xfHgroY4MQHvLXUXMwIQL1UjmbBX1Tv
credentialSecretKey: INDrIXcT8YmomZBcsy0oNirnU0LTN4X7 credentialSecretKey: INDrIXcT8YmomZBcsy0oNirnU0LTN4X7
merchantId: 0NSJ2309281116194321 merchantId: 0NSJ2309281116194321
privateKeyHexDecodeinfo: MHcCAQEEIEw7MRv3uYlpmU6Fko4GlXSh6Vd38k0cUQZ5zDwvRg+voAoGCCqBHM9VAYItoUQDQgAEUdxIAWhGg4LUXf1GoPdb8XMbGudpexPQCuaaRi9BCnNbpaF1kcwRhhsBKvop9ZmW/nOz4wQ1r/iIEOrc9qCXgQ== privateKeyHexDecodeinfo: 4c3b311bf7b98969994e85928e069574a1e95777f24d1c510679cc3c2f460faf
+6 -6
View File
@@ -60,11 +60,11 @@
</dependency> </dependency>
<!-- 动态数据源 --> <!-- 动态数据源 -->
<dependency> <dependency>
<groupId>com.baomidou</groupId> <groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.2</version> <version>3.5.2</version>
</dependency> </dependency>
<!-- 阿里JSON解析器 --> <!-- 阿里JSON解析器 -->
<dependency> <dependency>
@@ -144,7 +144,7 @@
<artifactId>tencentcloud-sdk-java-faceid</artifactId> <artifactId>tencentcloud-sdk-java-faceid</artifactId>
<version>3.1.875</version> <version>3.1.875</version>
</dependency> </dependency>
<!--用户信息解密-->
<dependency> <dependency>
<groupId>cn.hutool</groupId> <groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId> <artifactId>hutool-all</artifactId>
@@ -1,6 +1,7 @@
package com.ruoyi.wisdomarbitrate.service; package com.ruoyi.wisdomarbitrate.service;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.wisdomarbitrate.domain.IdentityAuthentication; import com.ruoyi.wisdomarbitrate.domain.IdentityAuthentication;
public interface IdentityAuthenticationService { public interface IdentityAuthenticationService {
@@ -8,6 +9,14 @@ public interface IdentityAuthenticationService {
IdentityAuthentication selectIdentityAuthentication(IdentityAuthentication identityAuthentication); IdentityAuthentication selectIdentityAuthentication(IdentityAuthentication identityAuthentication);
/**
* 检查是否已经认证的用户
*
* @param identityAuthentication
* @return
*/
String checkIsAuthentication(IdentityAuthentication identityAuthentication);
/** /**
* 获取Eidtoken * 获取Eidtoken
* *
@@ -21,5 +30,5 @@ public interface IdentityAuthenticationService {
* @param ientityAuthentication * @param ientityAuthentication
* @return * @return
*/ */
JSONObject selectIdentityAuthenticaRespon(IdentityAuthentication ientityAuthentication); AjaxResult selectIdentityAuthenticaRespon(IdentityAuthentication ientityAuthentication);
} }
@@ -1,10 +1,15 @@
package com.ruoyi.wisdomarbitrate.service.impl; package com.ruoyi.wisdomarbitrate.service.impl;
import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.SM2;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.wisdomarbitrate.domain.IdentityAuthentication; import com.ruoyi.wisdomarbitrate.domain.IdentityAuthentication;
import com.ruoyi.wisdomarbitrate.mapper.IdentityAuthenticationMapper; import com.ruoyi.wisdomarbitrate.mapper.IdentityAuthenticationMapper;
import com.ruoyi.wisdomarbitrate.service.IdentityAuthenticationService; import com.ruoyi.wisdomarbitrate.service.IdentityAuthenticationService;
@@ -24,6 +29,8 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
@Service @Service
public class IdentityAuthenticationServiceImpl implements IdentityAuthenticationService { public class IdentityAuthenticationServiceImpl implements IdentityAuthenticationService {
@@ -58,6 +65,27 @@ public class IdentityAuthenticationServiceImpl implements IdentityAuthentication
} }
/**
* 检查是否已经认证的用户
*
* @param identityAuthentication
* @return
*/
@Override
public String checkIsAuthentication(IdentityAuthentication identityAuthentication) {
IdentityAuthentication identityAuthenticationselect = identityAuthenticationMapper.selectIdentityAuthentication(identityAuthentication);
if (identityAuthenticationselect != null) {
return "1";
} else {
return "0";
}
}
/**
* 获取EIDtoken
*
* @return
*/
@Override @Override
public JSONObject selectIdentityAuthenticaEIDtoken() { public JSONObject selectIdentityAuthenticaEIDtoken() {
JSONObject objJSON = new JSONObject(); JSONObject objJSON = new JSONObject();
@@ -74,10 +102,10 @@ public class IdentityAuthenticationServiceImpl implements IdentityAuthentication
FaceidClient client = new FaceidClient(cred, "", clientProfile); FaceidClient client = new FaceidClient(cred, "", clientProfile);
// 实例化一个请求对象,每个接口都会对应一个request对象 // 实例化一个请求对象,每个接口都会对应一个request对象
GetEidTokenRequest req = new GetEidTokenRequest(); GetEidTokenRequest req = new GetEidTokenRequest();
req.setMerchantId(merchantId);
// 返回的resp是一个GetEidTokenResponse的实例,与请求对象对应 // 返回的resp是一个GetEidTokenResponse的实例,与请求对象对应
GetEidTokenResponse resp = client.GetEidToken(req); GetEidTokenResponse resp = client.GetEidToken(req);
// 输出json格式的字符串回包 // 输出json格式的字符串回包
System.out.println(GetEidTokenResponse.toJsonString(resp));
String respJSON = GetEidTokenResponse.toJsonString(resp); String respJSON = GetEidTokenResponse.toJsonString(resp);
objJSON = JSON.parseObject(respJSON); objJSON = JSON.parseObject(respJSON);
} catch (TencentCloudSDKException e) { } catch (TencentCloudSDKException e) {
@@ -87,9 +115,37 @@ public class IdentityAuthenticationServiceImpl implements IdentityAuthentication
return objJSON; return objJSON;
} }
/**
* 解密用户信息
*/
public JSONObject DecodeUserInfo(String deskey, String userInfo) {
JSONObject parse = null;
try {
byte[] desKeyBytes = Base64.decode(deskey);
final SM2 sm2 = new SM2(privateKeyHexDecodeinfo, null, null);
sm2.usePlainEncoding();
byte[] sm4KeyBytes = sm2.decrypt(desKeyBytes);
SymmetricCrypto sm4 = SmUtil.sm4(sm4KeyBytes);
byte[] plaintext = sm4.decrypt(Base64.decode(userInfo));
if (plaintext != null && plaintext.length > 0) {
String s = new String(plaintext);
parse = JSON.parseObject(s);
}
} catch (Exception e) {
System.out.println(e.toString());
}
return parse;
}
/**
* 小程序人脸核身后查询身份认证结果
*
* @param ientityAuthentication
* @return
*/
@Override @Override
@Transactional @Transactional
public JSONObject selectIdentityAuthenticaRespon(IdentityAuthentication ientityAuthentication) { public AjaxResult selectIdentityAuthenticaRespon(IdentityAuthentication ientityAuthentication) {
String eidToken = ientityAuthentication.getEidToken(); String eidToken = ientityAuthentication.getEidToken();
try { try {
@@ -108,10 +164,53 @@ public class IdentityAuthenticationServiceImpl implements IdentityAuthentication
// 返回的resp是一个GetEidResultResponse的实例,与请求对象对应 // 返回的resp是一个GetEidResultResponse的实例,与请求对象对应
GetEidResultResponse resp = client.GetEidResult(req); GetEidResultResponse resp = client.GetEidResult(req);
// 输出json格式的字符串回包 // 输出json格式的字符串回包
System.out.println(GetEidResultResponse.toJsonString(resp));
String s = GetEidResultResponse.toJsonString(resp); String s = GetEidResultResponse.toJsonString(resp);
JSONObject object = JSON.parseObject(s); JSONObject objJSON = JSON.parseObject(s);
return object; //查看是否核验成功
JSONObject text = objJSON.getJSONObject("Text");
if (text != null) {
Integer comparestatus = text.getInteger("Comparestatus");
if (comparestatus != null && comparestatus == 0) {
JSONObject eidInfo = objJSON.getJSONObject("EidInfo");
if (eidInfo != null) {
String desKey = eidInfo.getString("DesKey");
String userInfo = eidInfo.getString("UserInfo");
//1.解密用户的信息
JSONObject info = DecodeUserInfo(desKey, userInfo);
if (info != null) {
String idcardno = info.getString("idnum");
String name = info.getString("name");
//2.在用户认证表中插入用户认证记录
LoginUser loginUser = SecurityUtils.getLoginUser();
IdentityAuthentication authentication = new IdentityAuthentication();
/**
* 用户名
* 用户名id
* 姓名
* 身份证号
* 认证时间
* 认证状态0表示成功
* 请求id
*/
authentication.setUserName(loginUser.getUsername());
authentication.setUserId(loginUser.getUserId());
authentication.setName(name);
authentication.setIdentityNo(idcardno);
authentication.setCertificationTime(new Date());
authentication.setCertificationStatus(0);
authentication.setRequestId(objJSON.getString("RequestId"));
try {
identityAuthenticationMapper.insertIdentityAuthentication(authentication);
} catch (Exception e) {
System.out.println("认证记录新增失败");
}
}
}
}
}
return AjaxResult.success();
} catch (TencentCloudSDKException e) { } catch (TencentCloudSDKException e) {
System.out.println(e.toString()); System.out.println(e.toString());
} }