From 3b9a5ba4b90c86767b05255c98b3a4c077f65e87 Mon Sep 17 00:00:00 2001 From: luyan Date: Fri, 16 Jun 2023 15:31:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=B1=85=E6=B0=91=E7=A0=81?= =?UTF-8?q?=E6=89=AB=E6=8F=8F=E5=8A=9F=E8=83=BD=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../epmet/controller/JmmRiZhaoController.java | 98 ++++++++++++ .../java/com/epmet/util/JmmRiZhaoUtil.java | 144 ++++++++++++++++++ 2 files changed, 242 insertions(+) create mode 100644 epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/JmmRiZhaoController.java create mode 100644 epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/util/JmmRiZhaoUtil.java diff --git a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/JmmRiZhaoController.java b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/JmmRiZhaoController.java new file mode 100644 index 0000000000..28453c78a4 --- /dev/null +++ b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/JmmRiZhaoController.java @@ -0,0 +1,98 @@ +package com.epmet.controller; + +import cn.hutool.json.JSONObject; +import com.epmet.commons.tools.utils.Result; +import com.epmet.util.JmmRiZhaoUtil; +import lombok.extern.slf4j.Slf4j; +import org.apache.http.HttpEntity; +import org.apache.http.NameValuePair; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +/** + * @author yan Lu + * @description 日照居民们扫描接口 + * @create 2023/6/16 14:15 + */ +@Slf4j +@RestController +@RequestMapping("jmmRiZhao") +public class JmmRiZhaoController { + + @GetMapping("mutualDiscern/{QrCode}") + public Result mutualDiscern(@PathVariable("QrCode") String QrCode) { + try { + JSONObject infoJson = new JSONObject(); + infoJson.put("QrCode", QrCode); + infoJson.put("useCityCode", JmmRiZhaoUtil.USE_CITY_CODE); + infoJson.put("institutionCode", JmmRiZhaoUtil.INSTITUTION_CODE);//机构编码 + infoJson.put("businessStepCode", JmmRiZhaoUtil.BUSINESS_STEP_CODE);//业务环节编码 + infoJson.put("channelCode", JmmRiZhaoUtil.BUSINESS_STEP_CODE);//终端类型(0人工、1自) + infoJson.put("useTime", JmmRiZhaoUtil.APP_TIME); + String userInfo = post(JmmRiZhaoUtil.POST_URL, infoJson); + return new Result().ok(userInfo); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + private static String post(String url, cn.hutool.json.JSONObject jsonObject) { + try { + String uuid = UUID.randomUUID().toString(); + String timestamp = JmmRiZhaoUtil.APP_TIME; + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpPost httpPost = new HttpPost(JmmRiZhaoUtil.AEP_GATE_URL.concat(url)); + httpPost.addHeader(JmmRiZhaoUtil.AEP_NONCE, uuid); + httpPost.addHeader(JmmRiZhaoUtil.AEP_APPKEY, JmmRiZhaoUtil.appKeyValue); + httpPost.addHeader(JmmRiZhaoUtil.AEP_TIMESTAMP, timestamp); + String sign = JmmRiZhaoUtil.hmacSm3Str(JmmRiZhaoUtil.appSecretValue, JmmRiZhaoUtil.getBody(uuid, timestamp)); + httpPost.addHeader(JmmRiZhaoUtil.AEP_SIGNATURE, sign); + httpPost.addHeader(JmmRiZhaoUtil.APP_ID, JmmRiZhaoUtil.appid); + httpPost.addHeader(JmmRiZhaoUtil.APP_ISSECRET, JmmRiZhaoUtil.isSecret); + + // 解决中文乱码问题 + StringEntity stringEntity = null; + if ("0".equals(JmmRiZhaoUtil.isSecret)) { + stringEntity = new StringEntity(jsonObject.toString(), "UTF-8"); + } else if ("1".equals(JmmRiZhaoUtil.isSecret)) { + JSONObject json = new JSONObject(); + String encData = JmmRiZhaoUtil.encrypt(jsonObject.toString()); + json.put("encData", encData); + stringEntity = new StringEntity(json.toString(), "UTF-8"); + } + stringEntity.setContentEncoding("UTF-8"); + List params = new ArrayList<>(); + for (Map.Entry entry : jsonObject.entrySet()) { + NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue().toString()); + params.add(pair); + } + stringEntity.setContentType(MediaType.APPLICATION_JSON_VALUE); + httpPost.setEntity(stringEntity); + CloseableHttpResponse response = httpclient.execute(httpPost); + HttpEntity entity = response.getEntity(); + if (entity != null) { + return EntityUtils.toString(entity, "UTF-8"); + } + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/util/JmmRiZhaoUtil.java b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/util/JmmRiZhaoUtil.java new file mode 100644 index 0000000000..4da916ba4f --- /dev/null +++ b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/util/JmmRiZhaoUtil.java @@ -0,0 +1,144 @@ +package com.epmet.util; + +import cn.hutool.core.util.HexUtil; +import cn.hutool.crypto.SmUtil; +import org.apache.commons.lang.StringUtils; +import org.bouncycastle.jce.provider.BouncyCastleProvider; + +import javax.crypto.Cipher; +import javax.crypto.spec.SecretKeySpec; +import java.nio.charset.StandardCharsets; +import java.util.*; + +/** + * @author yan Lu + * @description 日照居民码工具 + * @create 2023/6/16 14:16 + */ +public class JmmRiZhaoUtil { + + /** + * 请求ID参数(唯一参数,使用UUID生成即可) + **/ + public static final String AEP_NONCE = "x-aep-nonce"; + /** + * appkey + */ + public static final String AEP_APPKEY = "x-aep-appkey"; + /** + * 时间戳参数 + */ + public static final String AEP_TIMESTAMP = "x-aep-timestamp"; + /** + * 签名参数 + **/ + public static final String AEP_SIGNATURE = "x-aep-signature"; + /** + * 网关地址 + */ + public final static String AEP_GATE_URL = "https://jmm.rizhao.cn"; + /** + * 应用 KEY + */ + public final static String appKeyValue = "1669157563761807360"; + /** + * 应用 Secret + */ + public final static String appSecretValue = "20be8ebaa78644a8979b221163937dc2"; + //应用参数 + /** + * 应用标识名称 + */ + public final static String APP_ID = "appid"; + /** + * 应用标识id + */ + public final static String appid = "n5h94k50holzvs280h59"; + /** + * 请求头加密传输标识 + */ + public final static String APP_ISSECRET = "isSecret"; + /** + * 是否开启加密传输:0否 1开启 + */ + public final static String isSecret = "0"; + + public final static String CHANNEL_CODE = "1"; + /** + * 请求时间 + */ + public final static String APP_TIME = String.valueOf(System.currentTimeMillis()); + /** + * sm4密钥 + */ + public final static String sm4Key = "opb7szp71kp92u35"; + + public final static String POST_URL = "/rzsubplatformprod/mutual/mutualDiscern"; + + public final static String INSTITUTION_CODE = "11371100MB2854624Q"; + + public final static String USE_CITY_CODE = "371100"; + + public final static String BUSINESS_STEP_CODE = "402"; + + public static final String ALGORITHM_NAME_ECB_PADDING = "SM4/ECB/PKCS7Padding"; + + public static final String ALGORITHM_NAME = "SM4"; + + public static final BouncyCastleProvider PROVIDER = new BouncyCastleProvider(); + + public static String getBody(String uuid, String time) { + Map objectMap = new HashMap<>(); + objectMap.put(JmmRiZhaoUtil.AEP_NONCE, uuid); + objectMap.put(JmmRiZhaoUtil.AEP_APPKEY, JmmRiZhaoUtil.appKeyValue); + objectMap.put(JmmRiZhaoUtil.AEP_TIMESTAMP, time); + return getSignContent(objectMap); + } + + public static String getSignContent(Map sortedParams) { + final StringBuffer content = new StringBuffer(); + final List keys = new ArrayList<>(sortedParams.keySet()); + Collections.sort(keys); + int index = 0; + int size = keys.size(); + for (int i = 0; i < size; ++i) { + final String key = keys.get(i); + final String value = sortedParams.get(key); + if (StringUtils.isNotEmpty(key) && StringUtils.isNotEmpty(value)) { + content.append(((index == 0) ? "" : "&") + key + "=" + value); + ++index; + } + } + return content.toString(); + } + + public static String hmacSm3Str(final String key, final String data) { + try { + return SmUtil.hmacSm3(key.getBytes(StandardCharsets.UTF_8)).digestHex(data); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + /** + * 加密 + */ + public static String encrypt(String content) throws Exception { + Cipher cipher = Cipher.getInstance(JmmRiZhaoUtil.ALGORITHM_NAME_ECB_PADDING, JmmRiZhaoUtil.PROVIDER); + cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(JmmRiZhaoUtil.sm4Key.getBytes(), JmmRiZhaoUtil.ALGORITHM_NAME)); + byte[] b = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8)); + return HexUtil.encodeHexStr(b); + } + + /** + * 解密 + */ + public static String decrypt(String encryptStr) throws Exception { + byte[] decodeBase64 = HexUtil.decodeHex(encryptStr); + Cipher cipher = Cipher.getInstance(JmmRiZhaoUtil.ALGORITHM_NAME_ECB_PADDING, JmmRiZhaoUtil.PROVIDER); + cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(JmmRiZhaoUtil.sm4Key.getBytes(), JmmRiZhaoUtil.ALGORITHM_NAME)); + byte[] decryptBytes = cipher.doFinal(decodeBase64); + return new String(decryptBytes); + } +}