【问题标题】:How to check TLS version of request如何检查请求的 TLS 版本
【发布时间】:2021-09-15 13:26:13
【问题描述】:

我有 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 版本?

【问题讨论】:

    标签: java spring-boot retrofit2 okhttp tls1.3


    【解决方案1】:
    response.handshake().tlsVersion()
    

    例如是TlsVersion.TLS_1_2

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

    【讨论】:

    • 只是给后代的小提示:这里使用了okhttp3.Response。我使用 Retrofit2,所以我有 retrofit2.Response。为了获得 okhttp3.Response 我使用了 myRetrofitResponse.raw()
    猜你喜欢
    • 2020-11-03
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多