refactor: 签名逻辑修改,全部使用换行符

This commit is contained in:
felord
2021-05-28 17:00:52 +08:00
parent 91dc8e8f8a
commit 16440d01cf
5 changed files with 29 additions and 43 deletions

View File

@@ -225,7 +225,7 @@ public abstract class AbstractApi {
* *
* @param link the link * @param link the link
* @return 对账单内容 ,有可能为空字符 “” * @return 对账单内容 ,有可能为空字符 “”
* @see AbstractApi#billResource(String) AbstractApi#billResource(String)AbstractApi#billResource(String) * @see AbstractApi#billResource(String) AbstractApi#billResource(String)AbstractApi#billResource(String)AbstractApi#billResource(String)
*/ */
protected String billCsvDownload(String link) { protected String billCsvDownload(String link) {
return this.client().withType(WechatPayV3Type.FILE_DOWNLOAD, link) return this.client().withType(WechatPayV3Type.FILE_DOWNLOAD, link)
@@ -292,7 +292,7 @@ public abstract class AbstractApi {
String ext = Objects.equals(TarType.GZIP, tradeBillParams.getTarType()) ? ".gzip" : ".txt"; String ext = Objects.equals(TarType.GZIP, tradeBillParams.getTarType()) ? ".gzip" : ".txt";
String filename = "tradebill-" + tradeBillParams.getBillDate().toString() + ext; String filename = "tradebill-" + tradeBillParams.getBillDate().toString() + ext;
return fileEntity(downloadUrl, filename); return downloadBillResponse(downloadUrl, filename);
} }
/** /**
@@ -338,10 +338,17 @@ public abstract class AbstractApi {
String ext = Objects.equals(TarType.GZIP, fundFlowBillParams.getTarType()) ? ".gzip" : ".txt"; String ext = Objects.equals(TarType.GZIP, fundFlowBillParams.getTarType()) ? ".gzip" : ".txt";
String filename = "fundflowbill-" + fundFlowBillParams.getBillDate().toString() + ext; String filename = "fundflowbill-" + fundFlowBillParams.getBillDate().toString() + ext;
return fileEntity(downloadUrl, filename); return this.downloadBillResponse(downloadUrl, filename);
} }
private ResponseEntity<Resource> fileEntity(String downloadUrl, String filename) { /**
* 调用{@code /v3/billdownload/file}直接下载为文件.
*
* @param downloadUrl 格式为 {@code https://api.mch.weixin.qq.com/v3/billdownload/file?token=xxx}
* @param filename 文件名称包含扩展名
* @return the response entity
*/
public ResponseEntity<Resource> downloadBillResponse(String downloadUrl, String filename) {
ResponseEntity<Resource> responseEntity = this.billResource(downloadUrl); ResponseEntity<Resource> responseEntity = this.billResource(downloadUrl);
HttpHeaders httpHeaders = new HttpHeaders(); HttpHeaders httpHeaders = new HttpHeaders();
// utf is not support // utf is not support
@@ -350,7 +357,7 @@ public abstract class AbstractApi {
} }
/** /**
* 对账单下载,流文件 * 调用{@code /v3/billdownload/file}下载返回的原始文件
* *
* @param link the link * @param link the link
* @return response entity * @return response entity
@@ -363,25 +370,6 @@ public abstract class AbstractApi {
.toUri(); .toUri();
return Get(uri); return Get(uri);
}) })
.resource(true); .resource();
}
/**
* todo 对账单下载,流文件。
*
* @param link the link
* @param newLine the new line
* @return response entity
*/
protected ResponseEntity<Resource> billResource(String link, boolean newLine) {
return this.client().withType(WechatPayV3Type.FILE_DOWNLOAD, link)
.function((type, downloadUrl) -> {
URI uri = UriComponentsBuilder.fromHttpUrl(downloadUrl)
.build()
.toUri();
return Get(uri);
})
.resource(newLine);
} }
} }

View File

@@ -118,7 +118,6 @@ public class SignatureProvider {
/** /**
* 我方请求前用 SHA256withRSA 加签使用API证书. * 我方请求前用 SHA256withRSA 加签使用API证书.
* *
* @param newLine 签名协议不兼容而增加的开关 github issues#18
* @param tenantId the properties key * @param tenantId the properties key
* @param method the method * @param method the method
* @param canonicalUrl the canonical url * @param canonicalUrl the canonical url
@@ -126,7 +125,7 @@ public class SignatureProvider {
* @return the string * @return the string
*/ */
@SneakyThrows @SneakyThrows
public String requestSign(boolean newLine, String tenantId, String method, String canonicalUrl, String body) { public String requestSign(String tenantId, String method, String canonicalUrl, String body) {
long timestamp = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")); long timestamp = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
String nonceStr = nonceStrGenerator.generateId() String nonceStr = nonceStrGenerator.generateId()
@@ -134,7 +133,7 @@ public class SignatureProvider {
.replaceAll("-", ""); .replaceAll("-", "");
WechatMetaBean wechatMetaBean = wechatMetaContainer.getWechatMeta(tenantId); WechatMetaBean wechatMetaBean = wechatMetaContainer.getWechatMeta(tenantId);
PrivateKey privateKey = wechatMetaBean.getKeyPair().getPrivate(); PrivateKey privateKey = wechatMetaBean.getKeyPair().getPrivate();
String encode = this.doRequestSign(newLine, privateKey, method, canonicalUrl, String.valueOf(timestamp), nonceStr, body); String encode = this.doRequestSign(privateKey, method, canonicalUrl, String.valueOf(timestamp), nonceStr, body);
// 序列号 // 序列号
String serialNo = wechatMetaBean.getSerialNumber(); String serialNo = wechatMetaBean.getSerialNumber();
// 生成token // 生成token
@@ -148,17 +147,16 @@ public class SignatureProvider {
/** /**
* Do request sign. * Do request sign.
* *
* @param newLine the has suffix
* @param privateKey the private key * @param privateKey the private key
* @param orderedComponents the orderedComponents * @param orderedComponents the orderedComponents
* @return the string * @return the string
* @since 1.0.4.RELEASE * @since 1.0.4.RELEASE
*/ */
@SneakyThrows @SneakyThrows
public String doRequestSign(boolean newLine, PrivateKey privateKey, String... orderedComponents) { public String doRequestSign(PrivateKey privateKey, String... orderedComponents) {
Signature signer = Signature.getInstance("SHA256withRSA", BC_PROVIDER); Signature signer = Signature.getInstance("SHA256withRSA", BC_PROVIDER);
signer.initSign(privateKey); signer.initSign(privateKey);
final String signatureStr = createSign(newLine, orderedComponents); final String signatureStr = createSign(true, orderedComponents);
signer.update(signatureStr.getBytes(StandardCharsets.UTF_8)); signer.update(signatureStr.getBytes(StandardCharsets.UTF_8));
return Base64Utils.encodeToString(signer.sign()); return Base64Utils.encodeToString(signer.sign());
} }
@@ -206,7 +204,7 @@ public class SignatureProvider {
} }
// 签名 // 签名
HttpMethod httpMethod = WechatPayV3Type.CERT.method(); HttpMethod httpMethod = WechatPayV3Type.CERT.method();
String authorization = requestSign(true, tenantId, httpMethod.name(), canonicalUrl, ""); String authorization = requestSign(tenantId, httpMethod.name(), canonicalUrl, "");
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
@@ -223,7 +221,7 @@ public class SignatureProvider {
ArrayNode certificates = bodyObjectNode.withArray("data"); ArrayNode certificates = bodyObjectNode.withArray("data");
if (certificates.isArray() && certificates.size() > 0) { if (certificates.isArray() && certificates.size() > 0) {
CERTIFICATE_MAP.remove(tenantId); CERTIFICATE_MAP.remove(tenantId);
final CertificateFactory certificateFactory = CertificateFactory.getInstance("X509",BC_PROVIDER); final CertificateFactory certificateFactory = CertificateFactory.getInstance("X509", BC_PROVIDER);
certificates.forEach(objectNode -> { certificates.forEach(objectNode -> {
JsonNode encryptCertificate = objectNode.get("encrypt_certificate"); JsonNode encryptCertificate = objectNode.get("encrypt_certificate");
String associatedData = encryptCertificate.get("associated_data").asText(); String associatedData = encryptCertificate.get("associated_data").asText();

View File

@@ -87,7 +87,7 @@ public class WechatDirectPayApi extends AbstractApi {
.toString() .toString()
.replaceAll("-", ""); .replaceAll("-", "");
String prepayId = body.get("prepay_id").asText(); String prepayId = body.get("prepay_id").asText();
String paySign = signatureProvider.doRequestSign(true, privateKey, appId, timestamp, nonceStr, prepayId); String paySign = signatureProvider.doRequestSign(privateKey, appId, timestamp, nonceStr, prepayId);
String mchId = wechatMetaBean.getV3().getMchId(); String mchId = wechatMetaBean.getV3().getMchId();
body.put("appid", appId); body.put("appid", appId);
@@ -136,7 +136,7 @@ public class WechatDirectPayApi extends AbstractApi {
.toString() .toString()
.replaceAll("-", ""); .replaceAll("-", "");
String packageStr = "prepay_id=" + body.get("prepay_id").asText(); String packageStr = "prepay_id=" + body.get("prepay_id").asText();
String paySign = signatureProvider.doRequestSign(true, privateKey, appId, timestamp, nonceStr, packageStr); String paySign = signatureProvider.doRequestSign(privateKey, appId, timestamp, nonceStr, packageStr);
body.put("appId", appId); body.put("appId", appId);
body.put("timeStamp", timestamp); body.put("timeStamp", timestamp);

View File

@@ -87,7 +87,7 @@ public class WechatPartnerPayApi extends AbstractApi {
.toString() .toString()
.replaceAll("-", ""); .replaceAll("-", "");
String prepayId = body.get("prepay_id").asText(); String prepayId = body.get("prepay_id").asText();
String paySign = signatureProvider.doRequestSign(true, privateKey, subAppid, timestamp, nonceStr, prepayId); String paySign = signatureProvider.doRequestSign(privateKey, subAppid, timestamp, nonceStr, prepayId);
body.put("appid", subAppid); body.put("appid", subAppid);
body.put("partnerid", partnerPayParams.getSubMchid()); body.put("partnerid", partnerPayParams.getSubMchid());
@@ -136,7 +136,7 @@ public class WechatPartnerPayApi extends AbstractApi {
.toString() .toString()
.replaceAll("-", ""); .replaceAll("-", "");
String packageStr = "prepay_id=" + body.get("prepay_id").asText(); String packageStr = "prepay_id=" + body.get("prepay_id").asText();
String paySign = signatureProvider.doRequestSign(true, privateKey, subAppid, timestamp, nonceStr, packageStr); String paySign = signatureProvider.doRequestSign(privateKey, subAppid, timestamp, nonceStr, packageStr);
body.put("appId", subAppid); body.put("appId", subAppid);
body.put("timeStamp", timestamp); body.put("timeStamp", timestamp);

View File

@@ -164,7 +164,7 @@ public class WechatPayClient {
public void request() { public void request() {
RequestEntity<?> requestEntity = this.requestEntityBiFunction.apply(this.wechatPayV3Type, this.model); RequestEntity<?> requestEntity = this.requestEntityBiFunction.apply(this.wechatPayV3Type, this.model);
WechatRequestEntity<?> wechatRequestEntity = WechatRequestEntity.of(requestEntity, this.responseBodyConsumer); WechatRequestEntity<?> wechatRequestEntity = WechatRequestEntity.of(requestEntity, this.responseBodyConsumer);
this.doExecute(this.header(true,wechatRequestEntity)); this.doExecute(this.header(wechatRequestEntity));
} }
@@ -176,7 +176,7 @@ public class WechatPayClient {
public String download() { public String download() {
RequestEntity<?> requestEntity = this.requestEntityBiFunction.apply(this.wechatPayV3Type, this.model); RequestEntity<?> requestEntity = this.requestEntityBiFunction.apply(this.wechatPayV3Type, this.model);
WechatRequestEntity<?> wechatRequestEntity = WechatRequestEntity.of(requestEntity, this.responseBodyConsumer); WechatRequestEntity<?> wechatRequestEntity = WechatRequestEntity.of(requestEntity, this.responseBodyConsumer);
return this.doDownload(this.header(true,wechatRequestEntity)); return this.doDownload(this.header(wechatRequestEntity));
} }
/** /**
@@ -185,10 +185,10 @@ public class WechatPayClient {
* @return the string * @return the string
* @since 1.0.6.RELEASE * @since 1.0.6.RELEASE
*/ */
protected ResponseEntity<Resource> resource(boolean newLine) { protected ResponseEntity<Resource> resource() {
RequestEntity<?> requestEntity = this.requestEntityBiFunction.apply(this.wechatPayV3Type, this.model); RequestEntity<?> requestEntity = this.requestEntityBiFunction.apply(this.wechatPayV3Type, this.model);
WechatRequestEntity<?> wechatRequestEntity = WechatRequestEntity.of(requestEntity, this.responseBodyConsumer); WechatRequestEntity<?> wechatRequestEntity = WechatRequestEntity.of(requestEntity, this.responseBodyConsumer);
return this.doResource(this.header(newLine,wechatRequestEntity)); return this.doResource(this.header(wechatRequestEntity));
} }
@@ -199,7 +199,7 @@ public class WechatPayClient {
* @param requestEntity the request entity * @param requestEntity the request entity
* @return the wechat request entity * @return the wechat request entity
*/ */
private <T> WechatRequestEntity<T> header(boolean newLine,WechatRequestEntity<T> requestEntity) { private <T> WechatRequestEntity<T> header(WechatRequestEntity<T> requestEntity) {
UriComponents uri = UriComponentsBuilder.fromUri(requestEntity.getUrl()).build(); UriComponents uri = UriComponentsBuilder.fromUri(requestEntity.getUrl()).build();
String canonicalUrl = uri.getPath(); String canonicalUrl = uri.getPath();
@@ -220,7 +220,7 @@ public class WechatPayClient {
} }
String tenantId = Objects.requireNonNull(headers.get("Pay-TenantId")).get(0); String tenantId = Objects.requireNonNull(headers.get("Pay-TenantId")).get(0);
String authorization = signatureProvider.requestSign(newLine,tenantId, httpMethod.name(), canonicalUrl, body); String authorization = signatureProvider.requestSign(tenantId, httpMethod.name(), canonicalUrl, body);
HttpHeaders httpHeaders = new HttpHeaders(); HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.addAll(headers); httpHeaders.addAll(headers);