From 9636725f83b4c14605ec4c6053d4574bc595a07c Mon Sep 17 00:00:00 2001 From: TomXin <766781886@qq.com> Date: Fri, 18 Feb 2022 18:41:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BFSSLContextBuilder=E5=AE=9E=E7=8E=B0Bui?= =?UTF-8?q?lder=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/net/SSLContextBuilder.java | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/net/SSLContextBuilder.java b/hutool-core/src/main/java/cn/hutool/core/net/SSLContextBuilder.java index 8a0efced2..d08c5fcdb 100644 --- a/hutool-core/src/main/java/cn/hutool/core/net/SSLContextBuilder.java +++ b/hutool-core/src/main/java/cn/hutool/core/net/SSLContextBuilder.java @@ -1,5 +1,7 @@ package cn.hutool.core.net; +import cn.hutool.core.builder.Builder; +import cn.hutool.core.exceptions.CheckedUtil; import cn.hutool.core.io.IORuntimeException; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.StrUtil; @@ -20,13 +22,13 @@ import java.security.SecureRandom; *
  • {@link TrustManager},默认{@link DefaultTrustManager},即信任全部
  • *
  • {@link SecureRandom}
  • * - * + *

    * 构建后可获得{@link SSLContext},通过调用{@link SSLContext#getSocketFactory()}获取{@link javax.net.ssl.SSLSocketFactory} * * @author Looly * @since 5.5.2 */ -public class SSLContextBuilder implements SSLProtocols { +public class SSLContextBuilder implements SSLProtocols, Builder { private String protocol = TLS; private KeyManager[] keyManagers; @@ -99,10 +101,21 @@ public class SSLContextBuilder implements SSLProtocols { * 构建{@link SSLContext} * * @return {@link SSLContext} - * @throws NoSuchAlgorithmException 无此算法 - * @throws KeyManagementException Key管理异常 */ - public SSLContext build() throws NoSuchAlgorithmException, KeyManagementException { + @Override + public SSLContext build() { + return CheckedUtil.uncheck(this::buildChecked, IORuntimeException::new).call(); + } + + /** + * 构建{@link SSLContext}需要处理异常 + * + * @return {@link SSLContext} + * @throws NoSuchAlgorithmException 无此算法异常 + * @throws KeyManagementException 密钥管理异常 + * @since 5.7.22 + */ + public SSLContext buildChecked() throws NoSuchAlgorithmException, KeyManagementException { SSLContext sslContext = SSLContext.getInstance(protocol); sslContext.init(this.keyManagers, this.trustManagers, this.secureRandom); return sslContext; @@ -116,7 +129,7 @@ public class SSLContextBuilder implements SSLProtocols { */ public SSLContext buildQuietly() throws IORuntimeException { try { - return build(); + return buildChecked(); } catch (GeneralSecurityException e) { throw new IORuntimeException(e); }