正文
一、环境准备
- SpringBoot 2.0.0
- QQ邮箱授权码
二、开发环节
2.1.导入依赖(2021/11/13)
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>laokou-third-party-demo</groupId> <artifactId>laokou-third-party-demo</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>io.laokou</groupId> <artifactId>laokou-third-party-email-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>laokou-third-party-email-demo</name> <description>第三方-邮件</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
2.2.配置文件(2021/11/13)
server: port: 12032 servlet: context-path: /laokou-demo spring: application: name: laokou-third-party-email-demo mail: default-encoding: UTF-8 host: smtp.qq.com username: 你的邮箱地址 password: 授权码 properties: mail: smtp: auth: true starttls: enable: true required: true
2.3.核心代码(2021/11/13)
package io.laokou.email.utils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Component; import java.util.Date; /** * TODO * * @author Kou Shenhai 2413176044 * @version 1.0 */ @Component public class EmailUtil { @Autowired private JavaMailSender sender; @Value("${spring.mail.username}") private String fromUser; public Integer send() { try{ // 构建一个邮件对象 SimpleMailMessage message = new SimpleMailMessage(); //发送人 message.setFrom(fromUser); //接收人 message.setTo(你发给哪个人的邮箱); //邮件主题 message.setSubject("验证码"); //邮件时间 message.setSentDate(new Date()); //邮件内容 message.setText(String.format("验证码:%s,您正在登录,若非本人操作,请勿泄露。", 233232)); sender.send(message); return 1; } catch (Exception e) { e.printStackTrace(); System.out.println("error:"+e.getMessage()); return 0; } } }
三、运行截图
初始效果截图(2021/11/13)
四、优化过程
4.1.优化(一) 2021/11/13
将写死的验证码变成随机的六位数
1).在pom.xml加入依赖
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency>
2)修改核心代码
message.setText(String.format("验证码:%s,您正在登录,若非本人操作,请勿泄露。", RandomStringUtils.randomNumeric(6)));
注:代码已更新
4.2 优化(二)2021/11/14
加入freemarker + mysql 生成对应的模板内容
1).数据库设计
DROP TABLE IF EXISTS `boot_freemarker_demo_template`; CREATE TABLE `boot_freemarker_demo_template` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id', `template_code` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '模板编码 login_email_captcha登录邮件验证码', `template_content` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL COMMENT '模板内容', `template_sign` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '模板签名', `template_subject` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '模板主题', `template_id` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '模板编号', `template_type` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '模板类型 email邮件 sms短信', PRIMARY KEY (`id`, `template_id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
2).加入依赖 2021/11/15
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.17</version> </dependency>
3).yml配置 2021/11/15
spring.jackson.time-zone=GMT+8 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss mybatis-plus.mapper-locations=classpath:/mybatis/mapper/*.xml mybatis-plus.type-aliases-package=io.laokou.freemarker.entity mybatis-plus.global-config.db-config.id-type=id_worker mybatis-plus.global-config.db-config.field-strategy=not_null mybatis-plus.global-config.db-config.column-like=true mybatis-plus.global-config.banner=false mybatis-plus.configuration.map-underscore-to-camel-case=true mybatis-plus.configuration.cache-enabled=false mybatis-plus.configuration.call-setters-on-nulls=true spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver spring.datasource.druid.url=jdbc:mysql://127.0.0.1:3306/kcloud?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true spring.datasource.druid.username=root spring.datasource.druid.password=123456 spring.datasource.druid.initial-size=10 spring.datasource.druid.max-active=100 spring.datasource.druid.min-idle=10 spring.datasource.druid.max-wait=60000 spring.datasource.druid.pool-prepared-statements=true spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20 spring.datasource.druid.time-between-eviction-runs-millis=60000 spring.datasource.druid.min-evictable-idle-time-millis=300000 spring.datasource.druid.test-while-idle=true spring.datasource.druid.test-on-borrow=false spring.datasource.druid.test-on-return=false spring.datasource.druid.stat-view-servlet.enabled=true spring.datasource.druid.stat-view-servlet.url-pattern=/druid/** spring.datasource.druid.stat-view-servlet.login-username=root spring.datasource.druid.stat-view-servlet.login-password=123456 spring.datasource.druid.filter.stat.log-slow-sql=true spring.datasource.druid.filter.stat.slow-sql-millis=1000 spring.datasource.druid.filter.stat.merge-sql=false spring.datasource.druid.filter.wall.config.multi-statement-allow=true
4).核心代码 2021/11/15
public class TemplateUtil { public static String getTemplate(String templateContent,Map<String,Object> params) throws IOException, TemplateException { //Freemarker配置 Configuration configuration = new Configuration(Configuration.getVersion()); configuration.setLocale(Locale.CHINA); configuration.setDefaultEncoding("UTF-8"); StringTemplateLoader loader = new StringTemplateLoader(); //略过替换部分标签 loader.putTemplate("templates",templateContent); configuration.setTemplateLoader(loader); Template template = configuration.getTemplate("templates","utf-8"); StringWriter result = new StringWriter(); template.process(params,result); return result.toString(); } }
注:代码已更新
4.2 优化(三)2021/11/17
加入redis + mysql索引
4.3 优化(四)2021/11/19
服务解耦,架构升级,由单体架构改成微服务架构,拆分如下:
laokou-third-party-email-demo:邮件发送服务
laokou-sso-captcha-demo:服务调用方
laokou-freemarker-demo:模板生成服务
4.4 优化(五)2021/11/19
提取公共代码,进行代码优化