java用ssl,SSL和TLS之间的区别及其在Java中的使用

I am trying to establish an SSL or TLS connection between a Java client and server I am setting up.

I have been using SSLContext.getInstance("SSL") to build the SSLContext, and it worked.

I would like to know what the purpose of the protocol parameter is in SSLContext.getInstance(String protocol).

In particular, what changes between using SSLContext.getInstance("SSL") and SSLContext.getInstance("TLS"), or other possible values?

解决方案

Here is a rather detailed answer that I wrote a while back describing the difference between SSL and TLS. In short, TLS is the successor of SSL, and TLS 1.0 can be considered as "SSL 3.1".

These static methods each return an instance that implements at least

the requested secure socket protocol. The returned instance may

implement other protocols too. For example, getInstance("TLSv1") may

return a instance which implements "TLSv1", "TLSv1.1" and "TLSv1.2".

This is also mentioned in the Standard Names document.

In particular, if you check the Oracle/OpenJDK 7 source code for SSLContextImpl, you'll find that all its SSLContexts support all protocols (from SSLv3 using an SSLv2 Client Hello to TLS 1.2). What differs is which protocols are enabled by default. In addition, you shouldn't rely on this in general, since other Java implementations (e.g. the IBM JRE) could behave differently.

If you want a particular set of protocols to be used for a connection, you should use SSLSocket or SSLEngine's setEnabledProtocols method. Otherwise, it will use the default values, as described in the Providers documentation.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java进行SSL证书验证,可以使用Java Secure Socket Extension(JSSE)框架提供的工具类来实现。 JSSE提供了一个名为javax.net.ssl.SSLSocketFactory的类,它可以创建SSL连接,并提供了一些方法来验证SSL证书。其,通过设置javax.net.ssl.TrustManager接口的实现类来进行证书验证。 下面是一个简单的示例代码,用于验证SSL服务器的证书是否有效: ``` import java.io.*; import java.net.*; import javax.net.ssl.*; public class SSLTest { public static void main(String[] args) throws Exception { String host = "example.com"; int port = 443; // 创建SSL连接 SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault(); SSLSocket socket = (SSLSocket) factory.createSocket(host, port); // 设置证书验证 SSLContext context = SSLContext.getInstance("TLS"); context.init(null, new TrustManager[] { new X509TrustManager() { public void checkClientTrusted(X509Certificate[] chain, String authType) {} public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { // 验证证书是否有效 chain[0].checkValidity(); } public X509Certificate[] getAcceptedIssuers() { return null; } } }, null); socket.startHandshake(); socket.close(); } } ``` 在上面的代码,我们创建了一个SSLSocket对象,并设置了证书验证,其X509TrustManager接口的实现类用于验证证书是否有效。在checkServerTrusted方法,我们检查服务器证书链的第一个证书是否在有效期内,如果不在有效期内,则会抛出CertificateException异常。 需要注意的是,以上示例的证书验证并不安全,因为它只是检查服务器证书链的第一个证书是否在有效期内,并没有验证证书的真实性,因此建议在生产环境使用更安全的证书验证机制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值