This commit is contained in:
Looly
2023-03-10 00:51:16 +08:00
parent b3ddbcba23
commit 17e9cd97ac
9 changed files with 59 additions and 32 deletions

View File

@@ -18,7 +18,7 @@ import java.security.SecureRandom;
* <ul>
* <li>协议protocol默认TLS</li>
* <li>{@link KeyManager},默认空</li>
* <li>{@link TrustManager},默认{@link DefaultTrustManager},即信任全部</li>
* <li>{@link TrustManager},默认{@link TrustAnyTrustManager},即信任全部</li>
* <li>{@link SecureRandom}</li>
* </ul>
* <p>
@@ -32,7 +32,7 @@ public class SSLContextBuilder implements SSLProtocols, Builder<SSLContext> {
private String protocol = TLS;
private KeyManager[] keyManagers;
private TrustManager[] trustManagers = {DefaultTrustManager.INSTANCE};
private TrustManager[] trustManagers = {TrustAnyTrustManager.INSTANCE};
private SecureRandom secureRandom = new SecureRandom();

View File

@@ -15,23 +15,37 @@ import javax.net.ssl.TrustManager;
public class SSLUtil {
/**
* 创建{@link SSLContext}默认新人全部
* 创建{@link SSLContext}信任全部协议为TLS
*
* @param protocol SSL协议例如TLS等
* @return {@link SSLContext}
* @throws IORuntimeException 包装 GeneralSecurityException异常
*/
public static SSLContext createTrustAnySSLContext() throws IORuntimeException {
return createTrustAnySSLContext(null);
}
/**
* 创建{@link SSLContext},信任全部
*
* @param protocol SSL协议例如TLS等{@code null}表示默认TLS
* @return {@link SSLContext}
* @throws IORuntimeException 包装 GeneralSecurityException异常
* @since 5.7.8
*/
public static SSLContext createSSLContext(final String protocol) throws IORuntimeException{
return SSLContextBuilder.of().setProtocol(protocol).build();
public static SSLContext createTrustAnySSLContext(final String protocol) throws IORuntimeException {
return SSLContextBuilder.of()
.setProtocol(protocol)
// 信任所有服务端
.setTrustManagers(new TrustManager[]{TrustAnyTrustManager.INSTANCE})
.build();
}
/**
* 创建{@link SSLContext}
*
* @param protocol SSL协议例如TLS等
* @param keyManager 密钥管理器,{@code null}表示
* @param trustManager 信任管理器, {@code null}表示
* @param keyManager 密钥管理器,{@code null}表示默认
* @param trustManager 信任管理器, {@code null}表示默认
* @return {@link SSLContext}
* @throws IORuntimeException 包装 GeneralSecurityException异常
*/
@@ -46,8 +60,8 @@ public class SSLUtil {
* 创建和初始化{@link SSLContext}
*
* @param protocol SSL协议例如TLS等
* @param keyManagers 密钥管理器,{@code null}表示
* @param trustManagers 信任管理器, {@code null}表示
* @param keyManagers 密钥管理器,{@code null}表示默认
* @param trustManagers 信任管理器, {@code null}表示默认
* @return {@link SSLContext}
* @throws IORuntimeException 包装 GeneralSecurityException异常
*/

View File

@@ -6,19 +6,21 @@ import java.net.Socket;
import java.security.cert.X509Certificate;
/**
* 默认信任管理器默认信任所有客户端和服务端证书<br>
* 继承{@link X509ExtendedTrustManager}的原因见https://blog.csdn.net/ghaohao/article/details/79454913
* 新任所有信任管理器默认信任所有客户端和服务端证书<br>
* 继承{@link X509ExtendedTrustManager}的原因见<br>
* https://blog.csdn.net/ghaohao/article/details/79454913
*
* @author Looly
* @since 5.5.7
*/
public class DefaultTrustManager extends X509ExtendedTrustManager {
public class TrustAnyTrustManager extends X509ExtendedTrustManager {
/**
* 默认的全局单例默认信任管理器默认信任所有客户端和服务端证书
* 全局单例信任管理器默认信任所有客户端和服务端证书
*
* @since 5.7.8
*/
public static DefaultTrustManager INSTANCE = new DefaultTrustManager();
public static TrustAnyTrustManager INSTANCE = new TrustAnyTrustManager();
@Override
public X509Certificate[] getAcceptedIssuers() {