🎨 优化代码
This commit is contained in:
@@ -9,6 +9,7 @@ import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.error.WxRuntimeException;
|
||||
import me.chanjar.weixin.common.util.BeanUtils;
|
||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
|
||||
@@ -246,7 +247,7 @@ public abstract class BaseWxPayRequest implements Serializable {
|
||||
|
||||
return document.asXML();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("generate xml error", e);
|
||||
throw new WxRuntimeException("generate xml error", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import com.google.common.collect.Maps;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import lombok.Data;
|
||||
import me.chanjar.weixin.common.error.WxRuntimeException;
|
||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -139,7 +140,7 @@ public abstract class BaseWxPayResult {
|
||||
t.loadXml(doc);
|
||||
return (T) t;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("parse xml error", e);
|
||||
throw new WxRuntimeException("parse xml error", e);
|
||||
}
|
||||
}
|
||||
XStream xstream = XStreamInitializer.getInstance();
|
||||
@@ -243,7 +244,7 @@ public abstract class BaseWxPayResult {
|
||||
*/
|
||||
public Map<String, String> toMap() {
|
||||
if (StringUtils.isBlank(this.xmlString)) {
|
||||
throw new RuntimeException("xml数据有问题,请核实!");
|
||||
throw new WxRuntimeException("xml数据有问题,请核实!");
|
||||
}
|
||||
|
||||
Map<String, String> result = Maps.newHashMap();
|
||||
@@ -258,7 +259,7 @@ public abstract class BaseWxPayResult {
|
||||
result.put(list.item(i).getNodeName(), list.item(i).getTextContent());
|
||||
}
|
||||
} catch (XPathExpressionException e) {
|
||||
throw new RuntimeException("非法的xml文本内容:" + xmlString);
|
||||
throw new WxRuntimeException("非法的xml文本内容:" + xmlString);
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -282,7 +283,7 @@ public abstract class BaseWxPayResult {
|
||||
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
||||
return factory.newDocumentBuilder().parse(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("非法的xml文本内容:\n" + this.xmlString, e);
|
||||
throw new WxRuntimeException("非法的xml文本内容:\n" + this.xmlString, e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -302,7 +303,7 @@ public abstract class BaseWxPayResult {
|
||||
.compile(expression)
|
||||
.evaluate(doc, XPathConstants.STRING);
|
||||
} catch (XPathExpressionException e) {
|
||||
throw new RuntimeException("未找到相应路径的文本:" + expression);
|
||||
throw new WxRuntimeException("未找到相应路径的文本:" + expression);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ import com.github.binarywang.wxpay.util.XmlConfig;
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.collect.Maps;
|
||||
import jodd.io.ZipUtil;
|
||||
import me.chanjar.weixin.common.error.WxRuntimeException;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -409,7 +410,7 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
|
||||
WxPayUnifiedOrderResult unifiedOrderResult = this.unifiedOrder(request);
|
||||
String prepayId = unifiedOrderResult.getPrepayId();
|
||||
if (StringUtils.isBlank(prepayId)) {
|
||||
throw new RuntimeException(String.format("无法获取prepay id,错误代码: '%s',信息:%s。",
|
||||
throw new WxRuntimeException(String.format("无法获取prepay id,错误代码: '%s',信息:%s。",
|
||||
unifiedOrderResult.getErrCode(), unifiedOrderResult.getErrCodeDes()));
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import com.github.binarywang.wxpay.v3.util.PemUtils;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.error.WxRuntimeException;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
@@ -94,7 +95,7 @@ public class AutoUpdateCertificatesVerifier implements Verifier {
|
||||
autoUpdateCert();
|
||||
instant = Instant.now();
|
||||
} catch (IOException | GeneralSecurityException e) {
|
||||
throw new RuntimeException(e);
|
||||
throw new WxRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.github.binarywang.wxpay.v3.auth;
|
||||
|
||||
import me.chanjar.weixin.common.error.WxRuntimeException;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
@@ -30,11 +32,11 @@ public class CertificatesVerifier implements Verifier {
|
||||
sign.update(message);
|
||||
return sign.verify(Base64.getDecoder().decode(signature));
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException("当前Java环境不支持SHA256withRSA", e);
|
||||
throw new WxRuntimeException("当前Java环境不支持SHA256withRSA", e);
|
||||
} catch (SignatureException e) {
|
||||
throw new RuntimeException("签名验证过程发生了错误", e);
|
||||
throw new WxRuntimeException("签名验证过程发生了错误", e);
|
||||
} catch (InvalidKeyException e) {
|
||||
throw new RuntimeException("无效的证书", e);
|
||||
throw new WxRuntimeException("无效的证书", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.github.binarywang.wxpay.v3.auth;
|
||||
|
||||
import me.chanjar.weixin.common.error.WxRuntimeException;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.PrivateKey;
|
||||
@@ -27,11 +29,11 @@ public class PrivateKeySigner implements Signer {
|
||||
return new SignatureResult(
|
||||
Base64.getEncoder().encodeToString(sign.sign()), certificateSerialNumber);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException("当前Java环境不支持SHA256withRSA", e);
|
||||
throw new WxRuntimeException("当前Java环境不支持SHA256withRSA", e);
|
||||
} catch (SignatureException e) {
|
||||
throw new RuntimeException("签名计算失败", e);
|
||||
throw new WxRuntimeException("签名计算失败", e);
|
||||
} catch (InvalidKeyException e) {
|
||||
throw new RuntimeException("无效的私钥", e);
|
||||
throw new WxRuntimeException("无效的私钥", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.github.binarywang.wxpay.v3.util;
|
||||
|
||||
import me.chanjar.weixin.common.error.WxRuntimeException;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -35,11 +37,11 @@ public class PemUtils {
|
||||
return kf.generatePrivate(
|
||||
new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey)));
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException("当前Java环境不支持RSA", e);
|
||||
throw new WxRuntimeException("当前Java环境不支持RSA", e);
|
||||
} catch (InvalidKeySpecException e) {
|
||||
throw new RuntimeException("无效的密钥格式");
|
||||
throw new WxRuntimeException("无效的密钥格式");
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("无效的密钥");
|
||||
throw new WxRuntimeException("无效的密钥");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,11 +52,11 @@ public class PemUtils {
|
||||
cert.checkValidity();
|
||||
return cert;
|
||||
} catch (CertificateExpiredException e) {
|
||||
throw new RuntimeException("证书已过期", e);
|
||||
throw new WxRuntimeException("证书已过期", e);
|
||||
} catch (CertificateNotYetValidException e) {
|
||||
throw new RuntimeException("证书尚未生效", e);
|
||||
throw new WxRuntimeException("证书尚未生效", e);
|
||||
} catch (CertificateException e) {
|
||||
throw new RuntimeException("无效的证书", e);
|
||||
throw new WxRuntimeException("无效的证书", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.github.binarywang.wxpay.v3.util;
|
||||
|
||||
import com.github.binarywang.wxpay.exception.WxPayException;
|
||||
import com.github.binarywang.wxpay.v3.SpecEncrypt;
|
||||
import me.chanjar.weixin.common.error.WxRuntimeException;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.Cipher;
|
||||
@@ -70,7 +71,7 @@ public class RsaCryptoUtil {
|
||||
byte[] ciphertext = cipher.doFinal(data);
|
||||
return Base64.getEncoder().encodeToString(ciphertext);
|
||||
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
|
||||
throw new RuntimeException("当前Java环境不支持RSA v1.5/OAEP", e);
|
||||
throw new WxRuntimeException("当前Java环境不支持RSA v1.5/OAEP", e);
|
||||
} catch (InvalidKeyException e) {
|
||||
throw new IllegalArgumentException("无效的证书", e);
|
||||
} catch (IllegalBlockSizeException | BadPaddingException e) {
|
||||
@@ -87,7 +88,7 @@ public class RsaCryptoUtil {
|
||||
byte[] data = Base64.getDecoder().decode(ciphertext);
|
||||
return new String(cipher.doFinal(data), StandardCharsets.UTF_8);
|
||||
} catch (NoSuchPaddingException | NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException("当前Java环境不支持RSA v1.5/OAEP", e);
|
||||
throw new WxRuntimeException("当前Java环境不支持RSA v1.5/OAEP", e);
|
||||
} catch (InvalidKeyException e) {
|
||||
throw new IllegalArgumentException("无效的私钥", e);
|
||||
} catch (BadPaddingException | IllegalBlockSizeException e) {
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
package com.github.binarywang.wxpay.v3.util;
|
||||
|
||||
import me.chanjar.weixin.common.error.WxRuntimeException;
|
||||
|
||||
import java.security.*;
|
||||
import java.util.Base64;
|
||||
import java.util.Random;
|
||||
|
||||
public class SignUtils {
|
||||
|
||||
public static String sign(String string, PrivateKey privateKey){
|
||||
public static String sign(String string, PrivateKey privateKey) {
|
||||
try {
|
||||
Signature sign = Signature.getInstance("SHA256withRSA");
|
||||
sign.initSign(privateKey);
|
||||
@@ -14,23 +16,24 @@ public class SignUtils {
|
||||
|
||||
return Base64.getEncoder().encodeToString(sign.sign());
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException("当前Java环境不支持SHA256withRSA", e);
|
||||
throw new WxRuntimeException("当前Java环境不支持SHA256withRSA", e);
|
||||
} catch (SignatureException e) {
|
||||
throw new RuntimeException("签名计算失败", e);
|
||||
throw new WxRuntimeException("签名计算失败", e);
|
||||
} catch (InvalidKeyException e) {
|
||||
throw new RuntimeException("无效的私钥", e);
|
||||
throw new WxRuntimeException("无效的私钥", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机生成32位字符串.
|
||||
*/
|
||||
public static String genRandomStr(){
|
||||
public static String genRandomStr() {
|
||||
return genRandomStr(32);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机字符串
|
||||
*
|
||||
* @param length 字符串长度
|
||||
* @return
|
||||
*/
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
|
||||
import com.google.inject.Binder;
|
||||
import com.google.inject.Module;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import me.chanjar.weixin.common.error.WxRuntimeException;
|
||||
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -24,7 +25,7 @@ public class ApiTestModule implements Module {
|
||||
public void configure(Binder binder) {
|
||||
try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(TEST_CONFIG_XML)) {
|
||||
if (inputStream == null) {
|
||||
throw new RuntimeException("测试配置文件【" + TEST_CONFIG_XML + "】未找到,请参照test-config-sample.xml文件生成");
|
||||
throw new WxRuntimeException("测试配置文件【" + TEST_CONFIG_XML + "】未找到,请参照test-config-sample.xml文件生成");
|
||||
}
|
||||
|
||||
XmlWxPayConfig config = this.fromXml(XmlWxPayConfig.class, inputStream);
|
||||
|
||||
Reference in New Issue
Block a user