package com.stylefeng.guns.core.util; import com.alibaba.fastjson.JSONObject; import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang.time.DateFormatUtils; import org.apache.http.HttpEntity; 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.util.EntityUtils; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.Serializable; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.UUID; public class GetOpenBodySig implements Serializable { static String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); static String nonce = UUID.randomUUID().toString().replace("-", ""); // static String appId = "10037e6f66f2d0f901672aa27d690006"; // 10037e6f6823b20801682b6a5e5a0006 // static String appKey = "47ace12ae3b348fe93ab46cee97c6fde"; static String appId = "8a81c1bf831e72e30187ad640adf49be"; // 10037e6f6823b20801682b6a5e5a0006 static String appKey = "6463dcd46c6d410eb40e68ee40e86e84"; /*public static void main(String[] args) throws Exception { JSONObject json = new JSONObject(); json.put("msgId", "001"); json.put("requestTimestamp", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")); // 报文请求时间 json.put("srcReserve", "请求系统预留字段"); json.put("mid", "898340149000035"); // 商户号 json.put("tid", "38557688"); // 终端号 json.put("instMid", "QRPAYDEFAULT"); // 业务类型 json.put("billNo", Util.getMerOrderId("1017")); // 商户订单号 json.put("billDate", DateFormatUtils.format(new Date(), "yyyy-MM-dd")); // 账单日期 // json.put("billNo", "1017202007101444016200450791"); // 商户订单号 //json.put("billDate", DateFormatUtils.format(new Date(), "yyyy-MM-dd")); json.put("billDesc", "账单描述"); // 账单描述 json.put("totalAmount", 1); // 支付总金额 String url = "https://test-api-open.chinaums.com/v1/netpay/bills/get-qrcode"; String send = send(url, json.toString()); System.out.println("返回结果:\n"+send); }*/ /** * token 方式 * @param url * @param entity * @return * @throws Exception */ public static String sendToken(String url, String entity,String authorization) throws Exception { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); httpPost.addHeader("Authorization", authorization); StringEntity se = new StringEntity(entity, "UTF-8"); se.setContentType("application/json"); httpPost.setEntity(se); CloseableHttpResponse response = httpClient.execute(httpPost); HttpEntity entity1 = response.getEntity(); String resStr = null; if (entity1 != null) { resStr = EntityUtils.toString(entity1, "UTF-8"); } httpClient.close(); response.close(); return resStr; } /** * OPEN-BODY-SIG 方式 * @param url * @param entity * @return * @throws Exception */ public static String sendOpenBodySig(String url, String entity) throws Exception { String authorization = getOpenBodySig(appId, appKey, entity); CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); httpPost.addHeader("Authorization", authorization); StringEntity se = new StringEntity(entity, "UTF-8"); se.setContentType("application/json"); httpPost.setEntity(se); CloseableHttpResponse response = httpClient.execute(httpPost); HttpEntity entity1 = response.getEntity(); String resStr = null; if (entity1 != null) { resStr = EntityUtils.toString(entity1, "UTF-8"); } httpClient.close(); response.close(); return resStr; } /** * open-body-sig方式获取到Authorization 的值 * * @param appId f0ec96ad2c3848b5b810e7aadf369e2f * @param appKey 775481e2556e4564985f5439a5e6a277 * @param body json字符串 String body = "{\"merchantCode\":\"123456789900081\",\"terminalCode\":\"00810001\",\"merchantOrderId\":\"20123333644493200\",\"transactionAmount\":\"1\",\"merchantRemark\":\"测试\",\"payMode\":\"CODE_SCAN\",\"payCode\":\"285668667587422761\",\"transactionCurrencyCode\":\"156\"}"; * @return * @throws Exception */ public static String getOpenBodySig(String appId, String appKey, String body) throws Exception { String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); // eg:20190227113148 String nonce = UUID.randomUUID().toString().replace("-", ""); // eg:be46cd581c9f46ecbd71b9858311ea12 byte[] data = body.getBytes("UTF-8"); System.out.println("data:\n" + body); InputStream is = new ByteArrayInputStream(data); String bodyDigest = testSHA256(is); // eg:d60bc3aedeb853e2a11c0c096baaf19954dd9b752e48dea8e919e5fb29a42a8d System.out.println("bodyDigest:\n" + bodyDigest); String str1_C = appId + timestamp + nonce + bodyDigest; // eg:f0ec96ad2c3848b5b810e7aadf369e2f + 20190227113148 + be46cd581c9f46ecbd71b9858311ea12 + d60bc3aedeb853e2a11c0c096baaf19954dd9b752e48dea8e919e5fb29a42a8d System.out.println("str1_C:" + str1_C); // System.out.println("appKey_D:\n" + appKey); byte[] localSignature = hmacSHA256(str1_C.getBytes(), appKey.getBytes()); String localSignatureStr = Base64.encodeBase64String(localSignature); // Signature System.out.println("Authorization:\n" + "OPEN-BODY-SIG AppId=" + "\"" + appId + "\"" + ", Timestamp=" + "\"" + timestamp + "\"" + ", Nonce=" + "\"" + nonce + "\"" + ", Signature=" + "\"" + localSignatureStr + "\"\n"); return ("OPEN-BODY-SIG AppId=" + "\"" + appId + "\"" + ", Timestamp=" + "\"" + timestamp + "\"" + ", Nonce=" + "\"" + nonce + "\"" + ", Signature=" + "\"" + localSignatureStr + "\""); } /** * 进行加密 * * @param is * @return 加密后的结果 */ private static String testSHA256(InputStream is) { try { // System.out.println(is.hashCode()); return DigestUtils.sha256Hex(is); } catch (IOException e) { e.printStackTrace(); } return null; } /** * @param data * @param key * @return * @throws NoSuchAlgorithmException * @throws InvalidKeyException */ public static byte[] hmacSHA256(byte[] data, byte[] key) throws NoSuchAlgorithmException, InvalidKeyException { String algorithm = "HmacSHA256"; Mac mac = Mac.getInstance(algorithm); mac.init(new SecretKeySpec(key, algorithm)); return mac.doFinal(data); } }