|
@@ -0,0 +1,146 @@
|
|
|
+package com.example.xiaoshiweixinback.service.weixinpay;
|
|
|
+
|
|
|
+import com.alibaba.druid.sql.ast.statement.SQLForeignKeyImpl;
|
|
|
+import com.example.xiaoshiweixinback.business.utils.RandomUtil;
|
|
|
+import com.example.xiaoshiweixinback.entity.weixinPay.GetAuthorizationVO;
|
|
|
+import okhttp3.HttpUrl;
|
|
|
+import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
|
|
|
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
|
|
+import org.bouncycastle.openssl.PEMParser;
|
|
|
+import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.io.FileReader;
|
|
|
+import java.security.PrivateKey;
|
|
|
+import java.security.Security;
|
|
|
+import java.security.Signature;
|
|
|
+import java.util.Base64;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class AuthorizationService {
|
|
|
+ /**
|
|
|
+ * 商户号
|
|
|
+ */
|
|
|
+
|
|
|
+ public static String merchantId = "1673179188";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 商户API私钥路径
|
|
|
+ */
|
|
|
+
|
|
|
+ public static String privateKeyPath = "C:\\Users\\admin\\Desktop\\小程序证书\\1673179188_20240408_cert\\apiclient_key.pem";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 商户证书序列号
|
|
|
+ */
|
|
|
+
|
|
|
+ public static String merchantSerialNumber = "794F7C195E3CEA0C926B5E8B425D8F5B03DFA049";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 商户APIV3密钥
|
|
|
+ */
|
|
|
+
|
|
|
+ public static String apiV3key = "wL3g4tAlOFe72gAd1THRqNPQsHIVxsYih6s20gN035oKRM3IBcd4c747zlQ1fLu8NrPCvGCo01Ox2jbDwkHJNaS9Yyn2R1NsTgoGWPObH6DWajwNBMrM3hjSC92XV3hJGfO7dkOsikNtHCigwFZk3DAXQmW6JLcl";
|
|
|
+
|
|
|
+// public String getToken(String method, S, String body) throws Exception {
|
|
|
+//
|
|
|
+// String nonceStr = "593BEC0C930BF1AFEB40B4A08C8FB242";
|
|
|
+//
|
|
|
+// long timestamp = System.currentTimeMillis() / 1000;
|
|
|
+//
|
|
|
+// String message = buildMessage(timestamp, nonceStr);
|
|
|
+//
|
|
|
+// String signature = sign(message.getBytes("utf-8"));
|
|
|
+//
|
|
|
+// return "mchid=\"" + merchantId + "\","
|
|
|
+//
|
|
|
+// + "nonce_str=\"" + nonceStr + "\","
|
|
|
+//
|
|
|
+// + "timestamp=\"" + timestamp + "\","
|
|
|
+//
|
|
|
+// + "serial_no=\"" + merchantSerialNumber + "\","
|
|
|
+//
|
|
|
+// + "signature=\"" + signature + "\"";
|
|
|
+//
|
|
|
+// }
|
|
|
+
|
|
|
+// public Map<String, Object> getSignature() throws Exception {
|
|
|
+// Map<String, Object> map = new HashMap<>();
|
|
|
+// String nonceStr = RandomUtil.generateRandomString(31);
|
|
|
+//
|
|
|
+// long timestamp = System.currentTimeMillis() / 1000;
|
|
|
+//
|
|
|
+// String message = buildMessage(timestamp, nonceStr);
|
|
|
+//
|
|
|
+// String signature = sign(message.getBytes("utf-8"));
|
|
|
+// map.put("signature", signature);
|
|
|
+// map.put("timestamp", timestamp);
|
|
|
+// map.put("nonceStr", nonceStr);
|
|
|
+// return map;
|
|
|
+//
|
|
|
+// }
|
|
|
+
|
|
|
+ public GetAuthorizationVO getAuthorization(String type, String url, String body) throws Exception {
|
|
|
+ GetAuthorizationVO getAuthorizationVO =new GetAuthorizationVO();
|
|
|
+ String re = "WECHATPAY2-SHA256-RSA2048 ";
|
|
|
+ re += "mchid=" + "\"" + merchantId + "\"";
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ String nonceStr = RandomUtil.generateRandomString(32);
|
|
|
+ long timestamp = System.currentTimeMillis() / 1000;
|
|
|
+ String message = buildMessage(type,url,timestamp, nonceStr, body);
|
|
|
+ String signature = sign(message.getBytes("utf-8"));
|
|
|
+ re += ",nonce_str=" + "\"" + nonceStr + "\"";
|
|
|
+ re += ",signature=" + "\"" + signature + "\"";
|
|
|
+ re += ",timestamp=" + "\"" + timestamp + "\"";
|
|
|
+ re += ",serial_no=" + "\"" + merchantSerialNumber + "\"";
|
|
|
+ getAuthorizationVO.setAuthorization(re);
|
|
|
+ getAuthorizationVO.setSignature(signature);
|
|
|
+ getAuthorizationVO.setTimestamp(timestamp);
|
|
|
+ getAuthorizationVO.setNonceStr(nonceStr);
|
|
|
+ return getAuthorizationVO;
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ String sign(byte[] message) throws Exception {
|
|
|
+
|
|
|
+ Signature sign = Signature.getInstance("SHA256withRSA");
|
|
|
+ // 添加Bouncy Castle作为安全提供者
|
|
|
+ Security.addProvider(new BouncyCastleProvider());
|
|
|
+
|
|
|
+
|
|
|
+ // 使用PEMParser读取PEM文件
|
|
|
+ try (FileReader fileReader = new FileReader(privateKeyPath);
|
|
|
+ PEMParser pemParser = new PEMParser(fileReader)) {
|
|
|
+
|
|
|
+ // 使用JcaPEMKeyConverter将PEM对象转换为PrivateKey
|
|
|
+ JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
|
|
|
+ PrivateKey privateKey = converter.getPrivateKey((PrivateKeyInfo) pemParser.readObject());
|
|
|
+ sign.initSign(privateKey);
|
|
|
+
|
|
|
+ sign.update(message);
|
|
|
+
|
|
|
+ return Base64.getEncoder().encodeToString(sign.sign());
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ String buildMessage(String type,String url,long timestamp, String nonceStr, String body) {
|
|
|
+
|
|
|
+ String re = type + "\n"
|
|
|
+
|
|
|
+ + url + "\n"
|
|
|
+
|
|
|
+ + timestamp + "\n"
|
|
|
+
|
|
|
+ + nonceStr + "\n";
|
|
|
+ if (body != null) {
|
|
|
+ re += body;
|
|
|
+ }
|
|
|
+ re += "\n";
|
|
|
+ return re;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|