mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
0e85436647
commit
e7ace4ae57
@ -45,7 +45,7 @@ public class SSLContextBuilder implements SSLProtocols, Builder<SSLContext> {
|
||||
|
||||
private String protocol = TLS;
|
||||
private KeyManager[] keyManagers;
|
||||
private TrustManager[] trustManagers = {TrustAnyTrustManager.INSTANCE};
|
||||
private TrustManager[] trustManagers = TrustManagerUtil.TRUST_ANYS;
|
||||
private SecureRandom secureRandom = new SecureRandom();
|
||||
|
||||
|
||||
|
@ -12,77 +12,19 @@
|
||||
|
||||
package org.dromara.hutool.core.net.ssl;
|
||||
|
||||
import org.dromara.hutool.core.exception.HutoolException;
|
||||
import org.dromara.hutool.core.io.IORuntimeException;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
|
||||
import javax.net.ssl.*;
|
||||
import java.security.KeyStore;
|
||||
import java.security.KeyStoreException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.Provider;
|
||||
import javax.net.ssl.KeyManager;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManager;
|
||||
|
||||
/**
|
||||
* SSL(Secure Sockets Layer 安全套接字协议)相关工具封装
|
||||
* SSL(Secure Sockets Layer 安全套接字协议)中的{@link SSLContext}相关工具封装
|
||||
*
|
||||
* @author looly
|
||||
* @since 5.5.2
|
||||
*/
|
||||
public class SSLUtil {
|
||||
|
||||
/**
|
||||
* 获取指定的{@link X509TrustManager}<br>
|
||||
* 此方法主要用于获取自签证书的{@link X509TrustManager}
|
||||
*
|
||||
* @param keyStore {@link KeyStore}
|
||||
* @param provider 算法提供者,如bc,{@code null}表示默认
|
||||
* @return {@link X509TrustManager} or {@code null}
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static X509TrustManager getTrustManager(final KeyStore keyStore, final Provider provider) {
|
||||
return getTrustManager(keyStore, null, provider);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的{@link X509TrustManager}<br>
|
||||
* 此方法主要用于获取自签证书的{@link X509TrustManager}
|
||||
*
|
||||
* @param keyStore {@link KeyStore}
|
||||
* @param algorithm 算法名称,如"SunX509",{@code null}表示默认SunX509
|
||||
* @param provider 算法提供者,如bc,{@code null}表示默认SunJSSE
|
||||
* @return {@link X509TrustManager} or {@code null}
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static X509TrustManager getTrustManager(final KeyStore keyStore, String algorithm, final Provider provider) {
|
||||
final TrustManagerFactory tmf;
|
||||
|
||||
if(StrUtil.isEmpty(algorithm)){
|
||||
algorithm = TrustManagerFactory.getDefaultAlgorithm();
|
||||
}
|
||||
try {
|
||||
if(null == provider){
|
||||
tmf = TrustManagerFactory.getInstance(algorithm);
|
||||
} else{
|
||||
tmf = TrustManagerFactory.getInstance(algorithm, provider);
|
||||
}
|
||||
} catch (final NoSuchAlgorithmException e) {
|
||||
throw new HutoolException(e);
|
||||
}
|
||||
try {
|
||||
tmf.init(keyStore);
|
||||
} catch (final KeyStoreException e) {
|
||||
throw new HutoolException(e);
|
||||
}
|
||||
|
||||
final TrustManager[] tms = tmf.getTrustManagers();
|
||||
for (final TrustManager tm : tms) {
|
||||
if (tm instanceof X509TrustManager) {
|
||||
return (X509TrustManager) tm;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
public class SSLContextUtil {
|
||||
|
||||
/**
|
||||
* 创建{@link SSLContext},信任全部,协议为TLS
|
||||
@ -106,7 +48,7 @@ public class SSLUtil {
|
||||
return SSLContextBuilder.of()
|
||||
.setProtocol(protocol)
|
||||
// 信任所有服务端
|
||||
.setTrustManagers(new TrustManager[]{TrustAnyTrustManager.INSTANCE})
|
||||
.setTrustManagers(TrustManagerUtil.TRUST_ANYS)
|
||||
.build();
|
||||
}
|
||||
|
@ -0,0 +1,128 @@
|
||||
/*
|
||||
* Copyright (c) 2024. looly(loolly@aliyun.com)
|
||||
* Hutool is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* https://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.core.net.ssl;
|
||||
|
||||
import org.dromara.hutool.core.exception.HutoolException;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.TrustManagerFactory;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import java.security.KeyStore;
|
||||
import java.security.KeyStoreException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.Provider;
|
||||
|
||||
/**
|
||||
* {@link TrustManager}相关工具类
|
||||
*
|
||||
* @author Looly
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public class TrustManagerUtil {
|
||||
|
||||
/**
|
||||
* 信任所有
|
||||
*/
|
||||
public static final X509TrustManager[] TRUST_ANYS = {TrustAnyTrustManager.INSTANCE};
|
||||
|
||||
/**
|
||||
* 获取默认的{@link TrustManager},为SunX509<br>
|
||||
* 此方法主要用于获取自签证书的{@link X509TrustManager}
|
||||
*
|
||||
* @return {@link X509TrustManager} or {@code null}
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static X509TrustManager getDefaultTrustManager() {
|
||||
return getTrustManager(null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的{@link X509TrustManager}<br>
|
||||
* 此方法主要用于获取自签证书的{@link X509TrustManager}
|
||||
*
|
||||
* @param keyStore {@link KeyStore}
|
||||
* @param provider 算法提供者,如bc,{@code null}表示默认
|
||||
* @return {@link X509TrustManager} or {@code null}
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static X509TrustManager getTrustManager(final KeyStore keyStore, final Provider provider) {
|
||||
return getTrustManager(keyStore, null, provider);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的{@link X509TrustManager}<br>
|
||||
* 此方法主要用于获取自签证书的{@link X509TrustManager}
|
||||
*
|
||||
* @param keyStore {@link KeyStore}
|
||||
* @param algorithm 算法名称,如"SunX509",{@code null}表示默认SunX509
|
||||
* @param provider 算法提供者,如bc,{@code null}表示默认SunJSSE
|
||||
* @return {@link X509TrustManager} or {@code null}
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static X509TrustManager getTrustManager(final KeyStore keyStore, final String algorithm, final Provider provider) {
|
||||
final TrustManager[] tms = getTrustManagers(keyStore, algorithm, provider);
|
||||
for (final TrustManager tm : tms) {
|
||||
if (tm instanceof X509TrustManager) {
|
||||
return (X509TrustManager) tm;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认的{@link TrustManager},为SunX509<br>
|
||||
* 此方法主要用于获取自签证书的{@link TrustManager}
|
||||
*
|
||||
* @return {@link X509TrustManager} or {@code null}
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static TrustManager[] getDefaultTrustManagers() {
|
||||
return getTrustManagers(null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的{@link TrustManager}<br>
|
||||
* 此方法主要用于获取自签证书的{@link TrustManager}
|
||||
*
|
||||
* @param keyStore {@link KeyStore}
|
||||
* @param algorithm 算法名称,如"SunX509",{@code null}表示默认SunX509
|
||||
* @param provider 算法提供者,如bc,{@code null}表示默认SunJSSE
|
||||
* @return {@link TrustManager} or {@code null}
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static TrustManager[] getTrustManagers(final KeyStore keyStore, String algorithm, final Provider provider) {
|
||||
final TrustManagerFactory tmf;
|
||||
|
||||
if(StrUtil.isEmpty(algorithm)){
|
||||
algorithm = TrustManagerFactory.getDefaultAlgorithm();
|
||||
}
|
||||
try {
|
||||
if(null == provider){
|
||||
tmf = TrustManagerFactory.getInstance(algorithm);
|
||||
} else{
|
||||
tmf = TrustManagerFactory.getInstance(algorithm, provider);
|
||||
}
|
||||
} catch (final NoSuchAlgorithmException e) {
|
||||
throw new HutoolException(e);
|
||||
}
|
||||
try {
|
||||
tmf.init(keyStore);
|
||||
} catch (final KeyStoreException e) {
|
||||
throw new HutoolException(e);
|
||||
}
|
||||
|
||||
return tmf.getTrustManagers();
|
||||
}
|
||||
}
|
@ -13,7 +13,7 @@
|
||||
package org.dromara.hutool.http.ssl;
|
||||
|
||||
import org.dromara.hutool.core.net.ssl.SSLProtocols;
|
||||
import org.dromara.hutool.core.net.ssl.SSLUtil;
|
||||
import org.dromara.hutool.core.net.ssl.SSLContextUtil;
|
||||
import org.dromara.hutool.core.net.ssl.SSLContextBuilder;
|
||||
import org.dromara.hutool.core.net.ssl.TrustAnyHostnameVerifier;
|
||||
import org.dromara.hutool.core.net.ssl.TrustAnyTrustManager;
|
||||
@ -47,7 +47,7 @@ public class SSLInfo {
|
||||
*/
|
||||
public static final SSLInfo TRUST_ANY = SSLInfo.of()
|
||||
.setHostnameVerifier(TrustAnyHostnameVerifier.INSTANCE)
|
||||
.setSslContext(SSLUtil.createTrustAnySSLContext())
|
||||
.setSslContext(SSLContextUtil.createTrustAnySSLContext())
|
||||
.setTrustManager(TrustAnyTrustManager.INSTANCE);
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user