From a45c92bbe4dd3c80f0d3c24b1992fac43b6fa839 Mon Sep 17 00:00:00 2001 From: Looly Date: Tue, 15 Dec 2020 09:49:25 +0800 Subject: [PATCH] add PassAuth --- .../java/cn/hutool/core/net/PassAuth.java | 41 +++++++++++++++++++ .../main/java/cn/hutool/http/HttpRequest.java | 4 +- .../java/cn/hutool/http/HttpRequestTest.java | 26 ++++++++++-- 3 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 hutool-core/src/main/java/cn/hutool/core/net/PassAuth.java diff --git a/hutool-core/src/main/java/cn/hutool/core/net/PassAuth.java b/hutool-core/src/main/java/cn/hutool/core/net/PassAuth.java new file mode 100644 index 000000000..d87177355 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/net/PassAuth.java @@ -0,0 +1,41 @@ +package cn.hutool.core.net; + +import java.net.Authenticator; +import java.net.PasswordAuthentication; + +/** + * 账号密码形式的{@link Authenticator} 实现。 + * + * @author looly + * @since 5.5.3 + */ +public class PassAuth extends Authenticator { + + /** + * 创建账号密码形式的{@link Authenticator} 实现。 + * + * @param user 用户名 + * @param pass 密码 + * @return PassAuth + */ + public static PassAuth of(String user, char[] pass) { + return new PassAuth(user, pass); + } + + private final PasswordAuthentication auth; + + /** + * 构造 + * + * @param user 用户名 + * @param pass 密码 + */ + public PassAuth(String user, char[] pass) { + auth = new PasswordAuthentication(user, pass); + } + + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return auth; + } +} diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java b/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java index daba627c1..e52e3fef9 100644 --- a/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java @@ -1094,9 +1094,9 @@ public class HttpRequest extends HttpBase { } /** - * 调用转发,如果需要转发返回转发结果,否则返回null + * 调用转发,如果需要转发返回转发结果,否则返回{@code null} * - * @return {@link HttpResponse},无转发返回 null + * @return {@link HttpResponse},无转发返回 {@code null} */ private HttpResponse sendRedirectIfPossible() { if (this.maxRedirectCount < 1) { diff --git a/hutool-http/src/test/java/cn/hutool/http/HttpRequestTest.java b/hutool-http/src/test/java/cn/hutool/http/HttpRequestTest.java index 2dc9e487d..66a37197f 100644 --- a/hutool-http/src/test/java/cn/hutool/http/HttpRequestTest.java +++ b/hutool-http/src/test/java/cn/hutool/http/HttpRequestTest.java @@ -4,14 +4,13 @@ import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.TimeInterval; import cn.hutool.core.lang.Console; import cn.hutool.core.util.CharsetUtil; -import cn.hutool.http.HttpRequest; -import cn.hutool.http.HttpResponse; -import cn.hutool.http.HttpUtil; import cn.hutool.http.ssl.SSLSocketFactoryBuilder; import cn.hutool.json.JSONUtil; import org.junit.Ignore; import org.junit.Test; +import java.net.Authenticator; +import java.net.PasswordAuthentication; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -144,4 +143,25 @@ public class HttpRequestTest { Console.log(execute.body()); } + @Test + public void getByProxy(){ + System.setProperty("jdk.http.auth.tunneling.disabledSchemes", ""); + + // 用户名密码, 若已添加白名单则不需要添加 + final String ProxyUser = "t10757311156848"; + final String ProxyPass = "ikm5uu44"; + + Authenticator.setDefault(new Authenticator() { + public PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(ProxyUser, ProxyPass.toCharArray()); + } + }); + + final HttpResponse res = HttpRequest.get("https://httpbin.org/get") + .basicAuth(ProxyUser, ProxyPass) + .setHttpProxy("tps193.kdlapi.com", 15818).execute(); + + Console.log(res.body()); + } + }