代码优化

This commit is contained in:
egan
2021-08-08 23:04:12 +08:00
parent 3062726c9d
commit 44c72fa710
9 changed files with 71 additions and 48 deletions

View File

@@ -44,7 +44,7 @@ public class CertEnvironment {
/**
* 缓存的不同支付宝公钥证书序列号对应的支付宝公钥
*/
private Map<String, String> cachedAliPayPublicKey = new ConcurrentHashMap<String, String>();
private static final Map<String, String> CACHED_ALI_PAY_PUBLIC_KEY = new ConcurrentHashMap<String, String>();
/**
* 构造证书运行环境
@@ -64,7 +64,7 @@ public class CertEnvironment {
String aliPayPublicCertContent = AntCertificationUtil.readFromInputStream(aliPayCert);
aliPayPublicKeySN = AntCertificationUtil.getCertSN(aliPayPublicCertContent);
cachedAliPayPublicKey.put(aliPayPublicKeySN,
CACHED_ALI_PAY_PUBLIC_KEY.put(aliPayPublicKeySN,
AntCertificationUtil.getCertPublicKey(aliPayPublicCertContent));
}
@@ -79,11 +79,11 @@ public class CertEnvironment {
public String getAliPayPublicKey(String sn) {
//如果没有指定sn则默认取缓存中的第一个值
if (StringUtils.isEmpty(sn)) {
return cachedAliPayPublicKey.values().iterator().next();
return CACHED_ALI_PAY_PUBLIC_KEY.values().iterator().next();
}
if (cachedAliPayPublicKey.containsKey(sn)) {
return cachedAliPayPublicKey.get(sn);
if (CACHED_ALI_PAY_PUBLIC_KEY.containsKey(sn)) {
return CACHED_ALI_PAY_PUBLIC_KEY.get(sn);
} else {
//网关在支付宝公钥证书变更前,一定会确认通知到商户并在商户做出反馈后,才会更新该商户的支付宝公钥证书
//TODO: 后续可以考虑加入自动升级支付宝公钥证书逻辑,注意并发更新冲突问题

View File

@@ -16,8 +16,8 @@ import java.util.UUID;
*
* 支付宝测试
* @author egan
* @email egzosn@gmail.com
* @date 2017/8/18
* email egzosn@gmail.com
* date 2017/8/18
*/
public class PayTest {

View File

@@ -14,20 +14,25 @@
*/
package com.egzosn.pay.common.util.sign;
import com.egzosn.pay.common.util.str.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.*;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Enumeration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.egzosn.pay.common.util.str.StringUtils;
/**
* acpsdk证书工具类主要用于对证书的加载和使用
@@ -69,13 +74,16 @@ public class CertDescriptor {
if (LOG.isWarnEnabled()) {
LOG.warn("[CertId=" + encryptCertTemp.getSerialNumber().toString() + "]");
}
} catch (CertificateException e) {
}
catch (CertificateException e) {
LOG.error("InitCert Error", e);
} finally {
}
finally {
if (null != certIn) {
try {
certIn.close();
} catch (IOException e) {
}
catch (IOException e) {
LOG.error(e.toString());
}
}
@@ -96,7 +104,8 @@ public class CertDescriptor {
try {
in = new FileInputStream(path);
encryptCertTemp = initCert(in);
} catch (FileNotFoundException e) {
}
catch (FileNotFoundException e) {
LOG.error("InitCert Error File Not Found", e);
}
return encryptCertTemp;
@@ -115,16 +124,18 @@ public class CertDescriptor {
if (aliasenum.hasMoreElements()) {
keyAlias = aliasenum.nextElement();
}
PrivateKey privateKey = (PrivateKey) keyStore.getKey(keyAlias,
pwd.toCharArray());
PrivateKey privateKey = (PrivateKey) keyStore.getKey(keyAlias, pwd.toCharArray());
return privateKey;
} catch (KeyStoreException e) {
}
catch (KeyStoreException e) {
LOG.error("getSignCertPrivateKey Error", e);
return null;
} catch (UnrecoverableKeyException e) {
}
catch (UnrecoverableKeyException e) {
LOG.error("getSignCertPrivateKey Error", e);
return null;
} catch (NoSuchAlgorithmException e) {
}
catch (NoSuchAlgorithmException e) {
LOG.error("getSignCertPrivateKey Error", e);
return null;
}
@@ -145,7 +156,8 @@ public class CertDescriptor {
}
X509Certificate cert = (X509Certificate) keyStore.getCertificate(keyAlias);
return cert.getSerialNumber().toString();
} catch (Exception e) {
}
catch (Exception e) {
LOG.error("getSignCertId Error", e);
return null;
}
@@ -168,7 +180,8 @@ public class CertDescriptor {
if (LOG.isInfoEnabled()) {
LOG.info("InitSignCert Successful. CertId=[" + getSignCertId() + "]");
}
} catch (IOException e) {
}
catch (IOException e) {
LOG.error("InitSignCert Error", e);
}
}
@@ -231,14 +244,17 @@ public class CertDescriptor {
ks.load(fxKeyFile, nPassword);
}
return ks;
} catch (Exception e) {
}
catch (Exception e) {
LOG.error("getKeyInfo Error", e);
return null;
} finally {
}
finally {
if (null != fxKeyFile) {
try {
fxKeyFile.close();
} catch (IOException e) {
}
catch (IOException e) {
LOG.error("getKeyInfo Error", e);
}
}
@@ -263,7 +279,8 @@ public class CertDescriptor {
X509Certificate cert = (X509Certificate) keyStore
.getCertificate(keyAlias);
return cert.getSerialNumber().toString();
} catch (KeyStoreException e) {
}
catch (KeyStoreException e) {
LOG.error("getCertIdIdByStore Error", e);
return null;
}
@@ -281,7 +298,8 @@ public class CertDescriptor {
if (LOG.isInfoEnabled()) {
LOG.info("Load PublicKeyCert Successful");
}
} else if (LOG.isInfoEnabled()) {
}
else if (LOG.isInfoEnabled()) {
LOG.info("PublicKeyCert is empty");
}
}
@@ -297,7 +315,8 @@ public class CertDescriptor {
if (LOG.isInfoEnabled()) {
LOG.info("Load PublicKeyCert Successful");
}
} else if (LOG.isInfoEnabled()) {
}
else if (LOG.isInfoEnabled()) {
LOG.info("PublicKeyCert is empty");
}
}
@@ -311,14 +330,17 @@ public class CertDescriptor {
if (!StringUtils.isEmpty(certPath)) {
try {
initRootCert(new FileInputStream(certPath));
} catch (FileNotFoundException e) {
}
catch (FileNotFoundException e) {
LOG.info("RootCert is empty");
}
} else if (LOG.isInfoEnabled()) {
}
else if (LOG.isInfoEnabled()) {
LOG.info("RootCert is empty");
}
}
/**
* 加载根证书
*
@@ -330,7 +352,8 @@ public class CertDescriptor {
if (LOG.isInfoEnabled()) {
LOG.info("Load RootCert Successful");
}
} else if (LOG.isInfoEnabled()) {
}
else if (LOG.isInfoEnabled()) {
LOG.info("RootCert is empty");
}
}

View File

@@ -7,8 +7,8 @@
/**
* 支付类型
* @author egan
* @email egzosn@gmail.com
* @date 2016/11/20 0:30
* email egzosn@gmail.com
* date 2016/11/20 0:30
*/
public enum PayType implements BasePayType {
@@ -107,8 +107,8 @@ public enum PayType implements BasePayType {
/**
* 支付响应对象
* @author: egan
* @email egzosn@gmail.com
* @date 2016/11/18 0:34
* email egzosn@gmail.com
* date 2016/11/18 0:34
*/
public class PayResponse {
@Resource
@@ -244,8 +244,8 @@ public class PayResponse {
/**
* 支付宝回调信息拦截器
* @author: egan
* @email egzosn@gmail.com
* @date 2017/1/18 19:28
* email egzosn@gmail.com
* date 2017/1/18 19:28
*/
public class AliPayMessageInterceptor implements PayMessageInterceptor {
/**

View File

@@ -335,8 +335,8 @@ public class AliPayController {
* @return 返回支付方下载对账单的结果
*/
@RequestMapping("downloadbill")
public Object downloadbill(QueryOrder order) {
return service.downloadbill(order.getBillDate(), order.getBillType());
public Object downloadBill(QueryOrder order) {
return service.downloadBill(order.getBillDate(), order.getBillType());
}

View File

@@ -333,8 +333,8 @@ public class WxPayController {
* @return 返回支付方下载对账单的结果
*/
@RequestMapping("downloadbill")
public Object downloadbill(QueryOrder order) {
return service.downloadbill(order.getBillDate(), order.getBillType());
public Object downloadBill(QueryOrder order) {
return service.downloadBill(order.getBillDate(), order.getBillType());
}

View File

@@ -16,8 +16,8 @@ import java.util.UUID;
*
* 富友支付测试
* @author egan
* @email egzosn@gmail.com
* @date 2017/8/18
* email egzosn@gmail.com
* date 2017/8/18
*/
public class PayTest {

View File

@@ -27,8 +27,8 @@ import java.util.UUID;
*
* payoneer支付测试
* @author Actinia
* @email hayesfu@qq.com
* @date 2018/1/18 0018 16:49
* email hayesfu@qq.com
* date 2018/1/18 0018 16:49
*/
public class PayTest {

View File

@@ -16,8 +16,8 @@ import java.util.UUID;
*
* 友店微信
* @author egan
* @email egzosn@gmail.com
* @date 2017/8/18
* email egzosn@gmail.com
* date 2017/8/18
*/
public class PayTest {