|
@@ -6,9 +6,28 @@ import com.qcloud.cos.auth.BasicCOSCredentials;
|
|
import com.qcloud.cos.auth.COSCredentials;
|
|
import com.qcloud.cos.auth.COSCredentials;
|
|
import com.qcloud.cos.http.HttpProtocol;
|
|
import com.qcloud.cos.http.HttpProtocol;
|
|
import com.qcloud.cos.region.Region;
|
|
import com.qcloud.cos.region.Region;
|
|
|
|
+import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
|
|
|
|
+import com.wechat.pay.contrib.apache.httpclient.auth.AutoUpdateCertificatesVerifier;
|
|
|
|
+import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
|
|
|
|
+import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
|
|
|
|
+import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
|
|
|
|
+import org.apache.http.client.HttpClient;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.context.annotation.Bean;
|
|
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 java.io.File;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.nio.file.Files;
|
|
|
|
+import java.nio.file.Paths;
|
|
|
|
+import java.security.KeyFactory;
|
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
|
+import java.security.PrivateKey;
|
|
|
|
+import java.security.spec.InvalidKeySpecException;
|
|
|
|
+import java.security.spec.PKCS8EncodedKeySpec;
|
|
|
|
+import java.util.Base64;
|
|
|
|
|
|
|
|
|
|
@Configuration
|
|
@Configuration
|
|
@@ -27,6 +46,9 @@ public class BeanConfig {
|
|
@Value("${cos.preffix}")
|
|
@Value("${cos.preffix}")
|
|
private String preffix;
|
|
private String preffix;
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private WxpayConfig wxpayConfig;
|
|
|
|
+
|
|
|
|
|
|
@Bean
|
|
@Bean
|
|
public COSClient configBean() {
|
|
public COSClient configBean() {
|
|
@@ -44,4 +66,47 @@ public class BeanConfig {
|
|
COSClient cosClient = new COSClient(cred, clientConfig);
|
|
COSClient cosClient = new COSClient(cred, clientConfig);
|
|
return cosClient;
|
|
return cosClient;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Bean
|
|
|
|
+ public HttpClient wxpayConfigBean() throws Exception{
|
|
|
|
+ ClassPathResource classPathResource = new ClassPathResource("");
|
|
|
|
+ File file = new ClassPathResource("wechatpay/apiclient_key.pem").getFile();
|
|
|
|
+ String realPath =file.getAbsolutePath();
|
|
|
|
+ PrivateKey privateKey = getPrivateKey(realPath);
|
|
|
|
+ // 加载平台证书(mchId:商户号,mchSerialNo:商户证书序列号,apiV3Key:V3密钥)
|
|
|
|
+ AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
|
|
|
|
+ new WechatPay2Credentials(wxpayConfig.getMerchantId(),
|
|
|
|
+ new PrivateKeySigner(wxpayConfig.getSerialNumber(), privateKey)),
|
|
|
|
+ wxpayConfig.getV3key().getBytes("utf-8"));
|
|
|
|
+
|
|
|
|
+ // 初始化httpClient
|
|
|
|
+ HttpClient httpClient = WechatPayHttpClientBuilder.create()
|
|
|
|
+ .withMerchant(wxpayConfig.getMerchantId(), wxpayConfig.getSerialNumber(), privateKey)
|
|
|
|
+ .withValidator(new WechatPay2Validator(verifier)).build();
|
|
|
|
+ return httpClient;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * 获取私钥。
|
|
|
|
+ *
|
|
|
|
+ * @param filename 私钥文件路径 (required)
|
|
|
|
+ * @return 私钥对象
|
|
|
|
+ */
|
|
|
|
+ public static PrivateKey getPrivateKey(String filename) throws IOException {
|
|
|
|
+ String content = new String(Files.readAllBytes(Paths.get(filename)), "utf-8");
|
|
|
|
+ try {
|
|
|
|
+ String privateKey = content.replace("-----BEGIN PRIVATE KEY-----", "")
|
|
|
|
+ .replace("-----END PRIVATE KEY-----", "")
|
|
|
|
+ .replaceAll("\\s+", "");
|
|
|
|
+
|
|
|
|
+ KeyFactory kf = KeyFactory.getInstance("RSA");
|
|
|
|
+ return kf.generatePrivate(
|
|
|
|
+ new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey)));
|
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
|
+ throw new RuntimeException("当前Java环境不支持RSA", e);
|
|
|
|
+ } catch (InvalidKeySpecException e) {
|
|
|
|
+ throw new RuntimeException("无效的密钥格式");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|