feat(新增): 拆分各个模块,封装邮件发送

This commit is contained in:
bunny 2024-05-09 16:42:44 +08:00
parent f6c3ee10f2
commit c1f4b9b40a
28 changed files with 214 additions and 62 deletions

View File

@ -28,10 +28,6 @@
<artifactId>jaxb-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- 数据库代码生成器 - 新版 -->
<dependency>
<groupId>com.baomidou</groupId>

View File

@ -35,4 +35,8 @@ public class MessageConstant {
public static final String LOGIN_DTO_IS_EMPTY = "登录参数不能为空";
public static final String TOKEN_IS_EMPTY = "token为空";
public static final String DATA_IS_EMPTY = "数据为空";
public static final String EMPTY_SEND_OBJECT = "空发送对象";
public static final String ADDRESS_NOT_NULL = "收件人不能为空";
public static final String TITLE_NOT_NULL = "标题不能为空";
public static final String SEND_MESSAGE_NOT_NULL = "发送消息不能为空";
}

View File

@ -8,8 +8,6 @@ import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "snowflake")
@Data
public class SnowflakeProperties {
// 数据中心id
private Long datacenterId;
// 数据中心id位数

View File

@ -22,11 +22,6 @@
<artifactId>common-utils</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- minio -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -1,7 +1,6 @@
package cn.bunny.common.service.config;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
@ -11,7 +10,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Slf4j
public class WebMvcConfiguration implements WebMvcConfigurer {
@Override
public void addResourceHandlers(@NotNull ResourceHandlerRegistry registry) {
public void addResourceHandlers(ResourceHandlerRegistry registry) {
log.info("WebMvcConfiguration===>设置");
registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/");

View File

@ -0,0 +1,12 @@
package cn.bunny.common.service.utils;
import cn.bunny.common.service.exception.BunnyException;
import org.springframework.util.StringUtils;
public class EmptyUtil {
public static void isEmpty(Object value, String message) {
if (value == null || !StringUtils.hasText(value.toString())) {
throw new BunnyException(message);
}
}
}

View File

@ -1,4 +1,4 @@
package cn.bunny.common.utils;
package cn.bunny.common.service.utils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
@ -29,7 +29,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpUtils {
public class HttpUtil {
public static HttpResponse doGet(String host, String path, String method, Map<String, String> headers, Map<String, String> querys) throws Exception {
HttpClient httpClient = wrapClient(host);

View File

@ -1,4 +1,4 @@
package cn.bunny.common.utils;
package cn.bunny.common.service.utils;
import io.jsonwebtoken.*;
import org.springframework.util.StringUtils;

View File

@ -1,4 +1,4 @@
package cn.bunny.common.utils;
package cn.bunny.common.service.utils;
import cn.bunny.common.result.Result;
import com.fasterxml.jackson.databind.ObjectMapper;

View File

@ -1,4 +1,4 @@
package cn.bunny.common.utils;
package cn.bunny.common.service.utils;
import cn.bunny.common.properties.SnowflakeProperties;

View File

@ -1,13 +1,12 @@
package cn.bunny.security.filter;
import cn.bunny.common.service.context.BaseContext;
import cn.bunny.common.utils.JwtHelper;
import cn.bunny.common.service.utils.JwtHelper;
import com.alibaba.fastjson2.JSON;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
@ -28,7 +27,7 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter {
}
@Override
protected void doFilterInternal(HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull FilterChain chain) throws ServletException, IOException {
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
String token = request.getHeader("token");
// login请求就没token直接放行因为后边有其他的过滤器
@ -46,9 +45,9 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter {
private UsernamePasswordAuthenticationToken getAuthentication(HttpServletRequest request) {
// 请求头是否有token
String token = request.getHeader("token");
if (!StringUtils.isEmpty(token)) {
if (StringUtils.hasText(token)) {
String username = JwtHelper.getUserName(token);
if (!StringUtils.isEmpty(username)) {
if (StringUtils.hasText(username)) {
// 当前用户信息放到ThreadLocal里面
BaseContext.setUserId(JwtHelper.getUserId(token));
BaseContext.setUsername(username);
@ -56,7 +55,7 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter {
// 通过username从redis获取权限数据
String authString = (String) redisTemplate.opsForValue().get(username);
// 把redis获取字符串权限数据转换要求集合类型 List<SimpleGrantedAuthority>
if (!StringUtils.isEmpty(authString)) {
if (StringUtils.hasText(authString)) {
List<Map> maplist = JSON.parseArray(authString, Map.class);
System.out.println(maplist);
List<SimpleGrantedAuthority> authList = new ArrayList<>();

View File

@ -1,8 +1,8 @@
package cn.bunny.security.filter;
import cn.bunny.common.result.Result;
import cn.bunny.common.utils.JwtHelper;
import cn.bunny.common.utils.ResponseUtil;
import cn.bunny.common.service.utils.JwtHelper;
import cn.bunny.common.service.utils.ResponseUtil;
import cn.bunny.entity.system.Login;
import cn.bunny.enums.ResultCodeEnum;
import cn.bunny.security.custom.CustomUser;

View File

@ -1,5 +1,6 @@
package cn.bunny.entity.email;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
@ -12,8 +13,10 @@ import org.springframework.web.multipart.MultipartFile;
@NoArgsConstructor
public class EmailSend {
// 给谁发送
@NotNull(message = "发送者不能为空")
private String sendTo;
// 发送主题
@NotNull(message = "主题不能为空")
private String subject;
// 是否为富文本
private Boolean isRichText;

View File

@ -0,0 +1,31 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.bunny</groupId>
<artifactId>module</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>module-mail</artifactId>
<packaging>jar</packaging>
<name>module-mail</name>
<url>https://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>cn.bunny</groupId>
<artifactId>service-utils</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,23 @@
Configuration example
mail:
host: smtp.qq.com # 邮箱地址
port: 465 # 邮箱端口号
username: xxx@qq.com # 设置发送邮箱
password: xx # 如果是纯数字要加引号
default-encoding: UTF-8 # 设置编码格式
protocol: smtps
properties:
mail:
debug: true # 是否开启debug模式发送邮件
smtp:
auth: true
connectionTimeout: 5000 # 设置连接延迟
timeout: 5000 # 延迟时间
writeTimeout: 5000 # 写入邮箱延迟
allow8BitMime: true
sendPartial: true
ssl:
enabled: true # 是否开启SSL连接
socketFactory:
class: javax.net.ssl.SSLSocketFactory # 必要设置!!!

View File

@ -0,0 +1,18 @@
package cn.bunny.module.mail.utils;
import cn.bunny.common.constant.MessageConstant;
import cn.bunny.common.service.utils.EmptyUtil;
import cn.bunny.entity.email.EmailSend;
public class MailSendCheck {
public static void check(EmailSend emailSend) {
// 空发送对象
EmptyUtil.isEmpty(emailSend, MessageConstant.EMPTY_SEND_OBJECT);
// 收件人不能为空
EmptyUtil.isEmpty(emailSend.getSendTo(), MessageConstant.ADDRESS_NOT_NULL);
// 标题不能为空
EmptyUtil.isEmpty(emailSend.getSubject(), MessageConstant.TITLE_NOT_NULL);
// 发送消息不能为空
EmptyUtil.isEmpty(emailSend.getMessage(), MessageConstant.SEND_MESSAGE_NOT_NULL);
}
}

View File

@ -1,4 +1,4 @@
package cn.bunny.common.utils;
package cn.bunny.module.mail.utils;
import cn.bunny.entity.email.EmailSend;
import cn.bunny.entity.email.EmailSendInit;
@ -37,6 +37,7 @@ public class MailSenderHelper {
* @param emailSend 邮件消息
*/
public void sendSimpleEmail(EmailSend emailSend) {
MailSendCheck.check(emailSend);
// 创建邮件消息体 SimpleMailMessage 发送简单邮件
SimpleMailMessage mailMessage = new SimpleMailMessage();
@ -58,6 +59,7 @@ public class MailSenderHelper {
* @param emailSend 邮件消息
*/
public void sendAttachmentEmail(EmailSend emailSend, MultipartFile file, boolean isRich) throws MessagingException {
MailSendCheck.check(emailSend);
// 创建 MimeMessage 对象用户发送附件或者是富文本内容
MimeMessage mailMessage = javaMailSender.createMimeMessage();
// 创建 MimeMessageHelper
@ -84,6 +86,7 @@ public class MailSenderHelper {
* @param emailSend 邮件消息
*/
public void sendRichText(EmailSend emailSend, boolean isRich) throws MessagingException {
MailSendCheck.check(emailSend);
// 创建 MimeMessage 对象用户发送附件或者是富文本内容
MimeMessage mailMessage = javaMailSender.createMimeMessage();
// 创建 MimeMessageHelper
@ -108,6 +111,7 @@ public class MailSenderHelper {
* @param emailSend 邮件消息
*/
public void sendCC(EmailSend emailSend, boolean isRich) throws MessagingException {
MailSendCheck.check(emailSend);
// 创建 MimeMessage 对象用于发送邮件富文本或者附件
MimeMessage message = javaMailSender.createMimeMessage();
// 创建 MimeMessageHelper
@ -134,6 +138,8 @@ public class MailSenderHelper {
* @param emailSend 邮件消息
*/
public void sendEmail(EmailSend emailSend) throws MessagingException {
MailSendCheck.check(emailSend);
// 创建 MimeMessage 对象用于发送邮件富文本或者附件
MimeMessage message = javaMailSender.createMimeMessage();
// 创建 MimeMessageHelper

View File

@ -0,0 +1,27 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.bunny</groupId>
<artifactId>module</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>module-minio</artifactId>
<packaging>jar</packaging>
<name>module-minio</name>
<url>https://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- minio -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -1,4 +1,4 @@
package cn.bunny.common.service.properties;
package cn.bunny.module.minio.properties;
import io.minio.MinioClient;
import lombok.Data;

View File

@ -0,0 +1,30 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.bunny</groupId>
<artifactId>module</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>module-rabbitMQ</artifactId>
<packaging>jar</packaging>
<name>module-rabbitMQ</name>
<url>https://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
</dependency>
</dependencies>
</project>

32
module/pom.xml Normal file
View File

@ -0,0 +1,32 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.bunny</groupId>
<artifactId>bunny-template</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>module</artifactId>
<packaging>pom</packaging>
<name>module</name>
<url>https://maven.apache.org</url>
<modules>
<module>module-minio</module>
<module>module-mail</module>
<module>module-rabbitMQ</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>cn.bunny</groupId>
<artifactId>model</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

View File

@ -19,6 +19,7 @@
<module>common</module>
<module>model</module>
<module>service</module>
<module>module</module>
</modules>
<properties>

View File

@ -25,6 +25,11 @@
<artifactId>spring-security</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.bunny</groupId>
<artifactId>module-mail</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- service-utils -->
<dependency>
<groupId>cn.bunny</groupId>

View File

@ -3,7 +3,6 @@ package cn.bunny.service.controller;
import cn.bunny.common.result.Result;
import cn.bunny.entity.email.EmailSend;
import cn.bunny.service.service.EmailService;
import com.alibaba.fastjson2.JSON;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
@ -12,7 +11,6 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
@Tag(name = "发送邮件")
@ -32,10 +30,9 @@ public class MailController {
@Operation(summary = "发送带附件邮件", description = "发送带附件邮件")
@PostMapping("send-attachment")
public Result<String> sendAttachment(@RequestBody MultipartFile file, String emailSend) {
public Result<String> sendAttachment(@RequestBody EmailSend emailSend) {
log.info("发送带附件邮件");
EmailSend send = JSON.parseObject(emailSend, EmailSend.class);
boolean isSuccess = emailService.sendAttachmentEmail(send, file);
boolean isSuccess = emailService.sendAttachmentEmail(emailSend);
return isSuccess ? Result.success() : Result.error();
}

View File

@ -2,7 +2,6 @@ package cn.bunny.service.service;
import cn.bunny.entity.email.EmailSend;
import org.springframework.web.multipart.MultipartFile;
public interface EmailService {
/**
@ -18,7 +17,7 @@ public interface EmailService {
* @param emailSend 邮件消息
* @return 是否成功
*/
boolean sendAttachmentEmail(EmailSend emailSend, MultipartFile file);
boolean sendAttachmentEmail(EmailSend emailSend);
/**
* 发送富文本邮件

View File

@ -1,13 +1,12 @@
package cn.bunny.service.service.impl;
import cn.bunny.common.utils.MailSenderHelper;
import cn.bunny.entity.email.EmailSend;
import cn.bunny.entity.email.EmailSendInit;
import cn.bunny.module.mail.utils.MailSenderHelper;
import cn.bunny.service.service.EmailService;
import jakarta.mail.MessagingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@Service
@Slf4j
@ -36,7 +35,7 @@ public class EmailServiceImpl implements EmailService {
* @return 是否成功
*/
@Override
public boolean sendAttachmentEmail(EmailSend emailSend, MultipartFile file) {
public boolean sendAttachmentEmail(EmailSend emailSend) {
try {
EmailSendInit emailSendInit = new EmailSendInit();
emailSendInit.setHost("smtp.qq.com");
@ -45,7 +44,7 @@ public class EmailServiceImpl implements EmailService {
emailSendInit.setPassword("axyqbvfuxkdqdaai");
MailSenderHelper mailSenderHelper = new MailSenderHelper(emailSendInit);
mailSenderHelper.sendAttachmentEmail(emailSend, file, true);
mailSenderHelper.sendEmail(emailSend);
return true;
} catch (MessagingException e) {
return false;

View File

@ -2,7 +2,7 @@ package cn.bunny.service.service.impl;
import cn.bunny.common.constant.MessageConstant;
import cn.bunny.common.service.exception.BunnyException;
import cn.bunny.common.utils.JwtHelper;
import cn.bunny.common.service.utils.JwtHelper;
import cn.bunny.entity.system.Login;
import cn.bunny.entity.system.SysUser;
import cn.bunny.entity.system.SysUserinfo;

View File

@ -35,28 +35,6 @@ spring:
name: user
password: 1
mail:
host: smtp.qq.com # 邮箱地址
port: 465 # 邮箱端口号
username: 3324855376@qq.com # 设置发送邮箱
password: axyqbvfuxkdqdaai # 如果是纯数字要加引号
default-encoding: UTF-8 # 设置编码格式
protocol: smtps
properties:
mail:
debug: true # 是否开启debug模式发送邮件
smtp:
auth: true
connectionTimeout: 5000 # 设置连接延迟
timeout: 5000 # 延迟时间
writeTimeout: 5000 # 写入邮箱延迟
allow8BitMime: true
sendPartial: true
ssl:
enabled: true # 是否开启SSL连接
socketFactory:
class: javax.net.ssl.SSLSocketFactory # 必要设置!!!
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
configuration: