新增:新增修改邮箱功能,并优化部分以往代码(引入 spring-boot-starter-mail 用于发送邮件验证码)

This commit is contained in:
2023-01-14 01:05:39 +08:00
parent 73fadb8315
commit 8b82557883
45 changed files with 1318 additions and 280 deletions

View File

@@ -0,0 +1,244 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.common.util;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import top.charles7c.cnadmin.common.util.validate.CheckUtils;
/**
* 邮件工具类
*
* @author Charles7c
* @since 2023/1/12 23:25
*/
@Data
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class MailUtils {
private static final JavaMailSender MAIL_SENDER = SpringUtil.getBean(JavaMailSender.class);
/**
* 发送文本邮件给单个人
*
* @param subject
* 主题
* @param content
* 内容
* @param to
* 收件人
* @throws MessagingException
* /
*/
public static void sendText(String to, String subject, String content) throws MessagingException {
send(splitAddress(to), null, null, subject, content, false);
}
/**
* 发送 HTML 邮件给单个人
*
* @param subject
* 主题
* @param content
* 内容
* @param to
* 收件人
* @throws MessagingException
* /
*/
public static void sendHtml(String to, String subject, String content) throws MessagingException {
send(splitAddress(to), null, null, subject, content, true);
}
/**
* 发送 HTML 邮件给单个人
*
* @param subject
* 主题
* @param content
* 内容
* @param to
* 收件人
* @param files
* 附件列表
* @throws MessagingException
* /
*/
public static void sendHtml(String to, String subject, String content, File... files) throws MessagingException {
send(splitAddress(to), null, null, subject, content, true, files);
}
/**
* 发送 HTML 邮件给多个人
*
* @param subject
* 主题
* @param content
* 内容
* @param tos
* 收件人列表
* @param files
* 附件列表
* @throws MessagingException
* /
*/
public static void sendHtml(Collection<String> tos, String subject, String content, File... files)
throws MessagingException {
send(tos, null, null, subject, content, true, files);
}
/**
* 发送 HTML 邮件给多个人
*
* @param subject
* 主题
* @param content
* 内容
* @param tos
* 收件人列表
* @param ccs
* 抄送人列表
* @param files
* 附件列表
* @throws MessagingException
* /
*/
public static void sendHtml(Collection<String> tos, Collection<String> ccs, String subject, String content,
File... files) throws MessagingException {
send(tos, ccs, null, subject, content, true, files);
}
/**
* 发送 HTML 邮件给多个人
*
* @param subject
* 主题
* @param content
* 内容
* @param tos
* 收件人列表
* @param ccs
* 抄送人列表
* @param bccs
* 密送人列表
* @param files
* 附件列表
* @throws MessagingException
* /
*/
public static void sendHtml(Collection<String> tos, Collection<String> ccs, Collection<String> bccs, String subject,
String content, File... files) throws MessagingException {
send(tos, ccs, bccs, subject, content, true, files);
}
/**
* 发送邮件给多个人
*
* @param tos
* 收件人列表
* @param ccs
* 抄送人列表
* @param bccs
* 密送人列表
* @param subject
* 主题
* @param content
* 内容
* @param isHtml
* 是否是 HTML
* @param files
* 附件列表
* @throws MessagingException
* /
*/
public static void send(Collection<String> tos, Collection<String> ccs, Collection<String> bccs, String subject,
String content, boolean isHtml, File... files) throws MessagingException {
CheckUtils.exIfCondition(() -> CollUtil.isEmpty(tos), "请至少指定一名收件人");
MimeMessage mimeMessage = MAIL_SENDER.createMimeMessage();
MimeMessageHelper messageHelper =
new MimeMessageHelper(mimeMessage, true, StandardCharsets.UTF_8.displayName());
// 设置基本信息
messageHelper.setFrom(SpringUtil.getProperty("spring.mail.username"));
messageHelper.setSubject(subject);
messageHelper.setText(content, isHtml);
// 设置收信人
// 抄送人
if (CollUtil.isNotEmpty(ccs)) {
messageHelper.setCc(ccs.toArray(new String[0]));
}
// 密送人
if (CollUtil.isNotEmpty(bccs)) {
messageHelper.setBcc(bccs.toArray(new String[0]));
}
// 收件人
messageHelper.setTo(tos.toArray(new String[0]));
// 设置附件
if (ArrayUtil.isNotEmpty(files)) {
for (File file : files) {
messageHelper.addAttachment(file.getName(), file);
}
}
// 发送邮件
MAIL_SENDER.send(mimeMessage);
}
/**
* 将多个联系人转为列表,分隔符为逗号或者分号
*
* @param addresses
* 多个联系人如果为空返回null
* @return 联系人列表
*/
private static List<String> splitAddress(String addresses) {
if (StrUtil.isBlank(addresses)) {
return null;
}
List<String> result;
if (StrUtil.contains(addresses, CharUtil.COMMA)) {
result = StrUtil.splitTrim(addresses, CharUtil.COMMA);
} else if (StrUtil.contains(addresses, ';')) {
result = StrUtil.splitTrim(addresses, ';');
} else {
result = CollUtil.newArrayList(addresses);
}
return result;
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.common.util;
import java.util.Map;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.TemplateConfig;
import cn.hutool.extra.template.TemplateEngine;
import cn.hutool.extra.template.TemplateUtil;
/**
* 模板工具类
*
* @author Charles7c
* @since 2023/1/13 20:37
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class TemplateUtils {
private static final String TEMPLATE_PARENT_PATH = "templates";
/**
* 将模板与绑定参数融合后返回为字符串
*
* @param bindingMap
* 绑定的参数此Map中的参数会替换模板中的变量
* @return 融合后的内容
*/
public static String render(String templatePath, Map<?, ?> bindingMap) {
TemplateEngine engine =
TemplateUtil.createEngine(new TemplateConfig(TEMPLATE_PARENT_PATH, TemplateConfig.ResourceMode.CLASSPATH));
Template template = engine.getTemplate(templatePath);
return template.render(bindingMap);
}
}

View File

@@ -35,18 +35,6 @@ public class CheckUtils extends Validator {
private static final Class<ServiceException> EXCEPTION_TYPE = ServiceException.class;
/**
* 如果为空,抛出异常
*
* @param obj
* 被检测的对象
* @param message
* 错误信息
*/
public static void exIfNull(Object obj, String message) {
exIfNull(obj, message, EXCEPTION_TYPE);
}
/**
* 如果为空,抛出异常
*
@@ -59,6 +47,18 @@ public class CheckUtils extends Validator {
exIfBlank(str, message, EXCEPTION_TYPE);
}
/**
* 如果不为空,抛出异常
*
* @param str
* 被检测的字符串
* @param message
* 错误信息
*/
public static void exIfNotBlank(CharSequence str, String message) {
exIfNotBlank(str, message, EXCEPTION_TYPE);
}
/**
* 如果相同,抛出异常
*
@@ -87,6 +87,58 @@ public class CheckUtils extends Validator {
exIfNotEqual(obj1, obj2, message, EXCEPTION_TYPE);
}
/**
* 如果相同,抛出异常(不区分大小写)
*
* @param str1
* 要比较的字符串1
* @param str2
* 要比较的字符串2
* @param message
* 错误信息
*/
public static void exIfEqualIgnoreCase(CharSequence str1, CharSequence str2, String message) {
exIfEqualIgnoreCase(str1, str2, message, EXCEPTION_TYPE);
}
/**
* 如果不相同,抛出异常(不区分大小写)
*
* @param str1
* 要比较的字符串1
* @param str2
* 要比较的字符串2
* @param message
* 错误信息
*/
public static void exIfNotEqualIgnoreCase(CharSequence str1, CharSequence str2, String message) {
exIfNotEqualIgnoreCase(str1, str2, message, EXCEPTION_TYPE);
}
/**
* 如果为空,抛出异常
*
* @param obj
* 被检测的对象
* @param message
* 错误信息
*/
public static void exIfNull(Object obj, String message) {
exIfNull(obj, message, EXCEPTION_TYPE);
}
/**
* 如果不为空,抛出异常
*
* @param obj
* 被检测的对象
* @param message
* 错误信息
*/
public static void exIfNotNull(Object obj, String message) {
exIfNotNull(obj, message, EXCEPTION_TYPE);
}
/**
* 如果条件成立,抛出异常
*

View File

@@ -35,18 +35,6 @@ public class ValidationUtils extends Validator {
private static final Class<BadRequestException> EXCEPTION_TYPE = BadRequestException.class;
/**
* 如果为空,抛出异常
*
* @param obj
* 被检测的对象
* @param message
* 错误信息
*/
public static void exIfNull(Object obj, String message) {
exIfNull(obj, message, EXCEPTION_TYPE);
}
/**
* 如果为空,抛出异常
*
@@ -59,6 +47,18 @@ public class ValidationUtils extends Validator {
exIfBlank(str, message, EXCEPTION_TYPE);
}
/**
* 如果不为空,抛出异常
*
* @param str
* 被检测的字符串
* @param message
* 错误信息
*/
public static void exIfNotBlank(CharSequence str, String message) {
exIfNotBlank(str, message, EXCEPTION_TYPE);
}
/**
* 如果相同,抛出异常
*
@@ -87,6 +87,58 @@ public class ValidationUtils extends Validator {
exIfNotEqual(obj1, obj2, message, EXCEPTION_TYPE);
}
/**
* 如果相同,抛出异常(不区分大小写)
*
* @param str1
* 要比较的字符串1
* @param str2
* 要比较的字符串2
* @param message
* 错误信息
*/
public static void exIfEqualIgnoreCase(CharSequence str1, CharSequence str2, String message) {
exIfEqualIgnoreCase(str1, str2, message, EXCEPTION_TYPE);
}
/**
* 如果不相同,抛出异常(不区分大小写)
*
* @param str1
* 要比较的字符串1
* @param str2
* 要比较的字符串2
* @param message
* 错误信息
*/
public static void exIfNotEqualIgnoreCase(CharSequence str1, CharSequence str2, String message) {
exIfNotEqualIgnoreCase(str1, str2, message, EXCEPTION_TYPE);
}
/**
* 如果为空,抛出异常
*
* @param obj
* 被检测的对象
* @param message
* 错误信息
*/
public static void exIfNull(Object obj, String message) {
exIfNull(obj, message, EXCEPTION_TYPE);
}
/**
* 如果不为空,抛出异常
*
* @param obj
* 被检测的对象
* @param message
* 错误信息
*/
public static void exIfNotNull(Object obj, String message) {
exIfNotNull(obj, message, EXCEPTION_TYPE);
}
/**
* 如果条件成立,抛出异常
*

View File

@@ -25,6 +25,8 @@ import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
/**
* 校验器
*
* @author Charles7c
* @since 2023/1/2 22:12
*/
@@ -32,23 +34,6 @@ import cn.hutool.core.util.StrUtil;
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Validator {
/**
* 如果为空,抛出异常
*
* @param obj
* 被检测的对象
* @param message
* 错误信息
* @param exceptionType
* 异常类型
*/
protected static void exIfNull(Object obj, String message, Class<? extends RuntimeException> exceptionType) {
if (obj == null) {
log.error(message);
throw ReflectUtil.newInstance(exceptionType, message);
}
}
/**
* 如果为空,抛出异常
*
@@ -59,11 +44,23 @@ public class Validator {
* @param exceptionType
* 异常类型
*/
public static void exIfBlank(CharSequence str, String message, Class<? extends RuntimeException> exceptionType) {
if (StrUtil.isBlank(str)) {
log.error(message);
throw ReflectUtil.newInstance(exceptionType, message);
}
protected static void exIfBlank(CharSequence str, String message, Class<? extends RuntimeException> exceptionType) {
exIfCondition(() -> StrUtil.isBlank(str), message, exceptionType);
}
/**
* 如果不为空,抛出异常
*
* @param str
* 被检测的字符串
* @param message
* 错误信息
* @param exceptionType
* 异常类型
*/
protected static void exIfNotBlank(CharSequence str, String message,
Class<? extends RuntimeException> exceptionType) {
exIfCondition(() -> StrUtil.isNotBlank(str), message, exceptionType);
}
/**
@@ -78,12 +75,9 @@ public class Validator {
* @param exceptionType
* 异常类型
*/
public static void exIfEqual(Object obj1, Object obj2, String message,
protected static void exIfEqual(Object obj1, Object obj2, String message,
Class<? extends RuntimeException> exceptionType) {
if (ObjectUtil.equals(obj1, obj2)) {
log.error(message);
throw ReflectUtil.newInstance(exceptionType, message);
}
exIfCondition(() -> ObjectUtil.equal(obj1, obj2), message, exceptionType);
}
/**
@@ -98,12 +92,71 @@ public class Validator {
* @param exceptionType
* 异常类型
*/
public static void exIfNotEqual(Object obj1, Object obj2, String message,
protected static void exIfNotEqual(Object obj1, Object obj2, String message,
Class<? extends RuntimeException> exceptionType) {
if (ObjectUtil.notEqual(obj1, obj2)) {
log.error(message);
throw ReflectUtil.newInstance(exceptionType, message);
}
exIfCondition(() -> ObjectUtil.notEqual(obj1, obj2), message, exceptionType);
}
/**
* 如果相同,抛出异常(不区分大小写)
*
* @param str1
* 要比较的字符串1
* @param str2
* 要比较的字符串2
* @param message
* 错误信息
* @param exceptionType
* 异常类型
*/
protected static void exIfEqualIgnoreCase(CharSequence str1, CharSequence str2, String message,
Class<? extends RuntimeException> exceptionType) {
exIfCondition(() -> StrUtil.equalsIgnoreCase(str1, str2), message, exceptionType);
}
/**
* 如果不相同,抛出异常(不区分大小写)
*
* @param str1
* 要比较的字符串1
* @param str2
* 要比较的字符串2
* @param message
* 错误信息
* @param exceptionType
* 异常类型
*/
protected static void exIfNotEqualIgnoreCase(CharSequence str1, CharSequence str2, String message,
Class<? extends RuntimeException> exceptionType) {
exIfCondition(() -> !StrUtil.equalsIgnoreCase(str1, str2), message, exceptionType);
}
/**
* 如果为空,抛出异常
*
* @param obj
* 被检测的对象
* @param message
* 错误信息
* @param exceptionType
* 异常类型
*/
protected static void exIfNull(Object obj, String message, Class<? extends RuntimeException> exceptionType) {
exIfCondition(() -> obj == null, message, exceptionType);
}
/**
* 如果不为空,抛出异常
*
* @param obj
* 被检测的对象
* @param message
* 错误信息
* @param exceptionType
* 异常类型
*/
protected static void exIfNotNull(Object obj, String message, Class<? extends RuntimeException> exceptionType) {
exIfCondition(() -> obj != null, message, exceptionType);
}
/**
@@ -116,7 +169,7 @@ public class Validator {
* @param exceptionType
* 异常类型
*/
public static void exIfCondition(java.util.function.BooleanSupplier conditionSupplier, String message,
protected static void exIfCondition(java.util.function.BooleanSupplier conditionSupplier, String message,
Class<? extends RuntimeException> exceptionType) {
if (conditionSupplier != null && conditionSupplier.getAsBoolean()) {
log.error(message);