报错信息如下:

Connected to the target VM, address: '127.0.0.1:56696', transport: 'socket'

javax.net.ssl.SSLException: Unsupported or unrecognized SSL message

	at sun.security.ssl.SSLSocketInputRecord.handleUnknownRecord(SSLSocketInputRecord.java:455)
	at sun.security.ssl.SSLSocketInputRecord.decode(SSLSocketInputRecord.java:184)
	at sun.security.ssl.SSLTransport.decode(SSLTransport.java:109)
	at sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1401)
	at sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1309)
	at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:440)
	at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.kt:379)
	at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.kt:337)
	at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:209)
	at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:226)
	at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:106)
	at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:74)
	at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:255)
	at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
	at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:517)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:750)

Disconnected from the target VM, address: '127.0.0.1:56696', transport: 'socket'

从错误信息来看,问题的核心是 SSL 握手失败,具体错误是:

这表明客户端(可能是 OkHttp)尝试与服务器建立 SSL/TLS 连接时,服务器返回了不支持或无法识别的 SSL 消息。以下是可能的原因和解决方法:


1. 服务器未正确配置 SSL/TLS

  • 问题原因

    • 服务器可能未正确配置 SSL/TLS,或者客户端尝试使用 HTTPS 连接到一个仅支持 HTTP 的服务器。

  • 解决方法

    1. 确保服务器已正确配置 SSL/TLS。

    2. 如果服务器仅支持 HTTP,将客户端的请求 URL 从 https:// 改为 http://


2. 客户端和服务器协议不匹配

  • 问题原因

    • 客户端和服务器使用的 SSL/TLS 协议版本不匹配。

  • 解决方法

    1. 确保客户端和服务器使用兼容的 SSL/TLS 协议版本。

    2. 在客户端代码中,显式指定支持的协议版本。例如,使用 OkHttp 时:

OkHttpClient client = new OkHttpClient.Builder()
    .sslSocketFactory(sslSocketFactory, trustManager)
    .protocols(Arrays.asList(Protocol.HTTP_1_1, Protocol.HTTP_2))
    .build();

3. 证书问题

  • 问题原因

    • 服务器的 SSL 证书可能无效或不受客户端信任。

  • 解决方法

    1. 确保服务器的 SSL 证书有效且由受信任的证书颁发机构(CA)签发。

    2. 如果使用自签名证书,需要在客户端代码中显式信任该证书。例如,使用 OkHttp 时:

TrustManager[] trustAllCerts = new TrustManager[]{
    new X509TrustManager() {
        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) {}
        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) {}
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }
    }
};

SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new SecureRandom());
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

OkHttpClient client = new OkHttpClient.Builder()
    .sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0])
    .hostnameVerifier((hostname, session) -> true)
    .build();

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐