小伙伴们,你们好呀!我是老寇!(本文用于记录代码,如果能帮助你,不胜荣幸)
一、环境准备
二、仓库地址
gitee : KCloud: 老寇云,我们一起进阶(已上传)
三、开发环节
3.1.导入依赖(2021/11/13)
4.0.0
laokou-third-party-demo
laokou-third-party-demo
1.0-SNAPSHOT
io.laokou
laokou-third-party-email-demo
0.0.1-SNAPSHOT
laokou-third-party-email-demo
第三方-邮件
1.8
org.springframework.boot
spring-boot-starter-mail
org.springframework.boot
spring-boot-starter-web
3.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
3.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)
五、优化过程
5.1.优化(一) 2021/11/13
将写死的验证码变成随机的六位数
1).在pom.xml加入依赖
org.apache.commons
commons-lang3
2)修改核心代码
message.setText(String.format("验证码:%s,您正在登录,若非本人操作,请勿泄露。", RandomStringUtils.randomNumeric(6)));
注:代码已更新
5.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
org.springframework.boot
spring-boot-starter-freemarker
com.baomidou
mybatis-plus-boot-starter
3.0.5
mysql
mysql-connector-java
com.alibaba
druid-spring-boot-starter
1.1.17
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 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();
}
}
注:代码已更新
5.2 优化(三)2021/11/17
加入redis + mysql索引
作者开发中...