首页 > 解决方案 > 如何检查请求的 TLS 版本

问题描述

我有okhttp3.OkHttpClient并且我使用 Retrofit2 发出 REST 请求。

interface WebService {
    @GET("/httptwo")
    public Call<String> executeRequest();
}


//in these 2 methods I initialize server IP, SSLContext, keystore, truststore and all the stuff.
OkHttpClient client = getClient();
Retrofit retrofit = getRetrofit(client);

//make call
WebService service = retrofit.create(WebService.class);
Call<String> call = service.executeRequest();
try {
    Response<String> response = call.execute();
    String responseBody = response.body();
    System.out.println(responseBody);
} catch (Exception e) {
    e.printStackTrace();
}

我向我的虚拟服务器提出请求。

@RestController
class SecureServiceController {
@RequestMapping(value = "/httptwo")
    public String httpTwoHandler() {
        String httpVersion = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest().getProtocol();
        System.out.println("http version: " + httpVersion);
        
        return "Hello from " + httpVersion;
    }

}

如您所见,我可以检查 http 协议的版本。但是如何检查使用的 TLS 版本?

标签: javaspring-bootretrofit2okhttptls1.3

解决方案


response.handshake().tlsVersion()

将例如TlsVersion.TLS_1_2

https://square.github.io/okhttp/4.x/okhttp/okhttp3/-tls-version/


推荐阅读