From 36a8cdb98875ec2fd7aa5386ba9aa95617528d20 Mon Sep 17 00:00:00 2001 From: taojinlong Date: Mon, 2 Mar 2026 11:43:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20ck=20=E6=94=AF=E6=8C=81https?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/dataease/datasource/type/CK.java | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/core/core-backend/src/main/java/io/dataease/datasource/type/CK.java b/core/core-backend/src/main/java/io/dataease/datasource/type/CK.java index 6e2acc39ed..f21243614e 100644 --- a/core/core-backend/src/main/java/io/dataease/datasource/type/CK.java +++ b/core/core-backend/src/main/java/io/dataease/datasource/type/CK.java @@ -12,11 +12,16 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Pattern; @Data @Component("ck") public class CK extends DatasourceConfiguration { + private static final Map SSL_CERT_PATH_CACHE = new ConcurrentHashMap<>(); private String driver = "com.clickhouse.jdbc.ClickHouseDriver"; private String extraParams = ""; private String compressAlgorithm = "none"; // 默认设置为none以避免HTTP压缩问题 @@ -76,19 +81,44 @@ public class CK extends DatasourceConfiguration { } private String writeTempCert(String certContent, String prefix) { + String cacheKey = prefix + ":" + sha256(certContent); + String cachedPath = SSL_CERT_PATH_CACHE.get(cacheKey); + if (StringUtils.isNotBlank(cachedPath) && Files.exists(Paths.get(cachedPath))) { + return cachedPath; + } try { Path certDir = Paths.get(System.getProperty("java.io.tmpdir"), "dataease2", "clickhouse-ssl"); Files.createDirectories(certDir); Path certFile = Files.createTempFile(certDir, "de-" + prefix + "-", ".pem"); Files.writeString(certFile, certContent, StandardCharsets.UTF_8, StandardOpenOption.TRUNCATE_EXISTING); certFile.toFile().deleteOnExit(); - return certFile.toAbsolutePath().toString(); + String certPath = certFile.toAbsolutePath().toString(); + SSL_CERT_PATH_CACHE.put(cacheKey, certPath); + return certPath; } catch (IOException e) { DEException.throwException("SSL cert write failed: " + e.getMessage()); return null; } } + private String sha256(String content) { + try { + byte[] digest = MessageDigest.getInstance("SHA-256").digest(content.getBytes(StandardCharsets.UTF_8)); + StringBuilder hex = new StringBuilder(digest.length * 2); + for (byte b : digest) { + String h = Integer.toHexString(b & 0xff); + if (h.length() == 1) { + hex.append('0'); + } + hex.append(h); + } + return hex.toString(); + } catch (NoSuchAlgorithmException e) { + DEException.throwException("SHA-256 not supported: " + e.getMessage()); + return ""; + } + } + private String appendParam(String jdbcUrl, String paramName, String paramValue) { String join = jdbcUrl.contains("?") ? "&" : "?"; return jdbcUrl + join + paramName + "=" + paramValue;