mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add PassAuth
This commit is contained in:
parent
54fdf60bc3
commit
a45c92bbe4
41
hutool-core/src/main/java/cn/hutool/core/net/PassAuth.java
Normal file
41
hutool-core/src/main/java/cn/hutool/core/net/PassAuth.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
@ -1094,9 +1094,9 @@ public class HttpRequest extends HttpBase<HttpRequest> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用转发,如果需要转发返回转发结果,否则返回<code>null</code>
|
||||
* 调用转发,如果需要转发返回转发结果,否则返回{@code null}
|
||||
*
|
||||
* @return {@link HttpResponse},无转发返回 <code>null</code>
|
||||
* @return {@link HttpResponse},无转发返回 {@code null}
|
||||
*/
|
||||
private HttpResponse sendRedirectIfPossible() {
|
||||
if (this.maxRedirectCount < 1) {
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user