This commit is contained in:
Looly 2023-04-17 13:48:14 +08:00
parent d2fd9d448d
commit fecc2ef0e0
8 changed files with 122 additions and 57 deletions

View File

@ -12,6 +12,7 @@
package org.dromara.hutool.extra.mail;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.util.CharsetUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.core.text.StrUtil;
@ -75,9 +76,11 @@ public class MailAccount implements Serializable {
*/
private String user;
/**
* 密码
* 密码<br>
* 使用char[]保存密码有利于及时擦除<br>
* https://www.cnblogs.com/okokabcd/p/16456966.html
*/
private String pass;
private char[] pass;
/**
* 发送方遵循RFC-822标准
*/
@ -260,7 +263,7 @@ public class MailAccount implements Serializable {
*
* @return 密码
*/
public String getPass() {
public char[] getPass() {
return pass;
}
@ -270,7 +273,7 @@ public class MailAccount implements Serializable {
* @param pass 密码
* @return this
*/
public MailAccount setPass(final String pass) {
public MailAccount setPass(final char[] pass) {
this.pass = pass;
return this;
}
@ -649,7 +652,7 @@ public class MailAccount implements Serializable {
}
if (null == this.auth) {
// 如果密码非空白则使用认证模式
this.auth = (!StrUtil.isBlank(this.pass));
this.auth = ArrayUtil.isNotEmpty(this.pass);
}
if (null == this.port) {
// 端口在SSL状态下默认与socketFactoryPort一致非SSL状态下默认为25
@ -665,7 +668,7 @@ public class MailAccount implements Serializable {
@Override
public String toString() {
return "MailAccount [host=" + host + ", port=" + port + ", auth=" + auth + ", user=" + user + ", pass=" + (StrUtil.isEmpty(this.pass) ? "" : "******") + ", from=" + from + ", startttlsEnable="
return "MailAccount [host=" + host + ", port=" + port + ", auth=" + auth + ", user=" + user + ", pass=" + (ArrayUtil.isEmpty(this.pass) ? "" : "******") + ", from=" + from + ", startttlsEnable="
+ starttlsEnable + ", socketFactoryClass=" + socketFactoryClass + ", socketFactoryFallback=" + socketFactoryFallback + ", socketFactoryPort=" + socketFactoryPort + "]";
}
}

View File

@ -12,16 +12,17 @@
package org.dromara.hutool.extra.mail;
import jakarta.mail.Authenticator;
import jakarta.mail.PasswordAuthentication;
import jakarta.mail.Session;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.text.split.SplitUtil;
import org.dromara.hutool.core.util.CharUtil;
import org.dromara.hutool.core.text.StrUtil;
import jakarta.mail.Authenticator;
import jakarta.mail.Session;
import java.io.File;
import java.io.InputStream;
import java.util.Collection;
@ -374,7 +375,15 @@ public class MailUtil {
public static Session getSession(final MailAccount mailAccount, final boolean isSingleton) {
Authenticator authenticator = null;
if (mailAccount.isAuth()) {
authenticator = new UserPassAuthenticator(mailAccount.getUser(), mailAccount.getPass());
authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
mailAccount.getUser(),
String.valueOf(mailAccount.getPass())
);
}
};
}
return isSingleton ? Session.getDefaultInstance(mailAccount.getSmtpProps(), authenticator) //

View File

@ -1,45 +0,0 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.extra.mail;
import jakarta.mail.Authenticator;
import jakarta.mail.PasswordAuthentication;
/**
* 用户名密码验证器
*
* @author looly
* @since 3.1.2
*/
public class UserPassAuthenticator extends Authenticator {
private final String user;
private final String pass;
/**
* 构造
*
* @param user 用户名
* @param pass 密码
*/
public UserPassAuthenticator(final String user, final String pass) {
this.user = user;
this.pass = pass;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(this.user, this.pass);
}
}

View File

@ -19,6 +19,7 @@ public class MailAccountTest {
final MailAccount account = GlobalMailAccount.INSTANCE.getAccount();
account.getSmtpProps();
Assertions.assertNotNull(account.getPass());
Assertions.assertNotNull(account.getCharset());
Assertions.assertTrue(account.isSslEnable());
}
@ -35,7 +36,7 @@ public class MailAccountTest {
public void customPropertyTest() throws GeneralSecurityException {
final MailAccount mailAccount = new MailAccount();
mailAccount.setFrom("xxx@xxx.com");
mailAccount.setPass("xxxxxx");
mailAccount.setPass("xxxxxx".toCharArray());
mailAccount.setHost("smtp.aol.com");

View File

@ -53,7 +53,7 @@ public class MailTest {
account.setSslEnable(true);
account.setFrom("hutool@yeah.net");
account.setUser("hutool");
account.setPass("q1w2e3");
account.setPass("q1w2e3".toCharArray());
MailUtil.send(account, "hutool@foxmail.com", "测试", "<h1>邮件来自Hutool测试</h1>", true);
}

View File

@ -0,0 +1,19 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.http.auth;
/**
*
*/
public class Credential {
}

View File

@ -0,0 +1,19 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/**
* 提供HTTP认证相关封装
*
* @author looly
* @since 6.0.0
*/
package org.dromara.hutool.http.auth;

View File

@ -0,0 +1,59 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.http.proxy;
import java.net.InetSocketAddress;
import java.net.Proxy;
/**
* HTTP代理提供代理地址和代理端口的持有
*
* @author looly
* @since 6.0.0
*/
public class HttpProxy extends Proxy {
private final String host;
private final int port;
/**
* 构造
*
* @param host 域名或IP
* @param port 端口
*/
public HttpProxy(final String host, final int port) {
super(Type.HTTP, new InetSocketAddress(host, port));
this.host = host;
this.port = port;
}
/**
* 获取域名或IP
*
* @return 域名或IP
*/
public String getHost() {
return host;
}
/**
* 获取端口
*
* @return 端口
*/
public int getPort() {
return port;
}
}