1.发送邮件工具的编写
参考:邮件工具的编写
2.在UserService中实现对前端提交数据的验证服务
@Override
public Map<String, Object> register(User user) {
Map<String, Object> map = new HashMap<>();
if (user == null) {
throw new IllegalArgumentException("参数不能为空");
}
//判断用户名
if (StringUtils.isBlank(user.getUsername())) {
map.put("usernameMsg", "用户名不能为空");
return map;
}
//判断密码
if (StringUtils.isBlank(user.getPassword())) {
map.put("passwordMsg", "密码不能为空");
return map;
}
//判断邮箱
if (StringUtils.isBlank(user.getEmail())) {
map.put("emailMsg", "邮箱不能为空");
return map;
}
User u = userMapper.selectByName(user.getUsername());
if (u != null) {
map.put("usernameMsg", "用户名已存在");
return map;
}
u = userMapper.selectByEmail(user.getEmail());
if (u != null) {
map.put("emailMsg", "邮箱已存在");
return map;
}
//注册用户
user.setSalt(CommunityUtil.generateUUID().substring(0, 5));//随机产生五位字符串
user.setPassword(CommunityUtil.md5(user.getPassword() + user.getSalt()));//密码加密后加上随机尾数
user.setType(0);
user.setStatus(0);
user.setActivationCode(CommunityUtil.generateUUID());//设置激活码
user.setCreateTime(new Date());
user.setHeaderUrl(String.format("http://image.nowcoder.com/head/%dt.png", new Random().nextInt(1000)));//设置随机头像
userMapper.insertUser(user);
return map;
通过查询返回错误信息,若无错误信息初始化用户其余属性,激活状态为未激活;
3.与前端交互(controller层)
//注册请求
@PostMapping("/register")
public String register(Model model, User user) {
Map<String, Object> map = userService.register(user);
//没有错误信息说明注册信息合法
if (map == null || map.isEmpty()) {
model.addAttribute("msg", "注册成功,我们已将您的验证信息发送至您的邮箱,请尽快验证!");
model.addAttribute("url", "/index");
return "/site/operate-result";
} else {//注册信息有误
model.addAttribute("usernameMsg", map.get("usernameMsg"));
model.addAttribute("passwordMsg", map.get("passwordMsg"));
model.addAttribute("emailMsg", map.get("emailMsg"));
}
return "/site/register";
}
将错误信息返回给前端,若无错误信息则激活成功,给用户发送邮件
4.添加发送邮件功能(service层)
//发送激活邮件
Context context = new Context();
context.setVariable("email", user.getEmail());
//url:http://localhost:8080/community/activation/101/激活码
String url = domain + contextPath + "/activation/" + user.getId() + "/" + user.getActivationCode();
context.setVariable("url", url);
String content = templateEngine.process("mail/activation", context);
mailClient.sendMail(user.getEmail(), "激活账号", content);
domain 和 contextPath 在application中配置,将用户id与激活码放入连接中方便获取
5.新建常量类工具
public interface CommunityConstant {
/**
* 激活成功
*/
int ACTIVATION_SUCCESS = 0;
/**
* 重复激活
*/
int ACTIVATION_REPEAT = 1;
/**
* 激活失败
*/
int ACTIVATION_FAILURE = 2;
}
常量用大写
6.判断激活状态(service层)
//返回激活状态
public int activation(int userId, String code) {
User user = userMapper.selectById(userId);
if (user.getStatus() == 1) {
return ACTIVATION_REPEAT;
} else if (user.getActivationCode().equals(code)) {
userMapper.updateStatus(userId, 1);
return ACTIVATION_SUCCESS;
} else {
return ACTIVATION_FAILURE;
}
}
激活成功更改用户状态
7.点击邮箱激活连接处理请求(controller层)
//激活账号并跳转
@GetMapping("/activation/{userId}/{code}")
public String activation(Model model, @PathVariable("userId") int userId,@PathVariable("code") String code){
int activation = userService.activation(userId, code);
if (activation == ACTIVATION_SUCCESS){
model.addAttribute("msg","您已激活成功,将自动为您跳转至登录页面");
model.addAttribute("url","/login");
}else if (activation == ACTIVATION_REPEAT){
model.addAttribute("msg","您的账号已激活,请勿重复激活,将自动为您跳转至登录页面");
model.addAttribute("url","/login");
}else {
model.addAttribute("msg","您的激活码无效,激活失败!");
model.addAttribute("url","/index");
}
return "/site/operate-result";
}
@PathVariable RESTful风格从url获取用户id与激活码比对进行激活
点击看gitee源码!