Browse Source

refactor(file): 终于把证书的路径拼接问题修复好了

- 移除生产环境中的 /file 前缀
- 统一文件上传和访问路径
- 修复历史路径格式的兼容性问题
-优化路径拼接逻辑,提高代码可读性和维护性
pan
科技小王子 3 weeks ago
parent
commit
b4a3259fd5
  1. 60
      src/test/java/com/gxwebsoft/test/CertificatePathConcatenationTest.java

60
src/test/java/com/gxwebsoft/test/CertificatePathConcatenationTest.java

@ -12,7 +12,7 @@ import java.io.File;
/** /**
* 证书路径拼接测试 * 证书路径拼接测试
* 验证开发环境的路径拼接规则配置文件upload-path + dev/wechat/ + 租户ID * 验证开发环境的路径拼接规则配置文件upload-path + dev/wechat/ + 租户ID
*
*
* @author 科技小王子 * @author 科技小王子
* @since 2025-08-09 * @since 2025-08-09
*/ */
@ -22,72 +22,72 @@ public class CertificatePathConcatenationTest {
@Value("${spring.profiles.active:prod}") @Value("${spring.profiles.active:prod}")
private String activeProfile; private String activeProfile;
@Resource @Resource
private ConfigProperties configProperties; private ConfigProperties configProperties;
@Test @Test
public void testCertificatePathConcatenation() { public void testCertificatePathConcatenation() {
System.out.println("=== 证书路径拼接测试 ==="); System.out.println("=== 证书路径拼接测试 ===");
System.out.println("当前环境: " + activeProfile); System.out.println("当前环境: " + activeProfile);
if ("dev".equals(activeProfile)) { if ("dev".equals(activeProfile)) {
testDevEnvironmentPathConcatenation(); testDevEnvironmentPathConcatenation();
} else { } else {
testProdEnvironmentPathConcatenation(); testProdEnvironmentPathConcatenation();
} }
System.out.println("=== 证书路径拼接测试完成 ==="); System.out.println("=== 证书路径拼接测试完成 ===");
} }
private void testDevEnvironmentPathConcatenation() { private void testDevEnvironmentPathConcatenation() {
System.out.println("--- 开发环境路径拼接测试 ---"); System.out.println("--- 开发环境路径拼接测试 ---");
// 获取配置文件中的upload-path // 获取配置文件中的upload-path
String uploadPath = configProperties.getUploadPath(); String uploadPath = configProperties.getUploadPath();
System.out.println("配置文件upload-path: " + uploadPath); System.out.println("配置文件upload-path: " + uploadPath);
// 拼接规则:配置文件upload-path + dev/wechat/ + 租户ID // 拼接规则:配置文件upload-path + dev/wechat/ + 租户ID
String tenantId = "10550"; String tenantId = "10550";
String certBasePath = uploadPath + "dev/wechat/" + tenantId + "/"; String certBasePath = uploadPath + "dev/wechat/" + tenantId + "/";
String privateKeyPath = certBasePath + "apiclient_key.pem"; String privateKeyPath = certBasePath + "apiclient_key.pem";
String certPath = certBasePath + "apiclient_cert.pem"; String certPath = certBasePath + "apiclient_cert.pem";
System.out.println("拼接规则: upload-path + dev/wechat/ + 租户ID"); System.out.println("拼接规则: upload-path + dev/wechat/ + 租户ID");
System.out.println("租户ID: " + tenantId); System.out.println("租户ID: " + tenantId);
System.out.println("证书基础路径: " + certBasePath); System.out.println("证书基础路径: " + certBasePath);
System.out.println("私钥文件路径: " + privateKeyPath); System.out.println("私钥文件路径: " + privateKeyPath);
System.out.println("证书文件路径: " + certPath); System.out.println("证书文件路径: " + certPath);
// 验证路径是否正确 // 验证路径是否正确
File privateKeyFile = new File(privateKeyPath); File privateKeyFile = new File(privateKeyPath);
File certFile = new File(certPath); File certFile = new File(certPath);
System.out.println("--- 文件存在性验证 ---"); System.out.println("--- 文件存在性验证 ---");
System.out.println("私钥文件存在: " + privateKeyFile.exists()); System.out.println("私钥文件存在: " + privateKeyFile.exists());
System.out.println("证书文件存在: " + certFile.exists()); System.out.println("证书文件存在: " + certFile.exists());
if (privateKeyFile.exists()) { if (privateKeyFile.exists()) {
System.out.println("✅ 私钥文件路径拼接正确"); System.out.println("✅ 私钥文件路径拼接正确");
System.out.println(" 文件大小: " + privateKeyFile.length() + " bytes"); System.out.println(" 文件大小: " + privateKeyFile.length() + " bytes");
} else { } else {
System.out.println("❌ 私钥文件路径拼接错误或文件不存在"); System.out.println("❌ 私钥文件路径拼接错误或文件不存在");
} }
if (certFile.exists()) { if (certFile.exists()) {
System.out.println("✅ 证书文件路径拼接正确"); System.out.println("✅ 证书文件路径拼接正确");
System.out.println(" 文件大小: " + certFile.length() + " bytes"); System.out.println(" 文件大小: " + certFile.length() + " bytes");
} else { } else {
System.out.println("❌ 证书文件路径拼接错误或文件不存在"); System.out.println("❌ 证书文件路径拼接错误或文件不存在");
} }
// 验证期望的路径 // 验证期望的路径
String expectedPath = "/Users/gxwebsoft/JAVA/cms-java-code/src/main/resources/dev/wechat/10550/"; String expectedPath = "/Users/gxwebsoft/JAVA/cms-java-code/src/main/resources/dev/wechat/10550/";
System.out.println("--- 路径验证 ---"); System.out.println("--- 路径验证 ---");
System.out.println("期望的证书路径: " + expectedPath); System.out.println("期望的证书路径: " + expectedPath);
System.out.println("实际拼接路径: " + certBasePath); System.out.println("实际拼接路径: " + certBasePath);
System.out.println("路径匹配: " + expectedPath.equals(certBasePath)); System.out.println("路径匹配: " + expectedPath.equals(certBasePath));
if (expectedPath.equals(certBasePath)) { if (expectedPath.equals(certBasePath)) {
System.out.println("✅ 路径拼接规则正确"); System.out.println("✅ 路径拼接规则正确");
} else { } else {
@ -95,61 +95,61 @@ public class CertificatePathConcatenationTest {
System.out.println(" 请检查配置文件中的upload-path设置"); System.out.println(" 请检查配置文件中的upload-path设置");
} }
} }
private void testProdEnvironmentPathConcatenation() { private void testProdEnvironmentPathConcatenation() {
System.out.println("--- 生产环境路径配置测试 ---"); System.out.println("--- 生产环境路径配置测试 ---");
System.out.println("生产环境使用数据库配置的证书路径"); System.out.println("生产环境使用数据库配置的证书路径");
System.out.println("路径格式: {uploadPath}/file/{relativePath}"); System.out.println("路径格式: {uploadPath}/file/{relativePath}");
String uploadPath = configProperties.getUploadPath(); String uploadPath = configProperties.getUploadPath();
System.out.println("配置的upload-path: " + uploadPath); System.out.println("配置的upload-path: " + uploadPath);
// 模拟生产环境路径拼接 // 模拟生产环境路径拼接
String relativePath = "wechat/10550/apiclient_key.pem"; String relativePath = "wechat/10550/apiclient_key.pem";
String prodPath = uploadPath + "file/" + relativePath; String prodPath = uploadPath + "file/" + relativePath;
System.out.println("生产环境示例路径: " + prodPath); System.out.println("生产环境示例路径: " + prodPath);
System.out.println("✅ 生产环境路径配置逻辑正确"); System.out.println("✅ 生产环境路径配置逻辑正确");
} }
@Test @Test
public void testMultipleTenantPaths() { public void testMultipleTenantPaths() {
System.out.println("=== 多租户路径拼接测试 ==="); System.out.println("=== 多租户路径拼接测试 ===");
if (!"dev".equals(activeProfile)) { if (!"dev".equals(activeProfile)) {
System.out.println("跳过:仅在开发环境测试多租户路径"); System.out.println("跳过:仅在开发环境测试多租户路径");
return; return;
} }
String uploadPath = configProperties.getUploadPath(); String uploadPath = configProperties.getUploadPath();
String[] tenantIds = {"10324", "10398", "10547", "10549", "10550"}; String[] tenantIds = {"10324", "10398", "10547", "10549", "10550"};
System.out.println("配置文件upload-path: " + uploadPath); System.out.println("配置文件upload-path: " + uploadPath);
System.out.println("测试多个租户的证书路径拼接:"); System.out.println("测试多个租户的证书路径拼接:");
for (String tenantId : tenantIds) { for (String tenantId : tenantIds) {
String certBasePath = uploadPath + "dev/wechat/" + tenantId + "/"; String certBasePath = uploadPath + "dev/wechat/" + tenantId + "/";
String privateKeyPath = certBasePath + "apiclient_key.pem"; String privateKeyPath = certBasePath + "apiclient_key.pem";
File privateKeyFile = new File(privateKeyPath); File privateKeyFile = new File(privateKeyPath);
System.out.println("租户 " + tenantId + ": " + (privateKeyFile.exists() ? "✅" : "❌") + " " + privateKeyPath); System.out.println("租户 " + tenantId + ": " + (privateKeyFile.exists() ? "✅" : "❌") + " " + privateKeyPath);
} }
System.out.println("=== 多租户路径拼接测试完成 ==="); System.out.println("=== 多租户路径拼接测试完成 ===");
} }
@Test @Test
public void testConfigurationProperties() { public void testConfigurationProperties() {
System.out.println("=== 配置属性测试 ===");
System.out.println("=== 配置属性测试1 ===");
System.out.println("当前环境: " + activeProfile); System.out.println("当前环境: " + activeProfile);
System.out.println("ConfigProperties注入: " + (configProperties != null ? "✅" : "❌")); System.out.println("ConfigProperties注入: " + (configProperties != null ? "✅" : "❌"));
if (configProperties != null) { if (configProperties != null) {
System.out.println("upload-path: " + configProperties.getUploadPath()); System.out.println("upload-path: " + configProperties.getUploadPath());
System.out.println("upload-location: " + configProperties.getUploadLocation()); System.out.println("upload-location: " + configProperties.getUploadLocation());
System.out.println("server-url: " + configProperties.getServerUrl()); System.out.println("server-url: " + configProperties.getServerUrl());
} }
System.out.println("=== 配置属性测试完成 ==="); System.out.println("=== 配置属性测试完成 ===");
} }
} }

Loading…
Cancel
Save