Skip to content

Commit 00dd90c

Browse files
committed
Merge branch '3.5.x'
2 parents 84e3865 + 2917747 commit 00dd90c

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

framework/fit/java/fit-builtin/plugins/fit-http-client-okhttp/src/main/java/modelengine/fit/http/client/okhttp/OkHttpClientBuilderFactory.java

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import modelengine.fit.http.client.HttpClassicClientFactory;
1515
import modelengine.fit.http.protocol.util.SslUtils;
1616
import modelengine.fitframework.log.Logger;
17+
import modelengine.fitframework.util.ArrayUtils;
1718
import modelengine.fitframework.util.StringUtils;
1819
import okhttp3.OkHttpClient;
1920

@@ -29,6 +30,17 @@
2930
/**
3031
* 创建 OkHttpClient.Builder 实例工厂。
3132
*
33+
* <p><strong>安全配置说明:</strong></p>
34+
* <p>本框架提供 {@link HttpsConstants#CLIENT_SECURE_IGNORE_TRUST} 配置项,允许忽略SSL证书验证。</p>
35+
* <p><strong>警告:</strong>启用此选项将使应用程序容易受到中间人攻击!</p>
36+
*
37+
* <p>使用场景:</p>
38+
* <ul>
39+
* <li>开发环境:使用自签名证书或内网测试</li>
40+
* <li>测试环境:快速原型验证</li>
41+
* <li><strong>生产环境:绝对不应启用此选项</strong></li>
42+
* </ul>
43+
*
3244
* @author 杭潇
3345
* @since 2024-04-15
3446
*/
@@ -79,7 +91,7 @@ private static void setSslConfig(OkHttpClient.Builder clientBuilder, HttpClassic
7991
TrustManager[] trustManagers = getTrustManagersConfig(config, isIgnoreTrust);
8092

8193
SSLContext sslContext = SslUtils.getSslContext(keyManagers, trustManagers, isStrongRandom, secureProtocol);
82-
if (isIgnoreTrust || isTrustManagerSet(trustManagers)) {
94+
if (isTrustManagerSet(trustManagers)) {
8395
clientBuilder.sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustManagers[0]);
8496
}
8597
if (isIgnoreTrust || isHostnameVerificationIgnored(trustManagers, config)) {
@@ -100,6 +112,15 @@ private static KeyManager[] getKeyManagersConfig(HttpClassicClientFactory.Config
100112
private static TrustManager[] getTrustManagersConfig(HttpClassicClientFactory.Config config, boolean isIgnoreTrust)
101113
throws GeneralSecurityException {
102114
if (isIgnoreTrust) {
115+
log.warn("========================================================");
116+
log.warn("SECURITY WARNING: SSL/TLS Certificate Validation DISABLED!");
117+
log.warn("This configuration is INSECURE and should NEVER be used in production!");
118+
log.warn("Your application is vulnerable to man-in-the-middle attacks!");
119+
log.warn("Current setting: {} = true", HttpsConstants.CLIENT_SECURE_IGNORE_TRUST);
120+
log.warn("========================================================");
121+
if (log.isDebugEnabled()) {
122+
log.debug("Certificate validation disabled at:", new Exception("Stack trace for debugging"));
123+
}
103124
return getTrustAllCerts();
104125
}
105126
String trustStoreFile = cast(config.custom().get(HttpsConstants.CLIENT_SECURE_TRUST_STORE_FILE));
@@ -120,13 +141,37 @@ private static boolean isHostnameVerificationIgnored(TrustManager[] trustManager
120141
.getOrDefault(HttpsConstants.CLIENT_SECURE_IGNORE_HOSTNAME, false)));
121142
}
122143

144+
/**
145+
* 创建一个接受所有证书的 {@link TrustManager}{@code []},其中仅有一个 {@link TrustManager}。
146+
* <p>此方法是框架设计的一部分,用于支持开发环境的快速集成,安全风险已通过配置和日志机制向用户明确告知。</p>
147+
* <p><strong>安全警告:</strong>此 {@link TrustManager}
148+
* 不验证任何证书,会接受所有证书包括无效、过期或伪造的证书,仅应在开发环境中使用,生产环境使用将导致严重的安全风险。</p>
149+
*
150+
* @return 不验证任何证书的 {@link TrustManager}{@code []}。
151+
*/
123152
private static TrustManager[] getTrustAllCerts() {
124153
X509TrustManager x509TrustManager = new X509TrustManager() {
125154
@Override
126-
public void checkClientTrusted(X509Certificate[] chain, String authType) {}
155+
public void checkClientTrusted(X509Certificate[] chain, String authType) {
156+
// 记录客户端证书验证被跳过
157+
if (log.isDebugEnabled()) {
158+
log.debug("Bypassing client certificate validation (INSECURE MODE). [authType={}]", authType);
159+
}
160+
}
127161

128162
@Override
129-
public void checkServerTrusted(X509Certificate[] chain, String authType) {}
163+
public void checkServerTrusted(X509Certificate[] chain, String authType) {
164+
// 记录服务器证书验证被跳过,包含证书信息便于调试
165+
if (log.isDebugEnabled() && ArrayUtils.isNotEmpty(chain)) {
166+
X509Certificate cert = chain[0];
167+
log.debug("Bypassing server certificate validation (INSECURE MODE):");
168+
log.debug(" - Subject: {}", cert.getSubjectX500Principal());
169+
log.debug(" - Issuer: {}", cert.getIssuerX500Principal());
170+
log.debug(" - Serial Number: {}", cert.getSerialNumber());
171+
log.debug(" - Valid from {} to {}", cert.getNotBefore(), cert.getNotAfter());
172+
log.debug(" - Auth Type: {}", authType);
173+
}
174+
}
130175

131176
@Override
132177
public X509Certificate[] getAcceptedIssuers() {

0 commit comments

Comments
 (0)