add UserPassAuthenticator

This commit is contained in:
Looly 2021-06-16 11:40:10 +08:00
parent 5dcc4eb57c
commit cdca396da3
3 changed files with 58 additions and 2 deletions

View File

@ -3,15 +3,16 @@
-------------------------------------------------------------------------------------------------------------
# 5.7.2 (2021-06-17)
# 5.7.2 (2021-06-16)
### 🐣新特性
* 【core 】 增加UserPassAuthenticator
### 🐞Bug修复
-------------------------------------------------------------------------------------------------------------
# 5.7.1 (2021-06-17)
# 5.7.1 (2021-06-16)
### 🐣新特性
* 【db 】 NamedSql支持in操作(issue#1652@Github)

View File

@ -11,6 +11,7 @@ import cn.hutool.core.util.StrUtil;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.net.Authenticator;
import java.net.DatagramSocket;
import java.net.HttpCookie;
import java.net.IDN;
@ -766,6 +767,27 @@ public class NetUtil {
return false;
}
}
/**
* 设置全局验证
*
* @param user 用户名
* @param pass 密码考虑安全此处不使用String
* @since 5.7.2
*/
public static void setGlobalAuthenticator(String user, char[] pass) {
setGlobalAuthenticator(new UserPassAuthenticator(user, pass));
}
/**
* 设置全局验证
*
* @param authenticator 验证器
* @since 5.7.2
*/
public static void setGlobalAuthenticator(Authenticator authenticator) {
Authenticator.setDefault(authenticator);
}
// ----------------------------------------------------------------------------------------- Private method start
/**

View File

@ -0,0 +1,33 @@
package cn.hutool.core.net;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
/**
* 账号密码形式的{@link Authenticator}
*
* @author looly
* @since 5.7.2
*/
public class UserPassAuthenticator extends Authenticator {
private final String user;
private final char[] pass;
/**
* 构造
*
* @param user 用户名
* @param pass 密码
*/
public UserPassAuthenticator(String user, char[] pass) {
this.user = user;
this.pass = pass;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(this.user, this.pass);
}
}