linwe
2024-08-09 8b7258c9427882bb1798f1502eaa35184c6e374e
src/main/java/org/springblade/common/config/WxPayConfig.java
@@ -12,15 +12,15 @@
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.io.FileInputStream;
import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;
//@Configuration
// @PropertySource("classpath:wxpay.properties") //读取配置文件
//@ConfigurationProperties(prefix = "wxpay") //读取wxpay节点
@Configuration
@ConfigurationProperties(prefix = "wxpay") //读取wxpay节点
@Data //使用set方法将wxpay节点中的值填充到当前类的属性中
@Slf4j
public class WxPayConfig {
@@ -112,7 +112,7 @@
    * @return
    */
//   @Bean
   public ScheduledUpdateCertificatesVerifier getVerifier(String privateKeyPath, String mchSerialNo, String mchId, String apiV3Key) {
   public ScheduledUpdateCertificatesVerifier getVerifier() {
      log.info("获取签名验证器");
@@ -141,7 +141,7 @@
    * @return
    */
//   @Bean(name = "wxPayClient")
   public CloseableHttpClient getWxPayClient(String privateKeyPath, String mchSerialNo, String mchId, String apiV3Key) {
   public CloseableHttpClient getWxPayClient(ScheduledUpdateCertificatesVerifier verifier) {
      log.info("获取httpClient");
@@ -150,7 +150,7 @@
      WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
         .withMerchant(mchId, mchSerialNo, privateKey)
         .withValidator(new WechatPay2Validator(getVerifier(privateKeyPath, mchSerialNo, mchId, apiV3Key)));
         .withValidator(new WechatPay2Validator(verifier));
      // ... 接下来,你仍然可以通过builder设置各种参数,来配置你的HttpClient
      // 通过WechatPayHttpClientBuilder构造的HttpClient,会自动的处理签名和验签,并进行证书自动更新
@@ -159,5 +159,30 @@
      return httpClient;
   }
   /**
    * 获取HttpClient,无需进行应答签名验证,跳过验签的流程
    */
//   @Bean(name = "wxPayNoSignClient")
   public CloseableHttpClient getWxPayNoSignClient() {
      //获取商户私钥
      PrivateKey privateKey = getPrivateKey(privateKeyPath);
      //用于构造HttpClient
      WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
         //设置商户信息
         .withMerchant(mchId, mchSerialNo, privateKey)
         //无需进行签名验证、通过withValidator((response) -> true)实现
         .withValidator((response) -> true);
      // 通过WechatPayHttpClientBuilder构造的HttpClient,会自动的处理签名和验签,并进行证书自动更新
      CloseableHttpClient httpClient = builder.build();
      log.info("== getWxPayNoSignClient END ==");
      return httpClient;
   }
}