fix: 支付宝maven打包读取不了证书

Closes #24
This commit is contained in:
felord
2021-03-25 11:00:46 +08:00
parent 9dfdb2b295
commit ae618da2d3

View File

@@ -19,11 +19,12 @@
package cn.felord.payment.alipay; package cn.felord.payment.alipay;
import cn.felord.payment.PayException;
import com.alipay.api.AlipayApiException; import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient; import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest; import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient; import com.alipay.api.DefaultAlipayClient;
import cn.felord.payment.PayException; import com.alipay.api.internal.util.file.IOUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -32,7 +33,9 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import java.io.*; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/** /**
* @author felord.cn * @author felord.cn
@@ -57,28 +60,28 @@ public class AliPayConfiguration {
propertyMapper.from(v1::getFormat).to(certAlipayRequest::setFormat); propertyMapper.from(v1::getFormat).to(certAlipayRequest::setFormat);
propertyMapper.from(v1::getCharset).to(certAlipayRequest::setCharset); propertyMapper.from(v1::getCharset).to(certAlipayRequest::setCharset);
propertyMapper.from(v1::getSignType).to(certAlipayRequest::setSignType); propertyMapper.from(v1::getSignType).to(certAlipayRequest::setSignType);
propertyMapper.from(v1::getAppCertPublicKeyPath).as(this::getFileAbsolutePath).to(certAlipayRequest::setCertPath); propertyMapper.from(v1::getAppCertPublicKeyPath).as(this::getContentFromClassPath).to(certAlipayRequest::setCertContent);
propertyMapper.from(v1::getAlipayPublicCertPath).as(this::getFileAbsolutePath).to(certAlipayRequest::setAlipayPublicCertPath); propertyMapper.from(v1::getAlipayPublicCertPath).as(this::getContentFromClassPath).to(certAlipayRequest::setAlipayPublicCertContent);
propertyMapper.from(v1::getAlipayRootCertPath).as(this::getFileAbsolutePath).to(certAlipayRequest::setRootCertPath); propertyMapper.from(v1::getAlipayRootCertPath).as(this::getContentFromClassPath).to(certAlipayRequest::setRootCertContent);
return new DefaultAlipayClient(certAlipayRequest); return new DefaultAlipayClient(certAlipayRequest);
} }
private String getFileAbsolutePath(String classPath) {
try {
return new ClassPathResource(classPath).getFile().getAbsolutePath();
} catch (IOException e) {
log.error("ali pay cert path is not exist ,{}", e.getMessage());
throw new PayException("ali pay cert path is not exist");
}
private String getContentFromClassPath(String classPath) {
ClassPathResource resource = new ClassPathResource(classPath);
try (InputStreamReader in = new InputStreamReader(resource.getInputStream())) {
return IOUtils.toString(in);
} catch (IOException e) {
log.error("ali pay root cert is invalid ,{}", e.getMessage());
throw new PayException("ali pay root cert path is invalid");
}
} }
private String appRSAPrivateKey(String classPath) { private String appRSAPrivateKey(String classPath) {
ClassPathResource resource = new ClassPathResource(classPath); ClassPathResource resource = new ClassPathResource(classPath);
try { try (InputStreamReader inputStreamReader = new InputStreamReader(resource.getInputStream())) {
InputStreamReader inputStreamReader = new InputStreamReader(resource.getInputStream()); try (BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
try(BufferedReader bufferedReader = new BufferedReader(inputStreamReader)){
return bufferedReader.readLine(); return bufferedReader.readLine();
} }
} catch (IOException e) { } catch (IOException e) {
@@ -87,5 +90,4 @@ public class AliPayConfiguration {
} }
} }
} }