From e2fd1701343a6d7a053c2400e6e8fa8b003cb0a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=8C=E7=8B=BC=E8=93=9D=E5=A4=A9?= Date: Wed, 9 Oct 2024 11:05:05 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E5=B7=A5=E5=8D=95=20#?= =?UTF-8?q?11?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../guaiguailang/harmony/mapper/AuthMapper.java | 2 ++ .../harmony/service/impl/UserServiceImpl.java | 5 +++-- src/main/resources/mapper/UserMapper.xml | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/guaiguailang/harmony/mapper/AuthMapper.java b/src/main/java/com/guaiguailang/harmony/mapper/AuthMapper.java index c71b01d..e985e9b 100644 --- a/src/main/java/com/guaiguailang/harmony/mapper/AuthMapper.java +++ b/src/main/java/com/guaiguailang/harmony/mapper/AuthMapper.java @@ -39,4 +39,6 @@ public interface AuthMapper { List getUserPermissionListByRoleId(String roleId); List getActionByPermissionIdAndUid(String permissionId, Long uid); + @Select("SELECT system_role.level from system_role where role_id=#{roleId}") + int getRoleLevelByRole(String roleId); } diff --git a/src/main/java/com/guaiguailang/harmony/service/impl/UserServiceImpl.java b/src/main/java/com/guaiguailang/harmony/service/impl/UserServiceImpl.java index 125bcd7..a67cb29 100644 --- a/src/main/java/com/guaiguailang/harmony/service/impl/UserServiceImpl.java +++ b/src/main/java/com/guaiguailang/harmony/service/impl/UserServiceImpl.java @@ -95,17 +95,18 @@ public class UserServiceImpl implements UserService { // 如果是root,admin 则加载全部数据,如果是agent,则加载对应的用户 UserInfo userInfo = userMapper.getUserById(uid); String merchantCode= Objects.equals(userInfo.getRoleId(), "root") || Objects.equals(userInfo.getRoleId(), "admin") ?"ALL":userInfo.getMerchantCode(); + int currentLevel = authMapper.getRoleLevelByRole(userInfo.getRoleId()); // 计算查询limit int limit = (userListParam.getCurrentPage()-1) * userListParam.getPageSize(); int end = limit + userListParam.getPageSize(); // 创建一个查询条件来排除当前用户 Map queryConditions = new HashMap<>(); queryConditions.put("excludeUid", uid); - + queryConditions.put("currentLevel", currentLevel); // 添加当前用户的等级作为查询条件 List userInfos; log.info("要查询的用户类型是:"+userListParam.getUserType()); if (Objects.equals(userListParam.getUserType(), "activate")) { - // 如果有额外的激活状态条件,也可以加入到queryConditions中,比如限制不能查询身份等级比自己高的 | todo 这个任务涉及到新增字段 | 已经新增字段level + // 如果有额外的激活状态条件,也可以加入到queryConditions中,比如限制不能查询身份等级比自己高的 | todo 这个任务涉及到新增字段 | 已经新增字段level。level数值越小,则身份权限越高,后面不管怎么查,都只能查询level值>当前用户level值的 userInfos = userMapper.getUserListActivate(limit, end, merchantCode, queryConditions); } else if (Objects.equals(userListParam.getUserType(), "disabled")) { // 同上,可以加入禁用状态条件 diff --git a/src/main/resources/mapper/UserMapper.xml b/src/main/resources/mapper/UserMapper.xml index f4c88e6..8b7a2ab 100644 --- a/src/main/resources/mapper/UserMapper.xml +++ b/src/main/resources/mapper/UserMapper.xml @@ -14,6 +14,11 @@ AND id != #{queryConditions.excludeUid} + + AND role_id IN ( + SELECT role_id FROM system_role WHERE level >= #{queryConditions.currentLevel} + ) + LIMIT #{limit},#{end} @@ -27,6 +32,11 @@ AND id != #{queryConditions.excludeUid} + + AND role_id IN ( + SELECT role_id FROM system_role WHERE level >= #{queryConditions.currentLevel} + ) + LIMIT #{limit},#{end} @@ -40,6 +50,11 @@ AND id != #{queryConditions.excludeUid} + + AND role_id IN ( + SELECT role_id FROM system_role WHERE level >= #{queryConditions.currentLevel} + ) + LIMIT #{limit},#{end} From ab1419f594df34dcfe6c7d684b8acbf26a086e3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=8C=E7=8B=BC=E8=93=9D=E5=A4=A9?= Date: Wed, 9 Oct 2024 16:03:52 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E9=82=AE=E4=BB=B6?= =?UTF-8?q?=E5=8F=91=E9=80=81=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 16 ++++++ .../harmony/config/MailConfig.java | 55 +++++++++++++++++++ .../harmony/service/EmailService.java | 5 ++ .../service/impl/EmailServiceImpl.java | 38 +++++++++++++ .../guaiguailang/harmony/utils/KeyBase64.java | 15 +++++ .../harmony/utils/UtilEmailSent.java | 41 ++++++++++++++ src/main/resources/application.properties | 41 ++++++++++++++ .../HarmonyLifeServerApplicationTests.java | 12 ++-- 8 files changed, 218 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/guaiguailang/harmony/config/MailConfig.java create mode 100644 src/main/java/com/guaiguailang/harmony/service/EmailService.java create mode 100644 src/main/java/com/guaiguailang/harmony/service/impl/EmailServiceImpl.java create mode 100644 src/main/java/com/guaiguailang/harmony/utils/KeyBase64.java create mode 100644 src/main/java/com/guaiguailang/harmony/utils/UtilEmailSent.java diff --git a/pom.xml b/pom.xml index 3e515c3..d8ad049 100644 --- a/pom.xml +++ b/pom.xml @@ -127,6 +127,22 @@ jansi 2.4.0 + + + + + + + + com.sun.mail + jakarta.mail + 2.0.1 + + + org.springframework.boot + spring-boot-starter-mail + 3.3.4 + diff --git a/src/main/java/com/guaiguailang/harmony/config/MailConfig.java b/src/main/java/com/guaiguailang/harmony/config/MailConfig.java new file mode 100644 index 0000000..89f6f04 --- /dev/null +++ b/src/main/java/com/guaiguailang/harmony/config/MailConfig.java @@ -0,0 +1,55 @@ +package com.guaiguailang.harmony.config; + +import lombok.Data; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.JavaMailSenderImpl; + +import java.util.Properties; +@Data +@Configuration +@PropertySource(value = "classpath:application.properties", encoding = "UTF-8") +public class MailConfig { + + @Value("${spring.mail.host}") + private String host; + + @Value("${spring.mail.port}") + private int port; + + @Value("${spring.mail.username}") + private String username; + + @Value("${spring.mail.password}") + private String password; + + @Value("${spring.mail.default-encoding}") + private String defaultEncoding; + + @Value("${spring.mail.protocol}") + private String protocol; + + @Value("${spring.mail.properties.mail.debug}") + private String debug; + @Value("[和生]消息通知") + private String nickname; + + @Bean + public JavaMailSender getJavaMailSender() { + JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); + mailSender.setHost(host); + mailSender.setPort(port); + mailSender.setUsername(username); + mailSender.setPassword(password); + Properties props = mailSender.getJavaMailProperties(); + props.put("mail.transport.protocol", protocol); + props.put("mail.smtp.auth", "true"); + props.put("mail.smtp.starttls.enable", "false"); // 关闭STARTTLS + props.put("mail.debug", debug); + return mailSender; + } + +} \ No newline at end of file diff --git a/src/main/java/com/guaiguailang/harmony/service/EmailService.java b/src/main/java/com/guaiguailang/harmony/service/EmailService.java new file mode 100644 index 0000000..0bc4718 --- /dev/null +++ b/src/main/java/com/guaiguailang/harmony/service/EmailService.java @@ -0,0 +1,5 @@ +package com.guaiguailang.harmony.service; + +public interface EmailService { + void emailSend(String nickname,String to,String subject,String content); +} diff --git a/src/main/java/com/guaiguailang/harmony/service/impl/EmailServiceImpl.java b/src/main/java/com/guaiguailang/harmony/service/impl/EmailServiceImpl.java new file mode 100644 index 0000000..8fafd21 --- /dev/null +++ b/src/main/java/com/guaiguailang/harmony/service/impl/EmailServiceImpl.java @@ -0,0 +1,38 @@ +package com.guaiguailang.harmony.service.impl; + +import com.guaiguailang.harmony.config.MailConfig; +import com.guaiguailang.harmony.service.EmailService; +import com.guaiguailang.harmony.utils.KeyBase64; +import jakarta.mail.internet.MimeMessage; +import org.apache.ibatis.annotations.Select; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.mail.MailProperties; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.stereotype.Service; + +@Service +public class EmailServiceImpl implements EmailService { + @Autowired + MailConfig mailConfig; + + @Override + public void emailSend(String nickname,String to,String subject,String content) { + nickname = KeyBase64.strToBase64(nickname); + nickname = "=?UTF-8?B?"+nickname+"?="; + JavaMailSender mailSender = mailConfig.getJavaMailSender(); + try { + MimeMessage ms = mailSender.createMimeMessage(); + MimeMessageHelper msi = new MimeMessageHelper(ms,true); + msi.setFrom(nickname+' '+'<'+mailConfig.getUsername()+'>');//From 合法性检查 https://service.mail.qq.com/detail/0/995 + msi.setTo(to); + msi.setSubject(subject); + msi.setText(content,true);//true开启HTML解析 + mailSender.send(ms); + }catch (Exception e){ + System.out.println(e); + } + } + } + diff --git a/src/main/java/com/guaiguailang/harmony/utils/KeyBase64.java b/src/main/java/com/guaiguailang/harmony/utils/KeyBase64.java new file mode 100644 index 0000000..19c233d --- /dev/null +++ b/src/main/java/com/guaiguailang/harmony/utils/KeyBase64.java @@ -0,0 +1,15 @@ +package com.guaiguailang.harmony.utils; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; +import java.util.Base64; +public class KeyBase64 { + public static String strToBase64(String str){ + try{ + return Base64.getEncoder().encodeToString(str.getBytes()); + }catch (Exception e){ + System.out.println(e); + } + return str; + } +} diff --git a/src/main/java/com/guaiguailang/harmony/utils/UtilEmailSent.java b/src/main/java/com/guaiguailang/harmony/utils/UtilEmailSent.java new file mode 100644 index 0000000..8e4f7da --- /dev/null +++ b/src/main/java/com/guaiguailang/harmony/utils/UtilEmailSent.java @@ -0,0 +1,41 @@ +package com.guaiguailang.harmony.utils; + +import com.guaiguailang.harmony.config.MailConfig; +import jakarta.mail.internet.MimeMessage; +import jakarta.validation.groups.Default; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.mail.MailSender; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.mail.javamail.MimeMessagePreparator; +import org.springframework.stereotype.Component; +@Slf4j +@Component +public class UtilEmailSent { + @Autowired + MailConfig mailConfig; + public void sendEmail(String to, String subject, String content){ + sendEmail(to,subject,content, mailConfig.getNickname()); + } + public void sendEmail(String to, String subject, String content,String nickname) { + JavaMailSender mailSender = mailConfig.getJavaMailSender(); + MimeMessage mimeMessage = mailSender.createMimeMessage(); + String from = mailConfig.getUsername(); + nickname = KeyBase64.strToBase64(nickname); + System.out.println("2:"+nickname); + nickname = "=?UTF-8?B?"+nickname+"?="; + try { + MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8"); + mimeMessageHelper.setFrom(nickname+' '+'<'+from+'>'); + mimeMessageHelper.setTo(to); + + mimeMessageHelper.setSubject(subject); + mimeMessageHelper.setText(content, true); + mailSender.send(mimeMessage); + }catch (Exception e){ + log.error("(配置邮件失败){}",e.getMessage()); + } + } +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index f1271e9..bc4be5e 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -67,3 +67,44 @@ sa-token.token-style=uuid # 是否输出操作日志 sa-token.is-log=true + +#application.properties基本配置,后面我都使用此配置来发送邮件 +## 基本配置 +### smtp服务器主机(163的) +spring.mail.host=smtp.qiye.163.com +spring.mail.port=25 +### 服务协议SMTP(代表是发送邮件) +spring.mail.protocol=smtp +### 登录服务器邮箱账号 +spring.mail.username=news@mllt.cc +### 登录服务器邮箱授权码(不是邮箱密码,这个是我们开通SMTP、POP时得到的授权码) +spring.mail.password=Qq2686485465@ +### 默认邮件的编码集(MimeMessage 编码,默认UTF-8) +spring.mail.default-encoding=UTF-8 + +# 补充配置(这里具体可以参照Jakarta Mail的扩展配置) +## 默认发送方邮箱账号(当程序未指定发件人邮箱则默认取这个) +spring.mail.properties.mail.smtp.from=news@mllt.cc +## 开启权限认证 +spring.mail.properties.mail.smtp.auth=true +## 邮件接收时间的限制 +spring.mail.properties.mail.smtp.timeout=60000 +## 连接时间的限制 +spring.mail.properties.mail.smtp.connectiontimeout=60000 +## 邮件发送时间的限制(毫秒) +spring.mail.properties.mail.smtp.writetimeout=60000 +## 日志打印,邮件发送过程的日志会被输出 +spring.mail.properties.mail.debug=true +# 下面这条配置请去 src/main/java/com/guaiguailang/harmony/config/MailConfig.java 修改。在这里修改是无效的。因为这里写中文会导致乱码。 +email.form.nickname=[和生]消息通知 + + + + +# 中文乱码问题 +banner.charset=UTF-8 +server.tomcat.uri-encoding=UTF-8 +server.servlet.encoding.charset=UTF-8 +server.servlet.encoding.enabled=true +server.servlet.encoding.force=true +spring.messages.encoding=UTF-8 \ No newline at end of file diff --git a/src/test/java/com/guaiguailang/harmony/HarmonyLifeServerApplicationTests.java b/src/test/java/com/guaiguailang/harmony/HarmonyLifeServerApplicationTests.java index 4f9acfd..25fa180 100644 --- a/src/test/java/com/guaiguailang/harmony/HarmonyLifeServerApplicationTests.java +++ b/src/test/java/com/guaiguailang/harmony/HarmonyLifeServerApplicationTests.java @@ -1,16 +1,18 @@ package com.guaiguailang.harmony; +import com.guaiguailang.harmony.config.MailConfig; +import com.guaiguailang.harmony.service.EmailService; +import com.guaiguailang.harmony.utils.UtilEmailSent; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; - -import javax.xml.transform.Result; -import java.util.Scanner; - @SpringBootTest class HarmonyLifeServerApplicationTests { - + @Autowired + UtilEmailSent utilEmailSent; @Test void contextLoads() { + utilEmailSent.sendEmail("2952458479@qq.com","测试邮件标题","测试邮件内容"); } } From 81eae9159ea0adc570f20792c61ffd75c5ca0966 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=8C=E7=8B=BC=E8=93=9D=E5=A4=A9?= Date: Wed, 9 Oct 2024 16:17:48 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E9=82=AE=E4=BB=B6?= =?UTF-8?q?=E5=8F=91=E9=80=81=E9=AA=8C=E8=AF=81=E7=A0=81=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../harmony/utils/UtilEmailSent.java | 27 ++++++++++++++++--- .../utils/VerificationCodeGenerator.java | 27 +++++++++++++++++++ .../HarmonyLifeServerApplicationTests.java | 7 ++--- 3 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/guaiguailang/harmony/utils/VerificationCodeGenerator.java diff --git a/src/main/java/com/guaiguailang/harmony/utils/UtilEmailSent.java b/src/main/java/com/guaiguailang/harmony/utils/UtilEmailSent.java index 8e4f7da..c2abc81 100644 --- a/src/main/java/com/guaiguailang/harmony/utils/UtilEmailSent.java +++ b/src/main/java/com/guaiguailang/harmony/utils/UtilEmailSent.java @@ -16,10 +16,10 @@ import org.springframework.stereotype.Component; public class UtilEmailSent { @Autowired MailConfig mailConfig; - public void sendEmail(String to, String subject, String content){ - sendEmail(to,subject,content, mailConfig.getNickname()); + public int sendEmail(String to, String subject, String content){ + return sendEmail(to,subject,content, mailConfig.getNickname()); } - public void sendEmail(String to, String subject, String content,String nickname) { + public int sendEmail(String to, String subject, String content,String nickname) { JavaMailSender mailSender = mailConfig.getJavaMailSender(); MimeMessage mimeMessage = mailSender.createMimeMessage(); String from = mailConfig.getUsername(); @@ -36,6 +36,27 @@ public class UtilEmailSent { mailSender.send(mimeMessage); }catch (Exception e){ log.error("(配置邮件失败){}",e.getMessage()); + return -1; } + return 1; } + + public int sendCode(String to){ + String code = VerificationCodeGenerator.generateFourDigitCode(); + + String title="【"+mailConfig.getNickname()+"】验证码 "+code; + String context="
\n" + + "

尊敬的用户:

\n" + + "

\n" + + "您正在乖乖狼科技旗下软件产品进行操作,您的验证码为\n" + + ""+code+"\n" + + ",有效期为十分钟,请及时填写验证码。\n" + + "

\n" + + "

如果这不是您本人操作,请忽略此邮件

\n" + + "

乖乖狼科技    

\n" + + "
"; + + return sendEmail(to,title,context); + } + } \ No newline at end of file diff --git a/src/main/java/com/guaiguailang/harmony/utils/VerificationCodeGenerator.java b/src/main/java/com/guaiguailang/harmony/utils/VerificationCodeGenerator.java new file mode 100644 index 0000000..c0496ed --- /dev/null +++ b/src/main/java/com/guaiguailang/harmony/utils/VerificationCodeGenerator.java @@ -0,0 +1,27 @@ +package com.guaiguailang.harmony.utils; + +import java.util.Random; + +public class VerificationCodeGenerator { + + public static void main(String[] args) { + String verificationCode = generateFourDigitCode(); + System.out.println("Generated Verification Code: " + verificationCode); + } + + /** + * 生成一个四位数的验证码 + * @return 四位数的验证码字符串 + */ + public static String generateFourDigitCode() { + Random random = new Random(); + StringBuilder code = new StringBuilder(4); + + for (int i = 0; i < 4; i++) { + int digit = random.nextInt(10); // 生成一个0到9之间的随机整数 + code.append(digit); + } + + return code.toString(); + } +} \ No newline at end of file diff --git a/src/test/java/com/guaiguailang/harmony/HarmonyLifeServerApplicationTests.java b/src/test/java/com/guaiguailang/harmony/HarmonyLifeServerApplicationTests.java index 25fa180..4d390a3 100644 --- a/src/test/java/com/guaiguailang/harmony/HarmonyLifeServerApplicationTests.java +++ b/src/test/java/com/guaiguailang/harmony/HarmonyLifeServerApplicationTests.java @@ -1,7 +1,4 @@ package com.guaiguailang.harmony; - -import com.guaiguailang.harmony.config.MailConfig; -import com.guaiguailang.harmony.service.EmailService; import com.guaiguailang.harmony.utils.UtilEmailSent; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -11,8 +8,8 @@ class HarmonyLifeServerApplicationTests { @Autowired UtilEmailSent utilEmailSent; @Test - void contextLoads() { - utilEmailSent.sendEmail("2952458479@qq.com","测试邮件标题","测试邮件内容"); + void testEmailSentUtil() { + utilEmailSent.sendCode("2952458479@qq.com"); } }