简体   繁体   English

如何使用 java 或 spring 库使 rest 服务仅使用 TLS V1.2?

[英]How make rest service to use only TLS V1.2 using java or spring libraries?

The service provider who is hosting a rest service is asking to communicate using TLS version 1.2 only.托管 rest 服务的服务提供商要求仅使用 TLS 1.2 版进行通信。 So now I need to make my application to communicate with that service using TLS v 1.2.所以现在我需要让我的应用程序使用 TLS v 1.2 与该服务进行通信。

I know, in java8 we have an option of disabling the legacy version of TLS and SSL using the property jdk.tls.disabledAlgorithms=SSLv3, RC4 in java.security file.我知道,在 java8 中,我们可以选择使用 java.security 文件中的属性 jdk.tls.disabledAlgorithms=SSLv3, RC4 来禁用旧版本的 TLS 和 SSL。

But in a server there will be other process using the same java settings, so I'm worried like if I change for that property in java.security file, then it will be applicable to other services which are using the same settings.但是在服务器中会有其他进程使用相同的 java 设置,所以我担心如果我在 java.security 文件中更改该属性,那么它将适用于使用相同设置的其他服务。

Question: I would like to know if there is any other way to make my rest calls use only particular TLS version, through application code using java/spring libraries?问题:我想知道是否有任何其他方法可以通过使用 java/spring 库的应用程序代码使我的 rest 调用仅使用特定的 TLS 版本?

Create a SSLContext to use the required protocol:创建一个SSLContext以使用所需的协议:

SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(null, null, null);

If the client code or library uses HttpsURLConnection then you can set the default SSLSocketFactory for all the HTTS Connections:如果客户端代码或库使用 HttpsURLConnection,那么您可以为所有 HTTPS 连接设置默认的SSLSocketFactory

HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());

Or at a per connection level:或者在每个连接级别:

HttpsURLConnection urlConnection = ....;
urlConnection.setSSLSocketFactory(sslContext.getSocketFactory());
urlConnection.connect();

Different libraries may have different systems to provide the SSLSocketFactory or SSLContext .不同的库可能有不同的系统来提供SSLSocketFactorySSLContext For example, if you are using JAX-RS, you can create a REST client that uses the SSLContext with:例如,如果您使用的是 JAX-RS,则可以创建一个 REST 客户端,该客户端使用SSLContext

Client client = ClientBuilder.newBuilder().sslContext(sslContext).build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM