springboot注册功能及邮箱验证的功能

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源码!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值