标签:mave 查看 xxx 邮件 autowire 细节 连接池 als tls
state
和code
字段,state表示激活状态,code是激活码state
字段为0(表示未激活),还有个随机的codestate
未为1,否则不激活
依赖
<!-- 父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<!-- JDK版本 -->
<properties>
<java.version>1.8</java.version>
</properties>
<!-- 各种依赖 -->
<dependencies>
<!-- Web启动类 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 阿里巴巴数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- 连接池的启动器 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- 添加数据库启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- 数据库驱动,因为springboot不知道我们使用什么数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.28</version>
</dependency>
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- mybatis启动器依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- Junit依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- 邮件依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
SpringBoot配置文件
# 数据库连接池,自带hikari
# hikari:
# maximum-pool-size: 30 # 最大连接数
# minimum-idle: 10 # 最小连接数
# 自己的邮件配置
Howl:
mail:
from: XXXXXXXXXX@qq.com
subject: 激活邮件
address: http://localhost:8080
spring:
# 数据源
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC
username: root
password:
druid:
initial-size: 10
min-idle: 5
max-active: 20
pool-prepared-statements: true
# 邮件配置
mail:
host: smtp.qq.com
port: 465
username: XXXXXXXXXX@qq.com
password: XXXXXXXXXXXXXXXXXX #这里是smtp的密码,不是QQ密码
protocol: smtp
default-encoding: utf-8
properties:
mail:
imap:
ssl:
socketFactory:
fallback: false
smtp:
auth: true
ssl:
enable: true
socketFactory:
class: com.fintech.modules.base.util.mail.MailSSLSocketFactory
starttls:
enable: true
required: true
test-connection: false
# mybatis配置
mybatis:
# 别名
type-aliases-package: com.howl.dto
# 映射文件路径,一般不用了
# mapper-locations: classpath:mappers/*.xml
configuration:
# 开启驼峰映射
map-underscore-to-camel-case: true
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`state` int(1) NOT NULL DEFAULT '0' COMMENT '用户激活状态:0表示未激活,1表示激活',
`code` varchar(255) NOT NULL COMMENT '激活码',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
public class User {
private int id;
private String username;
private String password;
private String email;
private String state;
private String code;
//Getters、Setters
//Constructor
}
@Mapper
public interface UserMapper {
@Options(useGeneratedKeys = true, keyProperty = "id")
@Insert("INSERT INTO user (`username`,`password`,`email`,`code`) VALUES (#{username},#{password},#{email},#{code})")
public int register(User user);
@Select("SELECT * FROM user WHERE id = #{id}")
public User getUserById(int id);
@Update("UPDATE user SET state = 1 WHERE id = #{id} AND code = #{code}")
public int updateByIdAndCode(int id, String code);
}
邮件工具类
@Component
public class EmailUtil {
@Autowired
private JavaMailSender javaMailSender;
@Value("${Howl.mail.from}")
private String from;
@Value("${Howl.mail.subject}")
private String subject;
@Value("${Howl.mail.address}")
private String address;
public void sendEmail(int id, String code, String to) {
String url = address + "/verify?id=" + id + "&code=" + code;
String label = "<a href=" + url + ">点击此处激活账号,有没有反应可以复制链接从浏览器打开</a>";
try {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
messageHelper.setFrom(from);
messageHelper.setTo(to);
messageHelper.setSubject(subject);
messageHelper.setText(label,true);
messageHelper.setSentDate(new Date());
javaMailSender.send(mimeMessage);
}catch (Exception e){
new RuntimeException("邮件发送失败",e);
}
}
}
激活码
@Component
public class CodeUtil {
public String getCode() {
//返回UUID
return UUID.randomUUID().toString().replace("-", "");
}
}
@Service
public class UserService {
@Autowired
UserMapper userMapper;
@Autowired
EmailUtil emailUtil;
@Autowired
CodeUtil codeUtil;
public int register(String username, String password, String email) {
int result;
String code = codeUtil.getCode();
User user = new User(username, password, email, code);
result = userMapper.register(user);
// 开线程来发邮件,提高效率,发邮件很慢
new Thread(() -> {
emailUtil.sendEmail(user.getId(), code, email);
}).run();
return result;
}
public int verify(int id, String code) {
User user = userMapper.getUserById(id);
if (user != null) {
return userMapper.updateByIdAndCode(id, code);
}
return 0;
}
}
@RestController
public class UserController {
@Autowired
UserService userService;
@GetMapping(value = "/register")
public String register(String username, String password, String email) {
return userService.register(username, password, email) + "";
}
@GetMapping(value = "/verify")
public String verify(int id, String code) {
return userService.verify(id, code) + "";
}
}
@SpringBootApplication
@MapperScan("com.howl.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
都看到这里了,没有前端页面。。。大家自行想象登录操作
这里使用地址栏GET方式注册,正式注册用POST表单的不要学我,为了懒不想写前端
总结
实现太简单,不过基本功能还是有的,细节方面可以慢慢补充
标签:mave 查看 xxx 邮件 autowire 细节 连接池 als tls
原文地址:https://www.cnblogs.com/Howlet/p/12198922.html