基于SpringBoot實(shí)現(xiàn)郵箱找回密碼的代碼示例
1. 實(shí)現(xiàn)思路
- 用戶點(diǎn)擊忘記密碼
- 用戶輸入用戶名以及郵箱,點(diǎn)擊獲取驗(yàn)證碼
- 后端校驗(yàn)用戶名以及郵箱,正確后生成驗(yàn)證碼
- 生成的驗(yàn)證碼作為value,前綴加用戶名為key,放入redis中并設(shè)置過期時(shí)間
- 用戶輸入驗(yàn)證碼以及新的密碼點(diǎn)擊保存
- 后端通過前綴+用戶名獲取驗(yàn)證碼,校驗(yàn)驗(yàn)證碼的正確性
- 密碼修改成功
2. 前端UI頁面
圖為ProcessOn所畫,可作為借鑒。也可設(shè)計(jì)為先驗(yàn)證用戶名然后再進(jìn)入改密碼頁面。

3. 發(fā)件郵箱要求
作者使用的是163郵箱(其他郵箱也基本一致),需要設(shè)置POP3/SMTP/IMAP

開啟
- IMAP/SMTP服務(wù)
- POP3/SMTP服務(wù)
開啟會(huì)讓設(shè)置授權(quán)碼,授權(quán)碼要記牢!后面需要寫在配置里

再往下翻有163 SMTP服務(wù)器地址 等下需要配置在application.yml中

各家郵箱大同小異,企業(yè)郵箱的話應(yīng)該是可以直接用的,不用開啟。
4. 代碼實(shí)現(xiàn)
4.1 依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
4.2 application.yml配置
# 配置郵箱服務(wù)器,賬號(hào)密碼等
spring:
mail:
host: smtp.163.com
username: xxxxxx@163.com
password: ERBDGXLVJAQMWMDI(授權(quán)碼)
4.3 獲取驗(yàn)證碼controller
import com.clisoft.srmsbackend.common.response.ServerResponse;
import com.clisoft.srmsbackend.service.IMailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 郵箱業(yè)務(wù) 前端控制器
* @author zzw
* @date 2022-01-14
*/
@Api(tags = "郵箱業(yè)務(wù)管理")
@RestController
@RequestMapping("/mail")
public class MailController {
@Autowired
private IMailService mailService;
/**
* 獲取重置密碼的驗(yàn)證碼
*/
@ApiOperation(value = "獲取重置密碼的驗(yàn)證碼", notes = "獲取重置密碼的驗(yàn)證碼", httpMethod = "GET")
@ApiImplicitParams({
@ApiImplicitParam(name = "staffNumber", value = "用戶編號(hào)(賬號(hào))", required = true, paramType = "form"),
@ApiImplicitParam(name = "mailAddress", value = "郵箱地址", required = true, paramType = "form"),
})
@GetMapping("/getCode")
public ServerResponse getCode(String staffNumber,String mailAddress){
return mailService.getCode(staffNumber,mailAddress);
}
}
4.4 ServiceImpl層
import com.clisoft.srmsbackend.common.constant.Constants;
import com.clisoft.srmsbackend.common.response.ServerResponse;
import com.clisoft.srmsbackend.config.MailCodeConfig;
import com.clisoft.srmsbackend.dao.entity.PersStaff;
import com.clisoft.srmsbackend.dao.mapper.PersStaffMapper;
import com.clisoft.srmsbackend.framework.redis.RedisCache;
import com.clisoft.srmsbackend.service.IMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Random;
import java.util.concurrent.TimeUnit;
/**
* 郵箱業(yè)務(wù) 實(shí)現(xiàn)類
*
* @author zzw
* @date 2022-01-14
*/
@Service
public class MailServiceImpl implements IMailService {
@Autowired
private PersStaffMapper persStaffMapper;
@Autowired
private MailCodeConfig mailCodeConfig;
@Autowired
private RedisCache redisCache;
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String mailUserName;
/**
* 獲取重置密碼的驗(yàn)證碼
*
* @param staffNumber 用戶賬號(hào)
* @param mailAddress 用戶郵箱
* @return
*/
@Override
public ServerResponse getCode(String staffNumber, String mailAddress) {
// 非空校驗(yàn)
if (null == staffNumber || "".equals(staffNumber)) return ServerResponse.createByErrorMessage("賬號(hào)不能為空!");
if (null == mailAddress || "".equals(mailAddress)) return ServerResponse.createByErrorMessage("郵箱不能為空!");
// 賬號(hào)存在校驗(yàn)
PersStaff persStaff = persStaffMapper.selectPersStaffByStaffNumber(staffNumber);
if (null == persStaff) return ServerResponse.createBySuccessMessage("賬號(hào)不存在!");
if (!persStaff.getEmail().equals(mailAddress)) return ServerResponse.createByErrorMessage("輸入郵箱和預(yù)留郵箱不一致!");
String verifyCode = redisCache.getCacheObject(Constants.MAIL_CODE_KEY + staffNumber);
if (verifyCode == null) {
verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);//生成短信驗(yàn)證碼
}
Integer overtime = mailCodeConfig.getOvertime(); // 過期時(shí)間
// 驗(yàn)證碼存入redis并設(shè)置過期時(shí)間
redisCache.setCacheObject(Constants.MAIL_CODE_KEY + staffNumber, verifyCode, overtime, TimeUnit.MINUTES);
// 編寫郵箱內(nèi)容
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("<html><head><title></title></head><body>");
stringBuilder.append("您好<br/>");
stringBuilder.append("您的驗(yàn)證碼是:").append(verifyCode).append("<br/>");
stringBuilder.append("您可以復(fù)制此驗(yàn)證碼并返回至科研管理系統(tǒng)找回密碼頁面,以驗(yàn)證您的郵箱。<br/>");
stringBuilder.append("此驗(yàn)證碼只能使用一次,在");
stringBuilder.append(overtime.toString());
stringBuilder.append("分鐘內(nèi)有效。驗(yàn)證成功則自動(dòng)失效。<br/>");
stringBuilder.append("如果您沒有進(jìn)行上述操作,請(qǐng)忽略此郵件。");
MimeMessage mimeMessage = mailSender.createMimeMessage();
// 發(fā)件配置并發(fā)送郵件
try {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
//這里只是設(shè)置username 并沒有設(shè)置host和password,因?yàn)閔ost和password在springboot啟動(dòng)創(chuàng)建JavaMailSender實(shí)例的時(shí)候已經(jīng)讀取了
mimeMessageHelper.setFrom(mailUserName);
// 用戶的郵箱地址
mimeMessageHelper.setTo(mailAddress);
// 郵件的標(biāo)題
mimeMessage.setSubject("郵箱驗(yàn)證-科研管理系統(tǒng)");
// 上面所拼接的郵件內(nèi)容
mimeMessageHelper.setText(stringBuilder.toString(), true);
mailSender.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
return ServerResponse.createBySuccessMessage("獲取驗(yàn)證碼成功,請(qǐng)查看移步您的郵箱" + mailAddress + "查看驗(yàn)證碼!");
}
}
4.5 修改密碼controller
/**
* 驗(yàn)證碼重置密碼
*/
@ApiOperation(value = "驗(yàn)證碼重置密碼", notes = "驗(yàn)證碼重置密碼", httpMethod = "POST")
@PostMapping("/codeUpdatePwd")
public ServerResponse codeUpdatePwd(@RequestBody CodeUpdatePwdVo codeUpdatePwdVo){
return persStaffService.codeUpdatePwd(codeUpdatePwdVo);
}
4.6 修改密碼ServiceImpl
/**
* 驗(yàn)證碼重置密碼
*
* @param codeUpdatePwdVo
* @return
*/
@Override
public ServerResponse codeUpdatePwd(CodeUpdatePwdVo codeUpdatePwdVo) {
String staffNumber = codeUpdatePwdVo.getStaffNumber();
String code = codeUpdatePwdVo.getCode();
String loginPassword = codeUpdatePwdVo.getLoginPassword();
// 非空校驗(yàn)
if (null == staffNumber || "".equals(staffNumber)) return ServerResponse.createByErrorMessage("賬號(hào)不能為空!");
if (null == code || "".equals(code)) return ServerResponse.createByErrorMessage("驗(yàn)證碼不能為空!");
if (null == loginPassword || "".equals(loginPassword)) return ServerResponse.createByErrorMessage("密碼不能為空!");
// 賬號(hào)存在校驗(yàn)
PersStaff persStaff = persStaffMapper.selectPersStaffByStaffNumber(staffNumber);
if (null == persStaff) return ServerResponse.createBySuccessMessage("賬號(hào)不存在!");
// 驗(yàn)證碼過期校驗(yàn)
String cacheCode = redisCache.getCacheObject(Constants.MAIL_CODE_KEY + staffNumber); // 獲取緩存中該賬號(hào)的驗(yàn)證碼
if (cacheCode == null) {
return ServerResponse.createByErrorMessage("驗(yàn)證碼已過期,請(qǐng)重新獲??!");
}
// 驗(yàn)證碼正確性校驗(yàn)
if (!cacheCode.equals(code)) {
return ServerResponse.createByErrorMessage("驗(yàn)證碼錯(cuò)誤!");
}
// 修改密碼
int result = 0;
try {
result = persStaffMapper.updatePwdByStaffNumber(staffNumber, PasswordStorageUtil.createHash(codeUpdatePwdVo.getLoginPassword()));
} catch (PasswordStorageUtil.CannotPerformOperationException e) {
return ServerResponse.createByErrorMessage("密碼加密時(shí)發(fā)生錯(cuò)誤");
}
if (result > 0) {
// 將驗(yàn)證碼過期
redisCache.expire(Constants.MAIL_CODE_KEY + staffNumber, 0);
return ServerResponse.createBySuccessMessage("密碼充值成功!請(qǐng)牢記您的密碼!");
}
return ServerResponse.createByErrorMessage("未知錯(cuò)誤,密碼修改失敗,請(qǐng)重試!");
}
4.7 效果圖

demo地址:https://gitee.com/lm_8692769/email-reset-password-demo
以上就是基于SpringBoot實(shí)現(xiàn)郵箱找回密碼的代碼示例的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot實(shí)現(xiàn)郵箱找回密碼的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot項(xiàng)目加入沖突動(dòng)態(tài)監(jiān)測算法的實(shí)現(xiàn)
沖突動(dòng)態(tài)監(jiān)測算法是一種網(wǎng)絡(luò)通信中的沖突檢測方法,適用于無線網(wǎng)絡(luò)或其他共享傳輸介質(zhì)的環(huán)境,本文主要介紹了SpringBoot項(xiàng)目加入沖突動(dòng)態(tài)監(jiān)測算法的實(shí)現(xiàn),感興趣的可以了解一下2023-09-09
Java中Array List與Linked List的實(shí)現(xiàn)分析
這篇文章主要給大家介紹了關(guān)于Array List與Linked List實(shí)現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
SpringBoot?Validation快速實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的示例代碼
在實(shí)際開發(fā)中,肯定會(huì)經(jīng)常遇到對(duì)參數(shù)字段進(jìn)行校驗(yàn)的場景,通常我們只能寫大量的if else來完成校驗(yàn)工作,而如果使用SpringBoot Validation則可以輕松的通過注解來完成,接下來小編給大家介紹下利用SpringBoot?Validation快速實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的示例代碼,需要的朋友參考下吧2022-06-06
Java實(shí)現(xiàn)文件上傳至服務(wù)器的方法
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)文件上傳至服務(wù)器的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
淺談Java 三種方式實(shí)現(xiàn)接口校驗(yàn)
這篇文章主要介紹了淺談Java 三種方式實(shí)現(xiàn)接口校驗(yàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10

