mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add UserPassAuthenticator
This commit is contained in:
parent
5dcc4eb57c
commit
cdca396da3
@ -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)
|
||||
|
@ -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
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user