切换server数据库
This commit is contained in:
@@ -172,6 +172,19 @@ public class CertificateController extends BaseController {
|
||||
}
|
||||
}
|
||||
|
||||
@ApiOperation("检查数据库证书配置")
|
||||
@GetMapping("/database-check")
|
||||
@PreAuthorize("hasAuthority('system:certificate:view')")
|
||||
public ApiResult<Map<String, Object>> checkDatabaseCertificates() {
|
||||
try {
|
||||
Map<String, Object> result = certificateHealthService.checkDatabaseCertificates();
|
||||
return success("数据库证书检查完成", result);
|
||||
} catch (Exception e) {
|
||||
log.error("检查数据库证书配置失败", e);
|
||||
return new ApiResult<>(1, "检查数据库证书配置失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@ApiOperation("刷新证书缓存")
|
||||
@PostMapping("/refresh")
|
||||
@PreAuthorize("hasAuthority('system:certificate:manage')")
|
||||
|
||||
@@ -1,10 +1,20 @@
|
||||
package com.gxwebsoft.common.core.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.gxwebsoft.common.core.config.CertificateProperties;
|
||||
import com.gxwebsoft.common.core.config.ConfigProperties;
|
||||
import com.gxwebsoft.common.system.entity.Payment;
|
||||
import com.gxwebsoft.common.system.service.PaymentService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -21,12 +31,36 @@ public class CertificateHealthService {
|
||||
private final CertificateService certificateService;
|
||||
private final CertificateProperties certificateProperties;
|
||||
|
||||
@Resource
|
||||
private PaymentService paymentService;
|
||||
|
||||
@Resource
|
||||
private ConfigProperties configProperties;
|
||||
|
||||
@Value("${spring.profiles.active:dev}")
|
||||
private String active;
|
||||
|
||||
public CertificateHealthService(CertificateService certificateService,
|
||||
CertificateProperties certificateProperties) {
|
||||
this.certificateService = certificateService;
|
||||
this.certificateProperties = certificateProperties;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前租户ID
|
||||
*/
|
||||
private Integer getCurrentTenantId() {
|
||||
try {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication != null && authentication.getPrincipal() instanceof com.gxwebsoft.common.system.entity.User) {
|
||||
return ((com.gxwebsoft.common.system.entity.User) authentication.getPrincipal()).getTenantId();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("获取当前租户ID失败: {}", e.getMessage());
|
||||
}
|
||||
return 1; // 默认租户ID
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义健康检查结果类
|
||||
*/
|
||||
@@ -61,23 +95,31 @@ public class CertificateHealthService {
|
||||
Map<String, Object> details = new HashMap<>();
|
||||
boolean allHealthy = true;
|
||||
|
||||
// 检查微信支付证书
|
||||
// 检查微信支付证书(配置文件模式)
|
||||
Map<String, Object> wechatHealth = checkWechatPayCertificates();
|
||||
details.put("wechatPay", wechatHealth);
|
||||
if (!(Boolean) wechatHealth.get("healthy")) {
|
||||
allHealthy = false;
|
||||
}
|
||||
|
||||
// 检查支付宝证书
|
||||
// 检查支付宝证书(配置文件模式)
|
||||
Map<String, Object> alipayHealth = checkAlipayCertificates();
|
||||
details.put("alipay", alipayHealth);
|
||||
if (!(Boolean) alipayHealth.get("healthy")) {
|
||||
allHealthy = false;
|
||||
}
|
||||
|
||||
// 检查数据库中的支付配置证书路径
|
||||
Map<String, Object> databaseCertHealth = checkDatabaseCertificates();
|
||||
details.put("databaseCertificates", databaseCertHealth);
|
||||
if (!(Boolean) databaseCertHealth.get("healthy")) {
|
||||
allHealthy = false;
|
||||
}
|
||||
|
||||
// 添加系统信息
|
||||
details.put("loadMode", certificateProperties.getLoadMode());
|
||||
details.put("certRootPath", certificateProperties.getCertRootPath());
|
||||
details.put("currentEnvironment", active);
|
||||
|
||||
if (allHealthy) {
|
||||
return HealthResult.up(details);
|
||||
@@ -250,4 +292,132 @@ public class CertificateHealthService {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查数据库中存储的证书路径
|
||||
*/
|
||||
public Map<String, Object> checkDatabaseCertificates() {
|
||||
Map<String, Object> health = new HashMap<>();
|
||||
boolean healthy = true;
|
||||
Map<String, Object> certificates = new HashMap<>();
|
||||
|
||||
try {
|
||||
Integer tenantId = getCurrentTenantId();
|
||||
log.info("检查租户 {} 的数据库证书配置", tenantId);
|
||||
|
||||
// 查询微信支付配置
|
||||
List<Payment> wechatPayments = paymentService.list(
|
||||
new LambdaQueryWrapper<Payment>()
|
||||
.eq(Payment::getCode, "wxPay")
|
||||
.eq(Payment::getStatus, true)
|
||||
.eq(Payment::getTenantId, tenantId)
|
||||
);
|
||||
|
||||
Map<String, Object> wechatDbCerts = new HashMap<>();
|
||||
if (!wechatPayments.isEmpty()) {
|
||||
Payment wechatPayment = wechatPayments.get(0);
|
||||
log.info("找到微信支付配置: 商户号={}, 序列号={}", wechatPayment.getMchId(), wechatPayment.getMerchantSerialNumber());
|
||||
|
||||
// 检查微信支付证书路径
|
||||
String apiclientKey = wechatPayment.getApiclientKey();
|
||||
String apiclientCert = wechatPayment.getApiclientCert();
|
||||
|
||||
if (apiclientKey != null && !apiclientKey.isEmpty()) {
|
||||
String keyPath = getAbsoluteCertPath(apiclientKey);
|
||||
boolean keyExists = new File(keyPath).exists();
|
||||
wechatDbCerts.put("privateKey", Map.of(
|
||||
"relativePath", apiclientKey,
|
||||
"absolutePath", keyPath,
|
||||
"exists", keyExists
|
||||
));
|
||||
log.info("微信支付私钥证书 - 相对路径: {}, 绝对路径: {}, 存在: {}", apiclientKey, keyPath, keyExists);
|
||||
if (!keyExists) healthy = false;
|
||||
} else {
|
||||
wechatDbCerts.put("privateKey", Map.of("error", "私钥路径未配置"));
|
||||
healthy = false;
|
||||
}
|
||||
|
||||
if (apiclientCert != null && !apiclientCert.isEmpty()) {
|
||||
String certPath = getAbsoluteCertPath(apiclientCert);
|
||||
boolean certExists = new File(certPath).exists();
|
||||
wechatDbCerts.put("certificate", Map.of(
|
||||
"relativePath", apiclientCert,
|
||||
"absolutePath", certPath,
|
||||
"exists", certExists
|
||||
));
|
||||
log.info("微信支付证书 - 相对路径: {}, 绝对路径: {}, 存在: {}", apiclientCert, certPath, certExists);
|
||||
if (!certExists) healthy = false;
|
||||
} else {
|
||||
wechatDbCerts.put("certificate", Map.of("error", "证书路径未配置"));
|
||||
healthy = false;
|
||||
}
|
||||
|
||||
wechatDbCerts.put("merchantId", wechatPayment.getMchId());
|
||||
wechatDbCerts.put("serialNumber", wechatPayment.getMerchantSerialNumber());
|
||||
wechatDbCerts.put("apiV3Key", wechatPayment.getApiKey() != null ? "已配置" : "未配置");
|
||||
|
||||
} else {
|
||||
wechatDbCerts.put("error", "未找到微信支付配置");
|
||||
healthy = false;
|
||||
log.warn("租户 {} 未找到微信支付配置", tenantId);
|
||||
}
|
||||
|
||||
certificates.put("wechatPay", wechatDbCerts);
|
||||
|
||||
// 查询支付宝配置(如果有的话)
|
||||
List<Payment> alipayPayments = paymentService.list(
|
||||
new LambdaQueryWrapper<Payment>()
|
||||
.eq(Payment::getCode, "alipay")
|
||||
.eq(Payment::getStatus, true)
|
||||
.eq(Payment::getTenantId, tenantId)
|
||||
);
|
||||
|
||||
Map<String, Object> alipayDbCerts = new HashMap<>();
|
||||
if (!alipayPayments.isEmpty()) {
|
||||
Payment alipayPayment = alipayPayments.get(0);
|
||||
log.info("找到支付宝配置: 应用ID={}", alipayPayment.getAppId());
|
||||
|
||||
// 这里可以添加支付宝证书路径检查逻辑
|
||||
alipayDbCerts.put("appId", alipayPayment.getAppId());
|
||||
alipayDbCerts.put("status", "支付宝配置存在,但证书路径检查需要根据具体字段实现");
|
||||
} else {
|
||||
alipayDbCerts.put("status", "未找到支付宝配置");
|
||||
log.info("租户 {} 未找到支付宝配置", tenantId);
|
||||
}
|
||||
|
||||
certificates.put("alipay", alipayDbCerts);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("检查数据库证书配置失败", e);
|
||||
certificates.put("error", e.getMessage());
|
||||
healthy = false;
|
||||
}
|
||||
|
||||
health.put("healthy", healthy);
|
||||
health.put("certificates", certificates);
|
||||
return health;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取证书的完整绝对路径
|
||||
*/
|
||||
private String getAbsoluteCertPath(String relativePath) {
|
||||
if (relativePath == null || relativePath.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// 如果是生产环境,证书存储在上传目录
|
||||
if (!"dev".equals(active)) {
|
||||
String uploadPath = configProperties.getUploadPath();
|
||||
// 修改路径拼接规则:uploadPath + "file" + 数据库存储的相对路径
|
||||
String fullPath = uploadPath + "file" + relativePath;
|
||||
log.debug("生产环境证书路径构建 - 上传根路径: {}, 相对路径: {}, 完整路径: {}",
|
||||
uploadPath, relativePath, fullPath);
|
||||
return fullPath;
|
||||
} else {
|
||||
// 开发环境,可能需要不同的处理逻辑
|
||||
log.debug("开发环境证书路径: {}", relativePath);
|
||||
return relativePath;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,7 +116,33 @@ public class CertificateService {
|
||||
* @return 证书路径
|
||||
*/
|
||||
public String getWechatPayCertPath(String fileName) {
|
||||
return certificateProperties.getWechatPayCertPath(fileName);
|
||||
String certPath = certificateProperties.getWechatPayCertPath(fileName);
|
||||
log.debug("获取微信支付证书路径 - 文件名: {}, 路径: {}", fileName, certPath);
|
||||
|
||||
// 打印完整的绝对路径信息
|
||||
if (certificateProperties.isClasspathMode()) {
|
||||
log.info("微信支付证书路径模式: CLASSPATH");
|
||||
log.info("微信支付证书相对路径: {}", certPath);
|
||||
try {
|
||||
ClassPathResource resource = new ClassPathResource(certPath);
|
||||
if (resource.exists()) {
|
||||
String absolutePath = resource.getFile().getAbsolutePath();
|
||||
log.info("微信支付证书完整绝对路径: {}", absolutePath);
|
||||
} else {
|
||||
log.warn("微信支付证书文件不存在于classpath: {}", certPath);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("无法获取微信支付证书绝对路径: {}", e.getMessage());
|
||||
}
|
||||
} else {
|
||||
java.io.File file = new java.io.File(certPath);
|
||||
String absolutePath = file.getAbsolutePath();
|
||||
log.info("微信支付证书路径模式: FILESYSTEM");
|
||||
log.info("微信支付证书完整绝对路径: {}", absolutePath);
|
||||
log.info("微信支付证书文件是否存在: {}", file.exists());
|
||||
}
|
||||
|
||||
return certPath;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -126,7 +152,33 @@ public class CertificateService {
|
||||
* @return 证书路径
|
||||
*/
|
||||
public String getAlipayCertPath(String fileName) {
|
||||
return certificateProperties.getAlipayCertPath(fileName);
|
||||
String certPath = certificateProperties.getAlipayCertPath(fileName);
|
||||
log.debug("获取支付宝证书路径 - 文件名: {}, 路径: {}", fileName, certPath);
|
||||
|
||||
// 打印完整的绝对路径信息
|
||||
if (certificateProperties.isClasspathMode()) {
|
||||
log.info("支付宝证书路径模式: CLASSPATH");
|
||||
log.info("支付宝证书相对路径: {}", certPath);
|
||||
try {
|
||||
ClassPathResource resource = new ClassPathResource(certPath);
|
||||
if (resource.exists()) {
|
||||
String absolutePath = resource.getFile().getAbsolutePath();
|
||||
log.info("支付宝证书完整绝对路径: {}", absolutePath);
|
||||
} else {
|
||||
log.warn("支付宝证书文件不存在于classpath: {}", certPath);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("无法获取支付宝证书绝对路径: {}", e.getMessage());
|
||||
}
|
||||
} else {
|
||||
java.io.File file = new java.io.File(certPath);
|
||||
String absolutePath = file.getAbsolutePath();
|
||||
log.info("支付宝证书路径模式: FILESYSTEM");
|
||||
log.info("支付宝证书完整绝对路径: {}", absolutePath);
|
||||
log.info("支付宝证书文件是否存在: {}", file.exists());
|
||||
}
|
||||
|
||||
return certPath;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -82,9 +82,18 @@ public class AlipayConfigUtil {
|
||||
log.debug("获取支付宝配置,租户ID: {}", tenantId);
|
||||
String key = "cache".concat(tenantId.toString()).concat(":setting:payment");
|
||||
log.debug("Redis缓存key: {}", key);
|
||||
String cache = stringRedisTemplate.opsForValue().get(key);
|
||||
|
||||
// 测试期间注释掉从缓存获取支付配置
|
||||
// String cache = stringRedisTemplate.opsForValue().get(key);
|
||||
// if (cache == null) {
|
||||
// throw new BusinessException("支付方式未配置");
|
||||
// }
|
||||
|
||||
// 测试期间:模拟缓存为空的情况
|
||||
String cache = null;
|
||||
log.debug("测试模式:支付宝配置缓存设为null");
|
||||
if (cache == null) {
|
||||
throw new BusinessException("支付方式未配置");
|
||||
throw new BusinessException("支付方式未配置(测试模式:缓存已注释)");
|
||||
}
|
||||
|
||||
// 解析json数据
|
||||
@@ -102,9 +111,36 @@ public class AlipayConfigUtil {
|
||||
this.alipayRootCert = certificateService.getAlipayCertPath(alipayConfig.getAlipayRootCertFile());
|
||||
|
||||
log.info("开发环境支付宝证书路径:");
|
||||
log.info("应用证书: {}", this.appCertPublicKey);
|
||||
log.info("支付宝证书: {}", this.alipayCertPublicKey);
|
||||
log.info("根证书: {}", this.alipayRootCert);
|
||||
log.info("应用证书相对路径: {}", this.appCertPublicKey);
|
||||
log.info("支付宝证书相对路径: {}", this.alipayCertPublicKey);
|
||||
log.info("根证书相对路径: {}", this.alipayRootCert);
|
||||
|
||||
// 打印完整的绝对路径
|
||||
try {
|
||||
if (certificateProperties.isClasspathMode()) {
|
||||
log.info("支付宝证书加载模式: CLASSPATH");
|
||||
org.springframework.core.io.ClassPathResource appCertResource = new org.springframework.core.io.ClassPathResource(this.appCertPublicKey);
|
||||
org.springframework.core.io.ClassPathResource alipayCertResource = new org.springframework.core.io.ClassPathResource(this.alipayCertPublicKey);
|
||||
org.springframework.core.io.ClassPathResource rootCertResource = new org.springframework.core.io.ClassPathResource(this.alipayRootCert);
|
||||
|
||||
if (appCertResource.exists()) {
|
||||
log.info("应用证书完整绝对路径: {}", appCertResource.getFile().getAbsolutePath());
|
||||
}
|
||||
if (alipayCertResource.exists()) {
|
||||
log.info("支付宝证书完整绝对路径: {}", alipayCertResource.getFile().getAbsolutePath());
|
||||
}
|
||||
if (rootCertResource.exists()) {
|
||||
log.info("根证书完整绝对路径: {}", rootCertResource.getFile().getAbsolutePath());
|
||||
}
|
||||
} else {
|
||||
log.info("支付宝证书加载模式: FILESYSTEM");
|
||||
log.info("应用证书完整绝对路径: {}", new java.io.File(this.appCertPublicKey).getAbsolutePath());
|
||||
log.info("支付宝证书完整绝对路径: {}", new java.io.File(this.alipayCertPublicKey).getAbsolutePath());
|
||||
log.info("根证书完整绝对路径: {}", new java.io.File(this.alipayRootCert).getAbsolutePath());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("获取支付宝证书绝对路径失败: {}", e.getMessage());
|
||||
}
|
||||
|
||||
// 检查证书文件是否存在
|
||||
if (!certificateService.certificateExists("alipay", alipayConfig.getAppCertPublicKeyFile())) {
|
||||
@@ -118,14 +154,20 @@ public class AlipayConfigUtil {
|
||||
}
|
||||
} else {
|
||||
// 生产环境:使用上传的证书文件
|
||||
this.appCertPublicKey = pathConfig.getUploadPath() + "file" + payment.getString("appCertPublicKey");
|
||||
this.alipayCertPublicKey = pathConfig.getUploadPath() + "file" + payment.getString("alipayCertPublicKey");
|
||||
this.alipayRootCert = pathConfig.getUploadPath() + "file" + payment.getString("alipayRootCert");
|
||||
// 修改路径拼接规则:uploadPath + "file" + 数据库存储的相对路径
|
||||
String appCertPath = payment.getString("appCertPublicKey");
|
||||
String alipayCertPath = payment.getString("alipayCertPublicKey");
|
||||
String rootCertPath = payment.getString("alipayRootCert");
|
||||
|
||||
log.info("生产环境支付宝证书路径:");
|
||||
log.info("应用证书: {}", this.appCertPublicKey);
|
||||
log.info("支付宝证书: {}", this.alipayCertPublicKey);
|
||||
log.info("根证书: {}", this.alipayRootCert);
|
||||
this.appCertPublicKey = pathConfig.getUploadPath() + "file" + appCertPath;
|
||||
this.alipayCertPublicKey = pathConfig.getUploadPath() + "file" + alipayCertPath;
|
||||
this.alipayRootCert = pathConfig.getUploadPath() + "file" + rootCertPath;
|
||||
|
||||
log.info("生产环境支付宝证书路径构建:");
|
||||
log.info("上传根路径: {}", pathConfig.getUploadPath());
|
||||
log.info("应用证书 - 数据库路径: {}, 完整路径: {}", appCertPath, this.appCertPublicKey);
|
||||
log.info("支付宝证书 - 数据库路径: {}, 完整路径: {}", alipayCertPath, this.alipayCertPublicKey);
|
||||
log.info("根证书 - 数据库路径: {}, 完整路径: {}", rootCertPath, this.alipayRootCert);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("配置支付宝证书路径失败: {}", e.getMessage(), e);
|
||||
|
||||
@@ -26,6 +26,7 @@ import io.swagger.annotations.ApiModelProperty;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@@ -116,18 +117,44 @@ public class WxNativePayController extends BaseController {
|
||||
return build;
|
||||
}
|
||||
|
||||
String key = "Payment:wxPay:".concat(tenantId.toString());
|
||||
final Payment payment = redisUtil.get(key, Payment.class);
|
||||
log.debug("获取支付配置: {}", payment);
|
||||
// String key = "Payment:wxPay:".concat(tenantId.toString());
|
||||
// 测试期间注释掉从缓存获取支付配置
|
||||
// final Payment payment = redisUtil.get(key, Payment.class);
|
||||
// log.debug("从缓存获取支付配置: {}", payment);
|
||||
|
||||
// 测试期间直接从数据库获取支付配置
|
||||
final Payment payment = null; // 暂时设为null,强制从数据库获取
|
||||
log.debug("测试模式:不从缓存获取支付配置,payment设为null");
|
||||
|
||||
String apiclientKey;
|
||||
try {
|
||||
if (active.equals("dev")) {
|
||||
// 开发环境:使用证书服务获取证书路径
|
||||
apiclientKey = certificateService.getWechatPayCertPath(
|
||||
String relativePath = certificateService.getWechatPayCertPath(
|
||||
certificateProperties.getWechatPay().getDev().getPrivateKeyFile()
|
||||
);
|
||||
log.info("开发环境使用证书路径: {}", apiclientKey);
|
||||
|
||||
// 获取完整的绝对路径
|
||||
if (certificateProperties.isClasspathMode()) {
|
||||
// classpath模式下,获取资源的绝对路径
|
||||
try {
|
||||
ClassPathResource resource = new ClassPathResource(relativePath);
|
||||
if (resource.exists()) {
|
||||
apiclientKey = resource.getFile().getAbsolutePath();
|
||||
} else {
|
||||
apiclientKey = "classpath:" + relativePath;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
apiclientKey = "classpath:" + relativePath;
|
||||
}
|
||||
} else {
|
||||
// 文件系统模式下,直接使用绝对路径
|
||||
apiclientKey = new java.io.File(relativePath).getAbsolutePath();
|
||||
}
|
||||
|
||||
log.info("开发环境微信支付证书完整绝对路径: {}", apiclientKey);
|
||||
log.info("证书相对路径: {}", relativePath);
|
||||
log.info("证书加载模式: {}", certificateProperties.getLoadMode());
|
||||
|
||||
// 检查证书文件是否存在
|
||||
if (!certificateService.certificateExists("wechat",
|
||||
@@ -135,17 +162,52 @@ public class WxNativePayController extends BaseController {
|
||||
throw new RuntimeException("微信支付私钥证书文件不存在");
|
||||
}
|
||||
} else {
|
||||
// 生产环境:需要从数据库获取支付配置
|
||||
if (payment == null) {
|
||||
throw new RuntimeException("测试模式:支付配置为空,无法获取证书路径");
|
||||
}
|
||||
// 生产环境:使用上传的证书文件
|
||||
apiclientKey = config.getUploadPath().concat("/file").concat(payment.getApiclientKey());
|
||||
log.info("生产环境使用证书路径: {}", apiclientKey);
|
||||
// 修改路径拼接规则:uploadPath + "file" + 数据库存储的相对路径
|
||||
String relativePath = payment.getApiclientKey();
|
||||
apiclientKey = config.getUploadPath() + "file" + relativePath;
|
||||
log.info("生产环境证书路径构建 - 上传根路径: {}", config.getUploadPath());
|
||||
log.info("生产环境证书路径构建 - 数据库相对路径: {}", relativePath);
|
||||
log.info("生产环境证书路径构建 - 完整路径: {}", apiclientKey);
|
||||
}
|
||||
|
||||
Config wxConfig = new RSAAutoCertificateConfig.Builder()
|
||||
.merchantId(payment.getMchId())
|
||||
.privateKeyFromPath(apiclientKey)
|
||||
.merchantSerialNumber(payment.getMerchantSerialNumber())
|
||||
.apiV3Key(payment.getApiKey())
|
||||
.build();
|
||||
Config wxConfig;
|
||||
if (active.equals("dev") && payment == null) {
|
||||
// 开发环境测试配置 - 使用测试商户号和序列号
|
||||
log.info("开发环境测试模式:使用测试微信支付参数");
|
||||
log.warn("注意:当前使用的是测试配置,请确保证书文件与测试商户号匹配");
|
||||
|
||||
// 测试用的商户号和序列号(需要根据实际测试环境调整)
|
||||
String testMerchantId = "1246610101"; // 测试商户号
|
||||
String testMerchantSerialNumber = "2903B872D5CA36E525FAEC37AEDB22E54ECDE7B7"; // 测试序列号
|
||||
String testApiV3Key = certificateProperties.getWechatPay().getDev().getApiV3Key();
|
||||
|
||||
log.info("测试商户号: {}", testMerchantId);
|
||||
log.info("测试序列号: {}", testMerchantSerialNumber);
|
||||
log.info("APIv3密钥: {}", testApiV3Key != null ? "已配置" : "未配置");
|
||||
|
||||
wxConfig = new RSAAutoCertificateConfig.Builder()
|
||||
.merchantId(testMerchantId)
|
||||
.privateKeyFromPath(apiclientKey)
|
||||
.merchantSerialNumber(testMerchantSerialNumber)
|
||||
.apiV3Key(testApiV3Key)
|
||||
.build();
|
||||
} else if (payment != null) {
|
||||
// 正常模式:使用数据库配置
|
||||
log.info("正常模式:使用数据库中的微信支付配置");
|
||||
wxConfig = new RSAAutoCertificateConfig.Builder()
|
||||
.merchantId(payment.getMchId())
|
||||
.privateKeyFromPath(apiclientKey)
|
||||
.merchantSerialNumber(payment.getMerchantSerialNumber())
|
||||
.apiV3Key(payment.getApiKey())
|
||||
.build();
|
||||
} else {
|
||||
throw new RuntimeException("支付配置不可用:payment为null且非开发环境");
|
||||
}
|
||||
|
||||
log.info("微信支付配置创建成功");
|
||||
WxNativeUtil.addConfig(tenantId, wxConfig);
|
||||
|
||||
@@ -121,8 +121,9 @@ public class SettingServiceImpl extends ServiceImpl<SettingMapper, Setting> impl
|
||||
final JSONObject jsonObject = JSONObject.parseObject(data.getContent());
|
||||
final String mchId = jsonObject.getString("mchId");
|
||||
final String apiclientKey = jsonObject.getString("apiclientKey");
|
||||
final String privateKey = pathConfig.getUploadPath().concat("file").concat(apiclientKey);
|
||||
final String apiclientCert = pathConfig.getUploadPath().concat("file").concat(jsonObject.getString("apiclientCert"));
|
||||
// 修改路径拼接规则:uploadPath + "file" + 数据库存储的相对路径
|
||||
final String privateKey = pathConfig.getUploadPath() + "file" + apiclientKey;
|
||||
final String apiclientCert = pathConfig.getUploadPath() + "file" + jsonObject.getString("apiclientCert");
|
||||
final String merchantSerialNumber = jsonObject.getString("merchantSerialNumber");
|
||||
final String apiV3key = jsonObject.getString("wechatApiKey");
|
||||
if(config == null){
|
||||
|
||||
@@ -9,6 +9,11 @@ spring:
|
||||
password: jdj7HYEdYHnYEFBy
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
redis:
|
||||
database: 0
|
||||
host: 1Panel-redis-Q1LE
|
||||
port: 6379
|
||||
password: redis_WSDb88
|
||||
|
||||
# 日志配置
|
||||
logging:
|
||||
|
||||
139
src/test/java/com/gxwebsoft/common/core/PaymentConfigTest.java
Normal file
139
src/test/java/com/gxwebsoft/common/core/PaymentConfigTest.java
Normal file
@@ -0,0 +1,139 @@
|
||||
package com.gxwebsoft.common.core;
|
||||
|
||||
import com.gxwebsoft.common.core.service.CertificateService;
|
||||
import com.gxwebsoft.common.core.service.CertificateHealthService;
|
||||
import com.gxwebsoft.common.core.config.CertificateProperties;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 支付配置测试类
|
||||
* 用于测试支付配置缓存注释和证书路径打印功能
|
||||
*/
|
||||
@Slf4j
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("dev")
|
||||
public class PaymentConfigTest {
|
||||
|
||||
@Autowired
|
||||
private CertificateService certificateService;
|
||||
|
||||
@Autowired
|
||||
private CertificateProperties certificateProperties;
|
||||
|
||||
@Autowired
|
||||
private CertificateHealthService certificateHealthService;
|
||||
|
||||
@Test
|
||||
public void testWechatPayCertPath() {
|
||||
log.info("=== 测试微信支付证书路径获取 ===");
|
||||
|
||||
try {
|
||||
String privateKeyFile = certificateProperties.getWechatPay().getDev().getPrivateKeyFile();
|
||||
log.info("配置的私钥文件名: {}", privateKeyFile);
|
||||
|
||||
String certPath = certificateService.getWechatPayCertPath(privateKeyFile);
|
||||
log.info("获取到的证书路径: {}", certPath);
|
||||
|
||||
// 测试证书是否存在
|
||||
boolean exists = certificateService.certificateExists("wechat", privateKeyFile);
|
||||
log.info("证书文件是否存在: {}", exists);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("测试微信支付证书路径失败: {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAlipayCertPath() {
|
||||
log.info("=== 测试支付宝证书路径获取 ===");
|
||||
|
||||
try {
|
||||
CertificateProperties.AlipayConfig alipayConfig = certificateProperties.getAlipay();
|
||||
|
||||
String appCertFile = alipayConfig.getAppCertPublicKeyFile();
|
||||
String alipayCertFile = alipayConfig.getAlipayCertPublicKeyFile();
|
||||
String rootCertFile = alipayConfig.getAlipayRootCertFile();
|
||||
|
||||
log.info("配置的应用证书文件名: {}", appCertFile);
|
||||
log.info("配置的支付宝证书文件名: {}", alipayCertFile);
|
||||
log.info("配置的根证书文件名: {}", rootCertFile);
|
||||
|
||||
String appCertPath = certificateService.getAlipayCertPath(appCertFile);
|
||||
String alipayCertPath = certificateService.getAlipayCertPath(alipayCertFile);
|
||||
String rootCertPath = certificateService.getAlipayCertPath(rootCertFile);
|
||||
|
||||
log.info("获取到的应用证书路径: {}", appCertPath);
|
||||
log.info("获取到的支付宝证书路径: {}", alipayCertPath);
|
||||
log.info("获取到的根证书路径: {}", rootCertPath);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("测试支付宝证书路径失败: {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCertificateProperties() {
|
||||
log.info("=== 测试证书配置属性 ===");
|
||||
|
||||
log.info("证书加载模式: {}", certificateProperties.getLoadMode());
|
||||
log.info("证书根路径: {}", certificateProperties.getCertRootPath());
|
||||
log.info("开发环境证书路径: {}", certificateProperties.getDevCertPath());
|
||||
log.info("是否为classpath模式: {}", certificateProperties.isClasspathMode());
|
||||
|
||||
// 微信支付配置
|
||||
CertificateProperties.WechatPayConfig wechatPay = certificateProperties.getWechatPay();
|
||||
log.info("微信支付证书目录: {}", wechatPay.getCertDir());
|
||||
log.info("微信支付APIv3密钥: {}", wechatPay.getDev().getApiV3Key() != null ? "已配置" : "未配置");
|
||||
log.info("微信支付私钥文件: {}", wechatPay.getDev().getPrivateKeyFile());
|
||||
|
||||
// 支付宝配置
|
||||
CertificateProperties.AlipayConfig alipay = certificateProperties.getAlipay();
|
||||
log.info("支付宝证书目录: {}", alipay.getCertDir());
|
||||
log.info("支付宝应用证书文件: {}", alipay.getAppCertPublicKeyFile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDatabaseCertificateCheck() {
|
||||
log.info("=== 测试数据库证书配置检查 ===");
|
||||
|
||||
try {
|
||||
Map<String, Object> result = certificateHealthService.checkDatabaseCertificates();
|
||||
log.info("数据库证书检查结果: {}", result);
|
||||
|
||||
Boolean healthy = (Boolean) result.get("healthy");
|
||||
log.info("证书健康状态: {}", healthy ? "健康" : "不健康");
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> certificates = (Map<String, Object>) result.get("certificates");
|
||||
if (certificates != null) {
|
||||
log.info("证书详细信息:");
|
||||
certificates.forEach((key, value) -> {
|
||||
log.info(" {}: {}", key, value);
|
||||
});
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("测试数据库证书检查失败: {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCertificateHealthCheck() {
|
||||
log.info("=== 测试完整证书健康检查 ===");
|
||||
|
||||
try {
|
||||
CertificateHealthService.HealthResult healthResult = certificateHealthService.health();
|
||||
log.info("证书健康检查状态: {}", healthResult.getStatus());
|
||||
log.info("证书健康检查详情: {}", healthResult.getDetails());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("测试证书健康检查失败: {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user