forked from rongchao/epmet-cloud-rizhao
2 changed files with 242 additions and 0 deletions
@ -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<String> 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<String>().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<NameValuePair> params = new ArrayList<>(); |
||||
|
for (Map.Entry<String, Object> 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; |
||||
|
} |
||||
|
|
||||
|
} |
@ -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<String, String> 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<String, String> sortedParams) { |
||||
|
final StringBuffer content = new StringBuffer(); |
||||
|
final List<String> 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); |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue