mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add IoCopier
This commit is contained in:
@@ -10,6 +10,7 @@ import cn.hutool.core.io.resource.MultiFileResource;
|
||||
import cn.hutool.core.io.resource.Resource;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.net.SSLUtil;
|
||||
import cn.hutool.core.net.url.UrlBuilder;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
@@ -887,11 +888,7 @@ public class HttpRequest extends HttpBase<HttpRequest> {
|
||||
*/
|
||||
public HttpRequest setSSLProtocol(String protocol) {
|
||||
Assert.notBlank(protocol, "protocol must be not blank!");
|
||||
try {
|
||||
setSSLSocketFactory(SSLSocketFactoryBuilder.create().setProtocol(protocol).build());
|
||||
} catch (Exception e) {
|
||||
throw new HttpException(e);
|
||||
}
|
||||
setSSLSocketFactory(SSLUtil.createSSLContext(protocol).getSocketFactory());
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@@ -1,12 +1,7 @@
|
||||
package cn.hutool.http.ssl;
|
||||
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import static cn.hutool.http.ssl.SSLSocketFactoryBuilder.SSLv3;
|
||||
import static cn.hutool.http.ssl.SSLSocketFactoryBuilder.TLSv1;
|
||||
import static cn.hutool.http.ssl.SSLSocketFactoryBuilder.TLSv11;
|
||||
import static cn.hutool.http.ssl.SSLSocketFactoryBuilder.TLSv12;
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.net.SSLProtocols;
|
||||
|
||||
/**
|
||||
* 兼容android低版本SSL连接<br>
|
||||
@@ -20,10 +15,11 @@ import static cn.hutool.http.ssl.SSLSocketFactoryBuilder.TLSv12;
|
||||
public class AndroidSupportSSLFactory extends CustomProtocolsSSLFactory {
|
||||
|
||||
// Android低版本不重置的话某些SSL访问就会失败
|
||||
private static final String[] protocols = {SSLv3, TLSv1, TLSv11, TLSv12};
|
||||
private static final String[] protocols = {
|
||||
SSLProtocols.SSLv3, SSLProtocols.TLSv1, SSLProtocols.TLSv11, SSLProtocols.TLSv12};
|
||||
|
||||
public AndroidSupportSSLFactory() throws KeyManagementException, NoSuchAlgorithmException {
|
||||
public AndroidSupportSSLFactory() throws IORuntimeException {
|
||||
super(protocols);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,7 @@
|
||||
package cn.hutool.http.ssl;
|
||||
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.net.SSLUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
|
||||
import javax.net.ssl.SSLSocket;
|
||||
@@ -7,8 +9,6 @@ import javax.net.ssl.SSLSocketFactory;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
/**
|
||||
* 自定义支持协议类型的SSLSocketFactory
|
||||
@@ -24,12 +24,11 @@ public class CustomProtocolsSSLFactory extends SSLSocketFactory {
|
||||
* 构造
|
||||
*
|
||||
* @param protocols 支持协议列表
|
||||
* @throws KeyManagementException KeyManagementException
|
||||
* @throws NoSuchAlgorithmException 无此算法
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public CustomProtocolsSSLFactory(String... protocols) throws KeyManagementException, NoSuchAlgorithmException {
|
||||
public CustomProtocolsSSLFactory(String... protocols) throws IORuntimeException {
|
||||
this.protocols = protocols;
|
||||
this.base = SSLSocketFactoryBuilder.create().build();
|
||||
this.base = SSLUtil.createSSLContext(null).getSocketFactory();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -90,7 +89,7 @@ public class CustomProtocolsSSLFactory extends SSLSocketFactory {
|
||||
* @param socket SSLSocket
|
||||
*/
|
||||
private void resetProtocols(SSLSocket socket) {
|
||||
if(ArrayUtil.isNotEmpty(this.protocols)){
|
||||
if (ArrayUtil.isNotEmpty(this.protocols)) {
|
||||
socket.setEnabledProtocols(this.protocols);
|
||||
}
|
||||
}
|
||||
|
@@ -1,8 +1,5 @@
|
||||
package cn.hutool.http.ssl;
|
||||
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
/**
|
||||
* 默认的SSLSocketFactory
|
||||
*
|
||||
@@ -11,7 +8,7 @@ import java.security.NoSuchAlgorithmException;
|
||||
*/
|
||||
public class DefaultSSLFactory extends CustomProtocolsSSLFactory {
|
||||
|
||||
public DefaultSSLFactory() throws KeyManagementException, NoSuchAlgorithmException {
|
||||
public DefaultSSLFactory() {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -1,14 +1,11 @@
|
||||
package cn.hutool.http.ssl;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.HttpException;
|
||||
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
/**
|
||||
* 默认的SSL配置,当用户未设置相关信息时,使用默认设置,默认设置为单例模式。
|
||||
* 默认的全局SSL配置,当用户未设置相关信息时,使用默认设置,默认设置为单例模式。
|
||||
*
|
||||
* @author looly
|
||||
* @since 5.1.2
|
||||
@@ -25,16 +22,11 @@ public class DefaultSSLInfo {
|
||||
|
||||
static {
|
||||
TRUST_ANY_HOSTNAME_VERIFIER = new TrustAnyHostnameVerifier();
|
||||
|
||||
try {
|
||||
if (StrUtil.equalsIgnoreCase("dalvik", System.getProperty("java.vm.name"))) {
|
||||
// 兼容android低版本SSL连接
|
||||
DEFAULT_SSF = new AndroidSupportSSLFactory();
|
||||
} else {
|
||||
DEFAULT_SSF = new DefaultSSLFactory();
|
||||
}
|
||||
} catch (KeyManagementException | NoSuchAlgorithmException e) {
|
||||
throw new HttpException(e);
|
||||
if (StrUtil.equalsIgnoreCase("dalvik", System.getProperty("java.vm.name"))) {
|
||||
// 兼容android低版本SSL连接
|
||||
DEFAULT_SSF = new AndroidSupportSSLFactory();
|
||||
} else {
|
||||
DEFAULT_SSF = new DefaultSSLFactory();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package cn.hutool.http.ssl;
|
||||
|
||||
import cn.hutool.core.net.SSLContextBuilder;
|
||||
import cn.hutool.core.net.SSLProtocols;
|
||||
|
||||
import javax.net.ssl.KeyManager;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
@@ -14,38 +15,10 @@ import java.security.SecureRandom;
|
||||
*
|
||||
* @author Looly
|
||||
* @see SSLContextBuilder
|
||||
* @deprecated 请使用 {@link SSLContextBuilder}
|
||||
*/
|
||||
public class SSLSocketFactoryBuilder {
|
||||
|
||||
/**
|
||||
* Supports some version of SSL; may support other versions
|
||||
*/
|
||||
public static final String SSL = SSLContextBuilder.SSL;
|
||||
/**
|
||||
* Supports SSL version 2 or later; may support other versions
|
||||
*/
|
||||
public static final String SSLv2 = SSLContextBuilder.SSLv2;
|
||||
/**
|
||||
* Supports SSL version 3; may support other versions
|
||||
*/
|
||||
public static final String SSLv3 = SSLContextBuilder.SSLv3;
|
||||
|
||||
/**
|
||||
* Supports some version of TLS; may support other versions
|
||||
*/
|
||||
public static final String TLS = SSLContextBuilder.TLS;
|
||||
/**
|
||||
* Supports RFC 2246: TLS version 1.0 ; may support other versions
|
||||
*/
|
||||
public static final String TLSv1 = SSLContextBuilder.TLSv1;
|
||||
/**
|
||||
* Supports RFC 4346: TLS version 1.1 ; may support other versions
|
||||
*/
|
||||
public static final String TLSv11 = SSLContextBuilder.TLSv11;
|
||||
/**
|
||||
* Supports RFC 5246: TLS version 1.2 ; may support other versions
|
||||
*/
|
||||
public static final String TLSv12 = SSLContextBuilder.TLSv12;
|
||||
@Deprecated
|
||||
public class SSLSocketFactoryBuilder implements SSLProtocols {
|
||||
|
||||
SSLContextBuilder sslContextBuilder;
|
||||
|
||||
|
@@ -3,8 +3,8 @@ package cn.hutool.http;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.date.TimeInterval;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.net.SSLProtocols;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.http.ssl.SSLSocketFactoryBuilder;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -98,7 +98,7 @@ public class HttpRequestTest {
|
||||
// 禁用缓存
|
||||
.disableCache()
|
||||
// 自定义SSL版本
|
||||
.setSSLProtocol(SSLSocketFactoryBuilder.TLSv12);
|
||||
.setSSLProtocol(SSLProtocols.TLSv12);
|
||||
Console.log(request.execute().body());
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user