//获取支付宝POST过来反馈信息
Map<String, String> params = new HashMap<>(16);
Map<String, String[]> requestParams = request.getParameterMap();
for (String name : requestParams.keySet()) {
String[] values = requestParams.get(name);
String valueStr = "";
for (int i = 0; i < values.length; i++) {
valueStr = (i == values.length - 1) ? valueStr + values[i]
: valueStr + values[i] + ",";
}
params.put(name, valueStr);
}
//直接用txt打开公钥证书的内容
String alipayCertContent = "公钥证书内容";
String publicKey = getPublicKey(alipayCertContent);
//证书模式验签
boolean signVerified = AlipaySignature.rsaCheckV1(params,
publicKey,
cn.hutool.core.util.CharsetUtil.UTF_8,
AlipayConstants.SIGN_TYPE_RSA2);
public static String getPublicKey(String s) throws AlipayApiException {
InputStream inputStream = null;
try {
inputStream = new ByteArrayInputStream(s.getBytes());
CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC");
X509Certificate cert = (X509Certificate) cf.generateCertificate(inputStream);
PublicKey publicKey = cert.getPublicKey();
return com.alipay.api.internal.util.codec.Base64.encodeBase64String(publicKey.getEncoded());
} catch (NoSuchProviderException e) {
throw new AlipayApiException(e);
} catch (CertificateException e) {
throw new AlipayApiException(e);
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
throw new AlipayApiException(e);
}
}
}
由于支付宝提供的官方sdk没有根据证书内容来验签的,需要自己翻sdk的底层代码拷贝出来,用getPublicKey方法解析支付宝公钥,然后在用公钥进行验签