新增:新增修改邮箱功能,并优化部分以往代码(引入 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

@@ -59,6 +59,17 @@ limitations under the License.
</exclusions>
</dependency>
<!-- Java 邮件支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- FreeMarker模板引擎 -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<!-- Hibernate Validator -->
<dependency>
<groupId>org.springframework.boot</groupId>
@@ -126,5 +137,11 @@ limitations under the License.
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
</dependency>
<!-- Easy CaptchaJava 图形验证码,支持 gif、中文、算术等类型可用于 Java Web、JavaSE 等项目) -->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,173 @@
/*
* 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.config.properties;
import java.awt.*;
import lombok.Data;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import com.wf.captcha.*;
import com.wf.captcha.base.Captcha;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
/**
* 验证码配置属性
*
* @author Charles7c
* @since 2022/12/11 13:35
*/
@Data
@Component
@ConfigurationProperties(prefix = "captcha")
public class CaptchaProperties {
/**
* 图片验证码配置
*/
private CaptchaImage image;
/**
* 邮箱验证码配置
*/
private CaptchaMail mail;
/**
* 图片验证码配置
*/
@Data
public static class CaptchaImage {
/**
* 类型
*/
private CaptchaImageTypeEnum type;
/**
* 内容长度
*/
private int length;
/**
* 过期时间
*/
private long expirationInMinutes;
/**
* 宽度
*/
private int width = 111;
/**
* 高度
*/
private int height = 36;
/**
* 字体
*/
private String fontName;
/**
* 字体大小
*/
private int fontSize = 25;
/**
* 获取图片验证码对象
*
* @return 验证码对象
*/
public Captcha getCaptcha() {
Captcha captcha = ReflectUtil.newInstance(type.getClazz(), this.width, this.height);
captcha.setLen(length);
if (StrUtil.isNotBlank(this.fontName)) {
captcha.setFont(new Font(this.fontName, Font.PLAIN, this.fontSize));
}
return captcha;
}
}
/**
* 邮箱验证码配置
*/
@Data
public static class CaptchaMail {
/**
* 内容长度
*/
private int length;
/**
* 过期时间
*/
private long expirationInMinutes;
/**
* 限制时间
*/
private long limitInSeconds;
/**
* 模板路径
*/
private String templatePath;
}
/**
* 图片验证码类型枚举
*/
@Getter
@RequiredArgsConstructor
private enum CaptchaImageTypeEnum {
/**
* 算术
*/
ARITHMETIC(ArithmeticCaptcha.class),
/**
* 中文
*/
CHINESE(ChineseCaptcha.class),
/**
* 中文闪图
*/
CHINESE_GIF(ChineseGifCaptcha.class),
/**
* 闪图
*/
GIF(GifCaptcha.class),
/**
* 特殊类型
*/
SPEC(SpecCaptcha.class),;
/**
* 验证码字节码类型
*/
private final Class<? extends Captcha> clazz;
}
}

View File

@@ -33,4 +33,14 @@ public class CacheConstants {
*/
public static final String LOGIN_USER_CACHE_KEY = "LOGIN_USER";
/**
* 验证码缓存键
*/
public static final String CAPTCHA_CACHE_KEY = "CAPTCHA";
/**
* 限流缓存键
*/
public static final String LIMIT_CACHE_KEY = "LIMIT";
}

View File

@@ -162,8 +162,8 @@ public class GlobalExceptionHandler {
@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ExceptionHandler(NotLoginException.class)
public R handleNotLoginException(NotLoginException e, HttpServletRequest request) {
log.error("请求地址'{}',认证失败'{}',无法访问系统资源", request.getRequestURI(), e.getMessage());
return R.fail(HttpStatus.UNAUTHORIZED.value(), "认证失败,无法访问系统资源");
log.error("请求地址'{}',认证失败,无法访问系统资源", request.getRequestURI(), e);
return R.fail(HttpStatus.UNAUTHORIZED.value(), "登录状态已过期,请重新登录");
}
/**

View File

@@ -0,0 +1,50 @@
/*
* 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.model.vo;
import java.io.Serializable;
import lombok.Data;
import lombok.experimental.Accessors;
import io.swagger.v3.oas.annotations.media.Schema;
/**
* 验证码信息
*
* @author Charles7c
* @since 2022/12/11 13:55
*/
@Data
@Accessors(chain = true)
@Schema(description = "验证码信息")
public class CaptchaVO implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 验证码标识
*/
@Schema(description = "验证码标识")
private String uuid;
/**
* 验证码图片Base64编码带图片格式data:image/gif;base64
*/
@Schema(description = "验证码图片Base64编码带图片格式data:image/gif;base64")
private String img;
}

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);