图片验证码
展示
首先生成图片通过HTTP响应输出到前端页面中,并在session的Attribute中设置code,以便后续输入的和session的key进行对比
@RequestMapping("/checkCode")
//HttpSession用于存储和获取会话数据
public void checkCode(HttpServletResponse response, HttpSession session, Integer type) throws
IOException {
CreateImageCode vCode = new CreateImageCode(130, 38, 5, 10);
//设置响应头
response.setHeader("Pragma", "No-cache");
//
response.setHeader("Cache-Control", "no-cache");
//不会被缓存
response.setDateHeader("Expires", 0);
//输出的是一个图片
response.setContentType("image/jpeg");
//生成验证码
String code = vCode.getCode();
System.out.println("code:" + code);
if (type == null || type == 0) {
//存储在名为CHECK_CODE_KEY的键中,值为code
session.setAttribute(Constants.CHECK_CODE_KEY, code);
} else {
session.setAttribute(Constants.CHECK_CODE_KEY_EMALL, code);
}
// 将生成的验证码图片写入HTTP响应输出流
vCode.write(response.getOutputStream());
}
发送邮箱验证码
通过使用javaMailSender通过qq邮箱提供的STMP服务,向指定的邮箱发送随机生成的验证码
配置:
spring.mail.host=smtp.qq.com
spring.mail.port=465
spring.mail.username=test@qq.com
spring.mail.password=
spring.mail.default-encoding=UTF-8
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.debug=true
主要设置的就是pwd和用户名,可以去百度教程找一下如何配置这两个
controller层
//发送邮箱验证码
@RequestMapping("/sendEmailCode")
@GlobalInterceptor(checkParams = true, checkLogin = false)
public ResponseVO sendEmailCode(HttpSession session,
@VerifyParam(required = true, regex = VerifyRegexEnum.EMAIL, max = 150) String email,
@VerifyParam(required = true) String checkCode,
@VerifyParam(required = true) Integer type) throws BusinessException {
try{
if (!checkCode.equalsIgnoreCase((String) session.getAttribute(Constants.CHECK_CODE_KEY_EMALL))) {
throw new BusinessException("图片验证码不正确");
}
//调用emailCodeService中的sendEmailCode方法发送邮件验证码
emailCodeService.sendEmailCode(email, type);
return getSuccessResponseVO(null);
} finally {
//从会话中移除CHECK_CODE_KEY_EMALL属性
session.removeAttribute(Constants.CHECK_CODE_KEY_EMALL);
}
}
1. 先验证图片验证码是否正确
2.发送图片验证码
EmailCodeServicelmpl层
@Override
//声明方法的事务属性,遇到Exception时回滚
@Transactional(rollbackFor = Exception.class)
public void sendEmailCode(String email, Integer type) throws BusinessException {
if(type==0){
//根据邮箱查询用户信息
UserInfo userInfo = userInfoMapper.selectByEmail(email);
if(null!=userInfo){
throw new BusinessException("邮箱已经存在");
}
}
//生成一个随机的五位数字验证码
String code = StringTools.getRandomNumber(Constants.LENGTH_5);
//重写方法,调用重写的sendEmailCode,向邮箱发送验证码
sendEmailCode(email, code);
//将之前的验证码都值为无效
emailCodeMapper.disableEmailCode(email);
EmailCode emailCode = new EmailCode();
emailCode.setCode(code);
emailCode.setEmail(email);
//设置状态为未使用
emailCode.setStatus(Constants.ZERO);
//设置创建时间为当前时间
emailCode.setCreateTime(new Date());
//插入新的EmailCode对象到数据库中
emailCodeMapper.insert(emailCode);
}
1.随机生成验证码
2.向指定邮箱生成验证码。
3.使得先前生成的验证码都无效
4.将code插入数据库中
发送验证码真正的逻辑:
private void sendEmailCode(String toEmail, String code) {
try{
//创建一个新的MinMessage
MimeMessage message = javamailSender.createMimeMessage();
//使用mimeMessageHelper来简化创建复杂的MimeMessage
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message);
//设置邮箱发送者
mimeMessageHelper.setFrom(appConfig.getSendUserName());
//设置邮箱的接收者
mimeMessageHelper.setTo(toEmail);
//从Redis缓存中的系统设置
SysSettingsDto sysSettingsDto = redisComponent.getSysSettingsDto();
//设置标题
mimeMessageHelper.setSubject(sysSettingsDto.getRegisterMailTitle());
//设置内容
mimeMessageHelper.setText(String.format(sysSettingsDto.getRegisterEmailContent(), code));
mimeMessageHelper.setSentDate(new Date());
javamailSender.send(message);
}catch (Exception e){
logger.error("邮件发送失败", e);
try {
throw new BusinessException(e.getMessage());
} catch (BusinessException ex) {
throw new RuntimeException(ex);
}
}
disableEmailCode:
emailCodeMapper.disableEmailCode(email);
xml层:
update email_code set status 1 where email = #{email} and status = 0