赞
踩
<!--导入邮箱-->
<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>
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: 】(若不是本人操作,可忽略该条邮件)
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;
}
}
@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";
}
}
//前台验证邮箱
@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";
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。