SpringBoot-Mail工具實(shí)現(xiàn)郵箱驗(yàn)證碼登錄注冊(cè)功能
前言
現(xiàn)在許多pc程序都有著使用郵箱驗(yàn)證碼實(shí)現(xiàn)登錄注冊(cè)的功能,那么我們應(yīng)該如何完成郵箱驗(yàn)證碼功能呢,我們可以使用springboot內(nèi)置的springboot-mail再結(jié)合redis來完成這個(gè)功能。
代碼實(shí)現(xiàn)
一、引入springboot-mail和redis依賴
<!--郵箱依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <!--redis依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
二、郵箱常量類
public class MailConstant { /** * 郵箱驗(yàn)證redis前綴 */ public static final String CODE_KEY_PREFIX = "mail:"; }
三、郵箱工具類
@Slf4j @Service public class MailUtils { @Resource RedisTemplate<String, Integer> redisTemplate; @Resource private JavaMailSenderImpl mailSender; public void sendMail(Mail mail) throws MailSendException { try { MimeMessageHelper messageHelper = new MimeMessageHelper(mailSender.createMimeMessage(), true); if (mail.getFrom() == null || mail.getFrom().isEmpty()) { mail.setFrom("xxx"); } //郵件發(fā)信人 messageHelper.setFrom(mailSender.getUsername() + '(' + mail.getFrom() + ')'); //郵件收信人 messageHelper.setTo(mail.getTo().split(",")); //郵件主題 messageHelper.setSubject(mail.getSubject()); //郵件內(nèi)容 messageHelper.setText(mail.getText()); //發(fā)送郵件 mailSender.send(messageHelper.getMimeMessage()); } catch (Exception e) { log.warn("郵件發(fā)送失?。簕}", e.getMessage()); throw new MailSendException("郵件發(fā)送失敗:" + e.getMessage()); } } /** * 發(fā)送驗(yàn)證碼郵件 * * @param mail 郵件信息 */ public void sendVerificationCode(Mail mail) { // 判斷當(dāng)前待發(fā)送郵箱是否已經(jīng)有驗(yàn)證碼 String key = CODE_KEY_PREFIX + mail.getTo(); Integer code = redisTemplate.opsForValue().get(key); if (code != null) { throw new UserException("當(dāng)前郵箱已經(jīng)發(fā)送驗(yàn)證碼"); } // 生成隨機(jī) 6位驗(yàn)證碼 int idenCode = (int) ((Math.random() * 9 + 1) * 100000); mail.setSubject("xxx"); mail.setText("驗(yàn)證碼:" + idenCode); redisTemplate.opsForValue().set(key, idenCode, 60, TimeUnit.SECONDS); sendMail(mail); } }
四、代碼調(diào)用
4.1 發(fā)送郵箱
/** * 發(fā)送郵箱驗(yàn)證碼 * * @param email 用戶郵箱 * @return 發(fā)送結(jié)果 */ @PostMapping("send/code") public Result<?> sendUserMail(String email) { Mail mail = new Mail(); mail.setTo(email); mailUtils.sendVerificationCode(mail); return Result.ok(null, "發(fā)送成功"); }
4.2 用戶注冊(cè)
@Override public boolean userRegister(User user, Integer captcha) { //判斷用戶是否已經(jīng)注冊(cè) User exist = getOne(Wrappers.<User>lambdaQuery().eq(User::getMail, user.getMail())); if(exist!=null){ throw new UserException("用戶已注冊(cè)"); } // 判斷驗(yàn)證碼是否相同 String key = CODE_KEY_PREFIX + user.getMail(); Integer authcode = (Integer) redisTemplate.opsForValue().get(key); if (authcode == null) { throw new UserException("驗(yàn)證碼已過期,請(qǐng)重新發(fā)送"); } if (!authcode.equals(captcha)) { throw new UserException("驗(yàn)證碼錯(cuò)誤"); } return userMapper.insert(user)>0; }
總結(jié)
以上就是springboot實(shí)現(xiàn)郵箱驗(yàn)證碼登錄注冊(cè)的基本實(shí)現(xiàn),覺得有用的大家可以點(diǎn)點(diǎn)贊。
到此這篇關(guān)于SpringBoot-Mail工具的使用-實(shí)現(xiàn)郵箱驗(yàn)證碼登錄注冊(cè)的文章就介紹到這了,更多相關(guān)SpringBoot郵箱驗(yàn)證碼登錄注冊(cè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring通過ApplicationContext主動(dòng)獲取bean的方法講解
今天小編就為大家分享一篇關(guān)于Spring通過ApplicationContext主動(dòng)獲取bean的方法講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03Java遞歸算法詳解(動(dòng)力節(jié)點(diǎn)整理)
Java遞歸算法是基于Java語言實(shí)現(xiàn)的遞歸算法。遞歸算法對(duì)解決一大類問題很有效,它可以使算法簡(jiǎn)潔和易于理解。接下來通過本文給大家介紹Java遞歸算法相關(guān)知識(shí),感興趣的朋友一起學(xué)習(xí)吧2017-03-03java通過模擬post方式提交表單實(shí)現(xiàn)圖片上傳功能實(shí)例
這篇文章主要介紹了java通過模擬post方式提交表單實(shí)現(xiàn)圖片上傳功能實(shí)例,涉及Java針對(duì)表單的提交操作響應(yīng)及文件傳輸?shù)南嚓P(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11使用spring boot開發(fā)時(shí)java對(duì)象和Json對(duì)象轉(zhuǎn)換的問題
這篇文章主要介紹了使用spring boot開發(fā)時(shí)java對(duì)象和Json對(duì)象轉(zhuǎn)換的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03spring cloud consul使用ip注冊(cè)服務(wù)的方法示例
這篇文章主要介紹了spring cloud consul使用ip注冊(cè)服務(wù)的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03SSH框架網(wǎng)上商城項(xiàng)目第2戰(zhàn)之基本增刪查改、Service和Action的抽取
SSH框架網(wǎng)上商城項(xiàng)目第2戰(zhàn)之基本增刪查改、Service和Action的抽取,感興趣的小伙伴們可以參考一下2016-05-05Spring注解實(shí)現(xiàn)自動(dòng)裝配過程解析
這篇文章主要介紹了Spring注解實(shí)現(xiàn)自動(dòng)裝配過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03