且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Java 7 中启用 TLS 1.2

更新时间:2023-01-16 23:30:40

有很多建议,但我发现其中最常见的有两个.

There are many suggestions but I found two of them most common.

我第一次尝试 export JAVA_OPTS="-Dhttps.protocols=SSLv3,TLSv1,TLSv1.1,TLSv1.2" 在程序启动之前在命令行上,但它对我不起作用.

I first tried export JAVA_OPTS="-Dhttps.protocols=SSLv3,TLSv1,TLSv1.1,TLSv1.2" on command line before startup of program but it didn't work for me.

然后我在启动类构造函数中添加了以下代码,它对我有用.

Then I added the following code in the startup class constructor and it worked for me.

try {
        SSLContext ctx = SSLContext.getInstance("TLSv1.2");
        ctx.init(null, null, null);
        SSLContext.setDefault(ctx);
} catch (Exception e) {
        System.out.println(e.getMessage());
}

坦率地说,我不知道为什么 ctx.init(null, null, null); 但所有 (SSL/TLS) 对我来说都很好.

Frankly, I don't know in detail why ctx.init(null, null, null); but all (SSL/TLS) is working fine for me.

还有一个选项:System.setProperty("https.protocols", "SSLv3,TLSv1,TLSv1.1,TLSv1.2");.它也将进入代码,但我没有尝试过.

There is one more option: System.setProperty("https.protocols", "SSLv3,TLSv1,TLSv1.1,TLSv1.2");. It will also go in code but I've not tried it.