当前位置:   article > 正文

SpringBoot发送邮箱验证码_springmail check

springmail check

SpringBoot发送邮箱验证码

1、导入依赖

		<!--导入邮箱-->
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--加密处理-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

2、编写yml配置文件

spring:
  thymeleaf:
    cache: false
  #配置静态资源
  #web:
    #resources:
      #static-locations: classpath:/static/,classpath:/templates/,classpath:/i18n/
  #邮箱验证
  mail:
    ##163 smtp.163.com
    host: smtp.163.com
    ##编码格式
    default-encoding: UTF-8
    ##邮箱用户名
    username: xys@163.com
    ##邮箱密码(使用授权码)
    password: 88888888888888
    #开启加密规则
    properties:
      mail.smtp.ssl.enable: true
check:
  #邮箱验证配置
  mail:
    sender: xys@163.com   #发送者
    subject: 高校志愿者平台            #邮箱标题
    str1: 【高校志愿者平台】你的验证码为【
    str2:(若不是本人操作,可忽略该条邮件)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

3、编写随机验证码工具类

public class CodeUtil {

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
    
    /**
     * 随机生成6位验证码
     *
     */
    public String getRandomCode(Integer code){
        Random random = new Random();
        StringBuffer result= new StringBuffer();
        for (int i=0;i<code;i++){
            result.append(random.nextInt(10));
        }
        return result.toString();
    }
    
    //保存验证码和时间
    public Code saveCode(String code){
        Code code1=new Code();
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MINUTE, 5);
        String currentTime = sf.format(c.getTime());// 生成5分钟后时间,用户校验是否过期
        //验证码加密
        String encode=passwordEncoder().encode(code);
        code1.setCode(encode);
        code1.setCurrentTime(currentTime);
        return code1;
    }
    
    /**
     * 解密处理
     * @param code 输入
     * @param code1 目标
     */
    public Boolean deciphering(String code,String code1){
        boolean matches = passwordEncoder().matches(code,code1);
        return matches;
    }


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

4、编写邮箱发送工具类

@Component
public class EmailMessageUtil{

    @Autowired
    JavaMailSenderImpl mailSender;
    
    @Value("${check.mail.sender}")
    private String sender;
    
    @Value("${check.mail.subject}")
    private String subject;
    
    @Value("${check.mail.str1}")
    private String str1;
    
    @Value("${check.mail.str2}")
    private String str2;
    /**
     * 发送邮件
     *
     * @return 提示信息
     */
    public String sendMessage(String email,String code)throws MailException {
        //引入编码工具类
        SimpleMailMessage message = new SimpleMailMessage();
        //发送者
        message.setFrom(sender);
        //发送邮件地址
        message.setTo(email);
        //标题
        message.setSubject(subject);
        //内容
        message.setText(str1+code+str2);
        mailSender.send(message);
        return "send success";
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

5、测试

//前台验证邮箱
    @RequestMapping("/checkEmail")
    public String checkEmail(@RequestParam("email") String email,
                             HttpSession session,
                             Model model){
        Users users=new Users();
        users.setEmail(email);
        if (loginService.CheckOnlyEmail(users)) {
            model.addAttribute("msg","没有此用户,请注册!");
            return "register";
        }else {
            Long id = loginService.findIdByEmail(email);
            session.setAttribute("user_id",id);
            model.addAttribute("info","已往你的邮箱为"+email+"发送了验证码");
            //发送验证码
            CodeUtil codeUtil=new CodeUtil();
            //获取六位验证码
            String randomCode = codeUtil.getRandomCode(6);
            //先清除session域
            session.removeAttribute("checkCode");
            //加密验证码存放session域中
            session.setAttribute("checkCode",codeUtil.passwordEncoder().encode(randomCode));
            sendMessage.sendMessage(email,randomCode);
            return "updatePwd";
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/686401
推荐阅读
相关标签
  

闽ICP备14008679号