From d4b23afb55e9d0ed4da8d019db9a9758ff4aa99f Mon Sep 17 00:00:00 2001 From: sheiy Date: Fri, 10 Jul 2020 12:04:43 +0800 Subject: [PATCH] =?UTF-8?q?pref:=20=E5=A6=82=E6=9E=9C=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E4=BA=86http=E4=BB=A3=E7=90=86=E5=88=99?= =?UTF-8?q?=E5=88=9B=E5=BB=BAhttpClient=E6=97=B6=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E4=BB=A3=E7=90=86=20(#956)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修改缓存时长 * 如果系统设置了http代理则创建httpClient时设置代理 * 如果系统设置了http代理则创建httpClient时设置代理 * 如果系统设置了http代理则创建httpClient时设置代理 * 如果系统设置了http代理则创建httpClient时设置代理 * 如果系统设置了http代理则创建httpClient时设置代理 添加注释 * 添加单元测试 没有useJUnitPlatform()单元测试跑不起来 * 修改单元测试,删除zxing的依赖 * 修正错误的http配置格式 Co-authored-by: Sheiy <35429674@qq.com> --- build.gradle | 3 - .../run/halo/app/utils/HttpClientUtils.java | 89 ++++++++++++++++--- .../halo/app/utils/HttpClientUtilsTest.java | 34 +++++++ 3 files changed, 112 insertions(+), 14 deletions(-) create mode 100644 src/test/java/run/halo/app/utils/HttpClientUtilsTest.java diff --git a/build.gradle b/build.gradle index 54e7038e0..a2deff79c 100644 --- a/build.gradle +++ b/build.gradle @@ -61,7 +61,6 @@ ext { levelDbVersion = "0.12" annotationsVersion = "3.0.1u2" jedisVersion = '3.3.0' - zxingVersion = "3.4.0" huaweiObsVersion = "3.19.7" githubApiVersion = "1.84" powermockVersion = "1.6.6" @@ -114,8 +113,6 @@ dependencies { implementation "net.sf.image4j:image4j:$image4jVersion" implementation "org.flywaydb:flyway-core:$flywayVersion" - implementation "com.google.zxing:core:$zxingVersion" - implementation "org.iq80.leveldb:leveldb:$levelDbVersion" implementation "redis.clients:jedis:$jedisVersion" runtimeOnly "com.h2database:h2:$h2Version" diff --git a/src/main/java/run/halo/app/utils/HttpClientUtils.java b/src/main/java/run/halo/app/utils/HttpClientUtils.java index afe21667e..b1590a23a 100644 --- a/src/main/java/run/halo/app/utils/HttpClientUtils.java +++ b/src/main/java/run/halo/app/utils/HttpClientUtils.java @@ -1,17 +1,27 @@ package run.halo.app.utils; +import cn.hutool.core.lang.Tuple; +import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpHost; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; import org.apache.http.client.config.RequestConfig; import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContextBuilder; import org.springframework.core.io.ByteArrayResource; import org.springframework.lang.NonNull; import javax.net.ssl.SSLContext; +import java.net.URI; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; +import java.util.Objects; /** * Http client utilities. @@ -41,14 +51,71 @@ public class HttpClientUtils { @NonNull public static CloseableHttpClient createHttpsClient(int timeout) throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException { SSLContext sslContext = new SSLContextBuilder() - .loadTrustMaterial(null, (certificate, authType) -> true) - .build(); + .loadTrustMaterial(null, (certificate, authType) -> true) + .build(); - return HttpClients.custom() - .setSSLContext(sslContext) - .setSSLHostnameVerifier(new NoopHostnameVerifier()) - .setDefaultRequestConfig(getRequestConfig(timeout)) - .build(); + return resolveProxySetting(HttpClients.custom()) + .setSSLContext(sslContext) + .setSSLHostnameVerifier(new NoopHostnameVerifier()) + .setDefaultRequestConfig(getRequestConfig(timeout)) + .build(); + } + + /** + * resolve system proxy config + * + * @param httpClientBuilder the httpClientBuilder + * @return the argument + */ + private static HttpClientBuilder resolveProxySetting(final HttpClientBuilder httpClientBuilder) { + final String httpProxyEnv = System.getenv("http_proxy"); + if (StringUtils.isNotBlank(httpProxyEnv)) { + final Tuple httpProxy = resolveHttpProxy(httpProxyEnv); + final HttpHost httpHost = HttpHost.create(httpProxy.get(0)); + httpClientBuilder.setProxy(httpHost); + if (httpProxy.getMembers().length == 3) { + //set proxy credentials + final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + credentialsProvider.setCredentials(new AuthScope(httpHost.getHostName(), httpHost.getPort()), + new UsernamePasswordCredentials(httpProxy.get(1), httpProxy.get(2))); + httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); + } + } + return httpClientBuilder; + } + + /** + * @param httpProxy http proxy env values + * @return resolved http proxy values; first is host(@nonNull), second is username(@nullable), third is password(@nullable) + */ + private static Tuple resolveHttpProxy(final String httpProxy) { + final URI proxyUri = URI.create(httpProxy); + int port = proxyUri.getPort(); + if (port == -1) { + if (Objects.equals("http", proxyUri.getScheme())) { + port = 80; + } + if (Objects.equals("https", proxyUri.getScheme())) { + port = 443; + } + } + final String hostUrl = proxyUri.getScheme() + "://" + proxyUri.getHost() + ":" + port; + final String usernamePassword = proxyUri.getUserInfo(); + if (StringUtils.isNotBlank(usernamePassword)) { + final String username; + final String password; + final int atColon = usernamePassword.indexOf(':'); + if (atColon >= 0) { + username = usernamePassword.substring(0, atColon); + password = usernamePassword.substring(atColon + 1); + } else { + username = usernamePassword; + password = null; + } + return new Tuple(hostUrl, username, password); + } else { + return new Tuple(hostUrl); + } } /** @@ -59,10 +126,10 @@ public class HttpClientUtils { */ private static RequestConfig getRequestConfig(int timeout) { return RequestConfig.custom() - .setConnectTimeout(timeout) - .setConnectionRequestTimeout(timeout) - .setSocketTimeout(timeout) - .build(); + .setConnectTimeout(timeout) + .setConnectionRequestTimeout(timeout) + .setSocketTimeout(timeout) + .build(); } diff --git a/src/test/java/run/halo/app/utils/HttpClientUtilsTest.java b/src/test/java/run/halo/app/utils/HttpClientUtilsTest.java new file mode 100644 index 000000000..3079a86e9 --- /dev/null +++ b/src/test/java/run/halo/app/utils/HttpClientUtilsTest.java @@ -0,0 +1,34 @@ +package run.halo.app.utils; + +import cn.hutool.core.lang.Tuple; +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Method; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class HttpClientUtilsTest { + + @Test + void resolveHttpProxyTest() throws Exception { + final Method resolveHttpProxy = HttpClientUtils.class.getDeclaredMethod("resolveHttpProxy", String.class); + resolveHttpProxy.setAccessible(true); + + Tuple result = (Tuple) resolveHttpProxy.invoke(null, "http://127.0.0.1"); + assertEquals(result.get(0), "http://127.0.0.1:80"); + + result = (Tuple) resolveHttpProxy.invoke(null, "https://127.0.0.1"); + assertEquals(result.get(0), "https://127.0.0.1:443"); + + result = (Tuple) resolveHttpProxy.invoke(null, "https://127.0.0.1:123"); + assertEquals(result.get(0), "https://127.0.0.1:123"); + + result = (Tuple) resolveHttpProxy.invoke(null, "https://u:p@127.0.0.1:123"); + assertArrayEquals(result.getMembers(), new Object[]{"https://127.0.0.1:123", "u", "p"}); + + result = (Tuple) resolveHttpProxy.invoke(null, "https://u@127.0.0.1"); + assertArrayEquals(result.getMembers(), new Object[]{"https://127.0.0.1:443", "u", null}); + } + +}