Quellcode durchsuchen

新增人脸核身解密功能

gy b vor 2 Jahren
Ursprung
Commit
d40fbccf24

+ 14
- 20
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java Datei anzeigen

@@ -22,12 +22,11 @@ import com.ruoyi.system.service.ISysMenuService;
22 22
 
23 23
 /**
24 24
  * 登录验证
25
- * 
25
+ *
26 26
  * @author ruoyi
27 27
  */
28 28
 @RestController
29
-public class SysLoginController
30
-{
29
+public class SysLoginController {
31 30
     @Autowired
32 31
     private SysLoginService loginService;
33 32
 
@@ -37,40 +36,36 @@ public class SysLoginController
37 36
     @Autowired
38 37
     private SysPermissionService permissionService;
39 38
     @Autowired
40
-    private IdentityAuthenticationService identityAuthenticationService;
39
+    IdentityAuthenticationService identityAuthenticationService;
41 40
 
42 41
     /**
43 42
      * 登录方法
44
-     * 
43
+     *
45 44
      * @param loginBody 登录信息
46 45
      * @return 结果
47 46
      */
48 47
     @PostMapping("/login")
49
-    public AjaxResult login(@RequestBody LoginBody loginBody)
50
-    {
48
+    public AjaxResult login(@RequestBody LoginBody loginBody) {
51 49
         AjaxResult ajax = AjaxResult.success();
52 50
         // 生成令牌
53 51
         String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
54 52
                 loginBody.getUuid());
55 53
         ajax.put(Constants.TOKEN, token);
56
-
57
-//        IdentityAuthentication identityAuthentication = new IdentityAuthentication();
58
-//        identityAuthentication.setUserName(loginBody.getUsername());
59
-//        IdentityAuthentication identityAuthenticationselect = identityAuthenticationService.selectIdentityAuthentication(identityAuthentication);
60
-//        ajax.put("certificationStatusName", identityAuthenticationselect.getCertificationStatusName());
61
-//        ajax.put("certificationStatus", identityAuthenticationselect.getCertificationStatus());
62
-
54
+        //判断该用户是否已经实名认证(certificationStatus1已认证0未认证)
55
+        IdentityAuthentication identityAuthentication=new IdentityAuthentication();
56
+        identityAuthentication.setUserName(loginBody.getUsername());
57
+        String status = identityAuthenticationService.checkIsAuthentication(identityAuthentication);
58
+        ajax.put("certificationStatus", status);
63 59
         return ajax;
64 60
     }
65 61
 
66 62
     /**
67 63
      * 获取用户信息
68
-     * 
64
+     *
69 65
      * @return 用户信息
70 66
      */
71 67
     @GetMapping("getInfo")
72
-    public AjaxResult getInfo()
73
-    {
68
+    public AjaxResult getInfo() {
74 69
         SysUser user = SecurityUtils.getLoginUser().getUser();
75 70
         // 角色集合
76 71
         Set<String> roles = permissionService.getRolePermission(user);
@@ -85,12 +80,11 @@ public class SysLoginController
85 80
 
86 81
     /**
87 82
      * 获取路由信息
88
-     * 
83
+     *
89 84
      * @return 路由信息
90 85
      */
91 86
     @GetMapping("getRouters")
92
-    public AjaxResult getRouters()
93
-    {
87
+    public AjaxResult getRouters() {
94 88
         Long userId = SecurityUtils.getUserId();
95 89
         List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
96 90
         return AjaxResult.success(menuService.buildMenus(menus));

+ 5
- 19
ruoyi-admin/src/main/java/com/ruoyi/web/controller/wisdomarbitrate/IdentityAuthenticationController.java Datei anzeigen

@@ -3,7 +3,6 @@ package com.ruoyi.web.controller.wisdomarbitrate;
3 3
 import com.alibaba.fastjson.JSONObject;
4 4
 import com.ruoyi.common.core.controller.BaseController;
5 5
 import com.ruoyi.common.core.domain.AjaxResult;
6
-import com.ruoyi.wisdomarbitrate.domain.CaseApplication;
7 6
 import com.ruoyi.wisdomarbitrate.domain.IdentityAuthentication;
8 7
 import com.ruoyi.wisdomarbitrate.service.IdentityAuthenticationService;
9 8
 import org.springframework.beans.factory.annotation.Autowired;
@@ -15,7 +14,7 @@ import org.springframework.web.bind.annotation.RestController;
15 14
 
16 15
 @RestController
17 16
 @RequestMapping("/identityAuthentication")
18
-public class IdentityAuthenticationController   extends BaseController {
17
+public class IdentityAuthenticationController extends BaseController {
19 18
     @Autowired
20 19
     private IdentityAuthenticationService identityAuthenticationService;
21 20
 
@@ -23,8 +22,7 @@ public class IdentityAuthenticationController   extends BaseController {
23 22
      * 获取EIDtoken
24 23
      */
25 24
     @PostMapping("/selectIdentityAuthenticaEIDtoken")
26
-    public AjaxResult selectIdentityAuthenticaEIDtoken()
27
-    {
25
+    public AjaxResult selectIdentityAuthenticaEIDtoken() {
28 26
         JSONObject tokenResult = identityAuthenticationService.selectIdentityAuthenticaEIDtoken();
29 27
         return success(tokenResult);
30 28
     }
@@ -33,22 +31,10 @@ public class IdentityAuthenticationController   extends BaseController {
33 31
      * 小程序人脸核身后查询身份认证结果
34 32
      */
35 33
     @PostMapping("/selectIdentityAuthenticaRespon")
36
-    public AjaxResult selectIdentityAuthenticaRespon(@Validated @RequestBody IdentityAuthentication ientityAuthentication)
37
-    {
38
-//        String username = this.getUsername();
39
-//        Long userId = this.getUserId();
40
-//        ientityAuthentication.setUserId(userId);
41
-//        ientityAuthentication.setUserName(username);
42
-        JSONObject checkResult = identityAuthenticationService.selectIdentityAuthenticaRespon( ientityAuthentication);
43
-        return success(checkResult);
34
+    public AjaxResult selectIdentityAuthenticaRespon(@Validated @RequestBody IdentityAuthentication ientityAuthentication) {
35
+        AjaxResult checkResult = identityAuthenticationService.selectIdentityAuthenticaRespon(ientityAuthentication);
36
+        return checkResult;
44 37
     }
45 38
 
46 39
 
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54 40
 }

+ 1
- 1
ruoyi-admin/src/main/resources/application.yml Datei anzeigen

@@ -166,4 +166,4 @@ identityAuthentication:
166 166
   credentialSecretId: AKID3xfHgroY4MQHvLXUXMwIQL1UjmbBX1Tv
167 167
   credentialSecretKey: INDrIXcT8YmomZBcsy0oNirnU0LTN4X7
168 168
   merchantId: 0NSJ2309281116194321
169
-  privateKeyHexDecodeinfo: MHcCAQEEIEw7MRv3uYlpmU6Fko4GlXSh6Vd38k0cUQZ5zDwvRg+voAoGCCqBHM9VAYItoUQDQgAEUdxIAWhGg4LUXf1GoPdb8XMbGudpexPQCuaaRi9BCnNbpaF1kcwRhhsBKvop9ZmW/nOz4wQ1r/iIEOrc9qCXgQ==
169
+  privateKeyHexDecodeinfo: 4c3b311bf7b98969994e85928e069574a1e95777f24d1c510679cc3c2f460faf

+ 8
- 8
ruoyi-common/pom.xml Datei anzeigen

@@ -52,19 +52,19 @@
52 52
             <groupId>org.apache.commons</groupId>
53 53
             <artifactId>commons-lang3</artifactId>
54 54
         </dependency>
55
-  
55
+
56 56
         <!-- JSON工具类 -->
57 57
         <dependency>
58 58
             <groupId>com.fasterxml.jackson.core</groupId>
59 59
             <artifactId>jackson-databind</artifactId>
60 60
         </dependency>
61
-        
61
+
62 62
         <!-- 动态数据源 -->
63
-		<dependency>
64
-			<groupId>com.baomidou</groupId>
65
-			<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
66
-			<version>3.5.2</version>
67
-		</dependency>
63
+        <dependency>
64
+            <groupId>com.baomidou</groupId>
65
+            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
66
+            <version>3.5.2</version>
67
+        </dependency>
68 68
 
69 69
         <!-- 阿里JSON解析器 -->
70 70
         <dependency>
@@ -144,7 +144,7 @@
144 144
             <artifactId>tencentcloud-sdk-java-faceid</artifactId>
145 145
             <version>3.1.875</version>
146 146
         </dependency>
147
-
147
+        <!--用户信息解密-->
148 148
         <dependency>
149 149
             <groupId>cn.hutool</groupId>
150 150
             <artifactId>hutool-all</artifactId>

+ 10
- 1
ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/service/IdentityAuthenticationService.java Datei anzeigen

@@ -1,6 +1,7 @@
1 1
 package com.ruoyi.wisdomarbitrate.service;
2 2
 
3 3
 import com.alibaba.fastjson.JSONObject;
4
+import com.ruoyi.common.core.domain.AjaxResult;
4 5
 import com.ruoyi.wisdomarbitrate.domain.IdentityAuthentication;
5 6
 
6 7
 public interface IdentityAuthenticationService {
@@ -8,6 +9,14 @@ public interface IdentityAuthenticationService {
8 9
 
9 10
     IdentityAuthentication selectIdentityAuthentication(IdentityAuthentication identityAuthentication);
10 11
 
12
+    /**
13
+     * 检查是否已经认证的用户
14
+     *
15
+     * @param identityAuthentication
16
+     * @return
17
+     */
18
+    String checkIsAuthentication(IdentityAuthentication identityAuthentication);
19
+
11 20
     /**
12 21
      * 获取Eidtoken
13 22
      *
@@ -21,5 +30,5 @@ public interface IdentityAuthenticationService {
21 30
      * @param ientityAuthentication
22 31
      * @return
23 32
      */
24
-    JSONObject selectIdentityAuthenticaRespon(IdentityAuthentication ientityAuthentication);
33
+    AjaxResult selectIdentityAuthenticaRespon(IdentityAuthentication ientityAuthentication);
25 34
 }

+ 105
- 6
ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/service/impl/IdentityAuthenticationServiceImpl.java Datei anzeigen

@@ -1,10 +1,15 @@
1 1
 package com.ruoyi.wisdomarbitrate.service.impl;
2 2
 
3 3
 
4
+import cn.hutool.core.codec.Base64;
5
+import cn.hutool.crypto.SmUtil;
6
+import cn.hutool.crypto.asymmetric.SM2;
7
+import cn.hutool.crypto.symmetric.SymmetricCrypto;
4 8
 import com.alibaba.fastjson.JSON;
5 9
 import com.alibaba.fastjson.JSONObject;
6 10
 import com.ruoyi.common.core.domain.AjaxResult;
7
-import com.ruoyi.common.utils.StringUtils;
11
+import com.ruoyi.common.core.domain.model.LoginUser;
12
+import com.ruoyi.common.utils.SecurityUtils;
8 13
 import com.ruoyi.wisdomarbitrate.domain.IdentityAuthentication;
9 14
 import com.ruoyi.wisdomarbitrate.mapper.IdentityAuthenticationMapper;
10 15
 import com.ruoyi.wisdomarbitrate.service.IdentityAuthenticationService;
@@ -24,6 +29,8 @@ import org.springframework.beans.factory.annotation.Value;
24 29
 import org.springframework.stereotype.Service;
25 30
 import org.springframework.transaction.annotation.Transactional;
26 31
 
32
+import java.util.Date;
33
+
27 34
 @Service
28 35
 public class IdentityAuthenticationServiceImpl implements IdentityAuthenticationService {
29 36
 
@@ -58,6 +65,27 @@ public class IdentityAuthenticationServiceImpl implements IdentityAuthentication
58 65
 
59 66
     }
60 67
 
68
+    /**
69
+     * 检查是否已经认证的用户
70
+     *
71
+     * @param identityAuthentication
72
+     * @return
73
+     */
74
+    @Override
75
+    public String checkIsAuthentication(IdentityAuthentication identityAuthentication) {
76
+        IdentityAuthentication identityAuthenticationselect = identityAuthenticationMapper.selectIdentityAuthentication(identityAuthentication);
77
+        if (identityAuthenticationselect != null) {
78
+            return "1";
79
+        } else {
80
+            return "0";
81
+        }
82
+    }
83
+
84
+    /**
85
+     * 获取EIDtoken
86
+     *
87
+     * @return
88
+     */
61 89
     @Override
62 90
     public JSONObject selectIdentityAuthenticaEIDtoken() {
63 91
         JSONObject objJSON = new JSONObject();
@@ -74,10 +102,10 @@ public class IdentityAuthenticationServiceImpl implements IdentityAuthentication
74 102
             FaceidClient client = new FaceidClient(cred, "", clientProfile);
75 103
             // 实例化一个请求对象,每个接口都会对应一个request对象
76 104
             GetEidTokenRequest req = new GetEidTokenRequest();
105
+            req.setMerchantId(merchantId);
77 106
             // 返回的resp是一个GetEidTokenResponse的实例,与请求对象对应
78 107
             GetEidTokenResponse resp = client.GetEidToken(req);
79 108
             // 输出json格式的字符串回包
80
-            System.out.println(GetEidTokenResponse.toJsonString(resp));
81 109
             String respJSON = GetEidTokenResponse.toJsonString(resp);
82 110
             objJSON = JSON.parseObject(respJSON);
83 111
         } catch (TencentCloudSDKException e) {
@@ -87,9 +115,37 @@ public class IdentityAuthenticationServiceImpl implements IdentityAuthentication
87 115
         return objJSON;
88 116
     }
89 117
 
118
+    /**
119
+     * 解密用户信息
120
+     */
121
+    public JSONObject DecodeUserInfo(String deskey, String userInfo) {
122
+        JSONObject parse = null;
123
+        try {
124
+            byte[] desKeyBytes = Base64.decode(deskey);
125
+            final SM2 sm2 = new SM2(privateKeyHexDecodeinfo, null, null);
126
+            sm2.usePlainEncoding();
127
+            byte[] sm4KeyBytes = sm2.decrypt(desKeyBytes);
128
+            SymmetricCrypto sm4 = SmUtil.sm4(sm4KeyBytes);
129
+            byte[] plaintext = sm4.decrypt(Base64.decode(userInfo));
130
+            if (plaintext != null && plaintext.length > 0) {
131
+                String s = new String(plaintext);
132
+                parse = JSON.parseObject(s);
133
+            }
134
+        } catch (Exception e) {
135
+            System.out.println(e.toString());
136
+        }
137
+        return parse;
138
+    }
139
+
140
+    /**
141
+     * 小程序人脸核身后查询身份认证结果
142
+     *
143
+     * @param ientityAuthentication
144
+     * @return
145
+     */
90 146
     @Override
91 147
     @Transactional
92
-    public JSONObject selectIdentityAuthenticaRespon(IdentityAuthentication ientityAuthentication) {
148
+    public AjaxResult selectIdentityAuthenticaRespon(IdentityAuthentication ientityAuthentication) {
93 149
         String eidToken = ientityAuthentication.getEidToken();
94 150
 
95 151
         try {
@@ -108,10 +164,53 @@ public class IdentityAuthenticationServiceImpl implements IdentityAuthentication
108 164
             // 返回的resp是一个GetEidResultResponse的实例,与请求对象对应
109 165
             GetEidResultResponse resp = client.GetEidResult(req);
110 166
             // 输出json格式的字符串回包
111
-            System.out.println(GetEidResultResponse.toJsonString(resp));
112 167
             String s = GetEidResultResponse.toJsonString(resp);
113
-            JSONObject object = JSON.parseObject(s);
114
-            return object;
168
+            JSONObject objJSON = JSON.parseObject(s);
169
+            //查看是否核验成功
170
+            JSONObject text = objJSON.getJSONObject("Text");
171
+            if (text != null) {
172
+                Integer comparestatus = text.getInteger("Comparestatus");
173
+                if (comparestatus != null && comparestatus == 0) {
174
+                    JSONObject eidInfo = objJSON.getJSONObject("EidInfo");
175
+                    if (eidInfo != null) {
176
+                        String desKey = eidInfo.getString("DesKey");
177
+                        String userInfo = eidInfo.getString("UserInfo");
178
+                        //1.解密用户的信息
179
+                        JSONObject info = DecodeUserInfo(desKey, userInfo);
180
+                        if (info != null) {
181
+                            String idcardno = info.getString("idnum");
182
+                            String name = info.getString("name");
183
+                            //2.在用户认证表中插入用户认证记录
184
+                            LoginUser loginUser = SecurityUtils.getLoginUser();
185
+                            IdentityAuthentication authentication = new IdentityAuthentication();
186
+                            /**
187
+                             * 用户名
188
+                             * 用户名id
189
+                             * 姓名
190
+                             * 身份证号
191
+                             * 认证时间
192
+                             * 认证状态0表示成功
193
+                             * 请求id
194
+                             */
195
+                            authentication.setUserName(loginUser.getUsername());
196
+                            authentication.setUserId(loginUser.getUserId());
197
+                            authentication.setName(name);
198
+                            authentication.setIdentityNo(idcardno);
199
+                            authentication.setCertificationTime(new Date());
200
+                            authentication.setCertificationStatus(0);
201
+                            authentication.setRequestId(objJSON.getString("RequestId"));
202
+                            try {
203
+                                identityAuthenticationMapper.insertIdentityAuthentication(authentication);
204
+                            } catch (Exception e) {
205
+                                System.out.println("认证记录新增失败");
206
+                            }
207
+
208
+                        }
209
+
210
+                    }
211
+                }
212
+            }
213
+            return AjaxResult.success();
115 214
         } catch (TencentCloudSDKException e) {
116 215
             System.out.println(e.toString());
117 216
         }