SpringBoot集成邮箱发送和验证修改密码

SpringBoot 使用邮箱修改用户密码

  • 最近一直申请阿里云的短信验证,但是总审核不过,所以想着用邮箱验证的方式来进行密码的找回。
  • 查看了百度好多的demo,进本都是半瓶子咣当,只给你截取部分代码,对新手十分不友好。下面链接是GitHub地址,欢迎各位star
    https://github.com/gaoxiaohao/email.git
  • 废话不多说,直接上代码。
  • 邮箱验证的整体思路还是 点击输入邮箱后 会发送到自己邮箱一个带有链接的网页,点击链接跳转到修改密码页面。
  • 我的项目是SpringBoot+MyBatis 的开发,ServiceImp 和mapper.xml相关代码就不上传了。
    6. pom 添加配置
  • org.springframework.boot spring-boot-starter-mail

7. 邮箱配置

  • #邮箱配置
    spring.mail.host=smtp.163.com
    spring.mail.username=//我用的是网易的邮箱
    spring.mail.password=//密码是开启pop3相关权限的密码,百度一查就可以
    spring.mail.default-encoding=UTF-8
    spring.mail.properties.smtp.auth=true
    spring.mail.properties.smtp.starttls.enable=true
    spring.mail.properties.smtp.starttls.required=true
    spring.mail.properties.mail.smtp.ssl.enable=true

  • Controller代码

  • @RequestMapping(value = “/email”)
    @ResponseBody
    public Map forgetPass(HttpServletRequest request, String userName) {
    User users = userServiceImp.findUserPhone("@163.com");
    Map map = new HashMap();
    String msg = “”;
    if (users == null) { //用户名不存在
    msg = “用户名不存在,你不会忘记用户名了吧?”;
    map.put(“msg”, msg);
    return map;
    }
    try {
    String secretKey = UUID.randomUUID().toString(); //密钥
    Timestamp outDate = new Timestamp(System.currentTimeMillis() + 30 * 60 * 1000);
    long date = outDate.getTime() / 1000 * 1000;
    users.setValidataCode(secretKey);
    users.setRegisterDate(String.valueOf(outDate));
    userServiceImp.updateUserInfo(users); //保存到数据库
    String key = users.getUserName() + “ " + d a t e + " " + date + " "+date+"” + secretKey;
    MD5Util md5Util = new MD5Util();
    String digitalSignature = md5Util.encode(key);
    String emailTitle = “密码找回”;
    String path = request.getContextPath();
    String basePath = request.getScheme() + “/” + request.getServerName() + “:” + request.getServerPort() + path + “/”;
    String resetPassHref = basePath + “hello?sid=” + digitalSignature + “&userName=” + users.getUserPhone();
    String emailContent = “请勿回复本邮件.点击下面的链接,重设密码
    点击我重新设置密码”

         System.out.print(resetPassHref);
         sendHtmlMail(emailContent, emailTitle, "@qq.com");
         msg = "操作成功,已经发送找回密码链接到您邮箱。请在30分钟内重置密码";
    
     } catch (Exception e) {
         e.printStackTrace();
         msg = "邮箱不存在";
     }
     map.put("msg", msg);
     return map;
    

    }

  • 发送邮件代码

  • public void sendHtmlMail(String content, String subject, String to) {
    MimeMessage message = sender.createMimeMessage();

    try {
        //true表示需要创建一个multipart message
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom("@163.com");
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
    
        sender.send(message);
        System.out.println("html已发送");
    } catch (Exception e) {
        System.out.println("发送html邮件时发生异常");
    }
    

    }

  • 页面跳转后代码

  • @RequestMapping(value = “hello”, method = RequestMethod.GET)
    public ModelAndView checkResetLink(String sid, String userName) {
    ModelAndView model = new ModelAndView(“error”);
    String msg = “”;
    if (sid.equals("") || userName.equals("")) {
    msg = “链接不完整,请重新生成”;
    model.addObject(“msg”, msg);
    return model;
    }
    User users = userServiceImp.findUserPhone("@163.com");
    if (users == null) {
    msg = “链接错误,无法找到匹配用户,请重新申请找回密码.”;
    model.addObject(“msg”, msg);
    return model;
    }

    Timestamp outDate = Timestamp.valueOf(users.getRegisterDate());
    if (outDate.getTime() <= System.currentTimeMillis()) {         
        msg = "链接已经过期,请重新申请找回密码.";
        model.addObject("msg", msg);
        return model;
    }
    String key = users.getUserName() + "$" + outDate.getTime() / 1000 * 1000 + "$" + users.getValidataCode();          
    MD5Util md5Util = new MD5Util();
    String digitalSignature = md5Util.encode(key);
    System.out.println(key + "\t" + digitalSignature);
    if (!digitalSignature.equals(sid)) {
        msg = "链接不正确";
        model.addObject("msg", msg);
        return model;
    }
    model.setViewName("hello");  
    model.addObject("userName", userName);
    return model;
    

    }

  • H5页面

Title

你可能感兴趣的:(SpringBoot)