Springboot實(shí)現(xiàn)發(fā)送郵件及注冊(cè)激活步驟
在上一篇文章中,我們使用springboot整合郵件實(shí)現(xiàn)發(fā)送功能,那么下面來用郵件發(fā)送功能實(shí)現(xiàn)用戶注冊(cè),方便我們進(jìn)一步熟悉郵件發(fā)送功能的使用。實(shí)現(xiàn)步驟大致如下:進(jìn)行用戶注冊(cè)同時(shí)發(fā)送一封激活郵件,郵件里面包含一條激活鏈接,點(diǎn)擊鏈接把使用UUIDUtils生產(chǎn)發(fā)送給用戶的郵箱驗(yàn)證碼提交進(jìn)行驗(yàn)證,從而修改用戶的激活狀態(tài),最后返回登陸頁面進(jìn)行驗(yàn)證登錄。
一.效果演示
輸入郵箱等一系列信息,點(diǎn)擊注冊(cè),系統(tǒng)自動(dòng)發(fā)送激活郵件到你綁定的郵箱地址。同時(shí)數(shù)據(jù)庫里面增加一條用戶信息,其中用戶狀態(tài)(status)默認(rèn)值為0,需要進(jìn)行激活改變狀態(tài)才能授權(quán)登錄。
如下,打開自己綁定的郵箱點(diǎn)擊下面的激活鏈接,激活成功后跳轉(zhuǎn)到登錄頁面
點(diǎn)擊郵件激活鏈接的時(shí)候,驗(yàn)證鏈接中的激活碼與數(shù)據(jù)庫的code值是否一致,若驗(yàn)證成功則清除數(shù)據(jù)庫中code的值,同時(shí)改變用戶狀態(tài)(即status的值由0變?yōu)?)。
二.編程實(shí)現(xiàn)步驟
1.實(shí)現(xiàn)郵件發(fā)送服務(wù)
如何實(shí)現(xiàn)郵件發(fā)送服務(wù),可以參考我之前相關(guān)的文章:Springboot整合實(shí)現(xiàn)郵件發(fā)送
2.封裝實(shí)體類
使用注解的方式快速創(chuàng)建User實(shí)體類
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class User { private int UserID; //賬號(hào) private String UserNumber; //密碼 private String Password; //郵箱地址 private String Email; /** * 狀態(tài):0代表未激活,1代表激活 */ private Integer status; /** * 利用UUID生成一段數(shù)字,發(fā)動(dòng)到用戶郵箱,當(dāng)用戶點(diǎn)擊鏈接時(shí) * 在做一個(gè)校驗(yàn)如果用戶傳來的code跟我們發(fā)生的code一致,更改狀態(tài)為“1”來激活用戶 */ private String code; }
3.創(chuàng)建生成UUID的工具類
UUID產(chǎn)生的目的是讓分布式系統(tǒng)中的所有元素,都能有唯一的辨識(shí)信息,而不需要通過中央控制端來做辨識(shí)信息的指定。如此一來,每個(gè)人都可以創(chuàng)建不與其它人沖突的UUID。在這樣的情況下,就不需考慮數(shù)據(jù)庫創(chuàng)建時(shí)的名稱重復(fù)問題。UUID 來作為數(shù)據(jù)庫中字段是非常不錯(cuò)的選擇,保證每次生成的UUID 是唯一的
import java.util.UUID; /** * @Description:使用UUIDUtils生產(chǎn)發(fā)送給用戶的郵箱驗(yàn)證碼 */ public class UUIDUtils { public static String getUUID(){ return UUID.randomUUID().toString().replace("-",""); } }
4.創(chuàng)建Userdao接口
package com.kuang.Dao; import com.kuang.pojo.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; //@Mapper : 表示本類是一個(gè) MyBatis 的 Mapper @Mapper @Repository public interface UsersDao { //登錄 public User login(@Param("UserNumber") String UserNumber, @Param("Password") String Password); //找回密碼 public User retrieve(@Param("Email") String Email); /** * 用戶注冊(cè) * @param user */ void register(User user); /** * 根據(jù)激活碼查詢用戶,之后再進(jìn)行修改用戶狀態(tài) * @param code * @return */ User checkCode(String code); /** * 激活賬戶,修改用戶狀態(tài)為"1" * @param user */ void updateUserStatus(User user); }
5.創(chuàng)建映射文件UserMapper.xml
注意:在mybatis中映射文件中的namespace是用于綁定Dao接口的,即面向接口編程,dao接口的方法對(duì)應(yīng)mapper中的sql語名。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.kuang.Dao.UsersDao"> <resultMap type="com.kuang.pojo.User" id="User"> <id column="UserID" property="UserID"/> <result column="UserID" property="UserID"/> <result column="UserNumber" property="UserNumber"/> <result column="Password" property="Password"/> <result column="Email" property="Email"/> <result column="code" property="code"/> <result column="status" property="status"/> </resultMap> <select id="login" resultMap="User"> select * from user where UserNumber=#{UserNumber} and Password=#{Password} and status=1 </select> <select id="retrieve" resultMap="User"> select * from user where Email=#{Email} </select> <!--注冊(cè)用戶--> <insert id="register" parameterType="User" > insert into user ( UserNumber, Password,Email,status,code) values (#{UserNumber,jdbcType=VARCHAR}, #{Password,jdbcType=VARCHAR}, #{Email,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER},#{code,jdbcType=INTEGER}) </insert> <!--根據(jù)激活碼code查詢用戶--> <select id="checkCode" parameterType="String" resultType="User"> select * from user where code = #[code] </select> <!--激活賬戶,修改用戶狀態(tài)--> <update id="updateUserStatus" parameterType="User"> update user set status=1,code=null where UserID=#{UserID} </update> </mapper>
6.創(chuàng)建UserService接口
package com.kuang.service; import com.kuang.pojo.User; public interface UserService { //用戶登錄 public User login(String userName, String password); //找回密碼 public User retrieve(String email); /** * 用戶注冊(cè) * @param user */ void register(User user); /** * 根據(jù)激活碼code查詢用戶,之后再進(jìn)行修改狀態(tài) * @param code * @return */ User checkCode(String code); /** * 激活賬戶,修改用戶狀態(tài) * @param user */ void updateUserStatus(User user); }
7.創(chuàng)建UserServiceImpl實(shí)現(xiàn)類
注意:這里使用到的sendHtmlMail發(fā)送郵件方法,在相關(guān)的文章:Springboot整合實(shí)現(xiàn)郵件發(fā)送
package com.kuang.service.impl; import com.kuang.Dao.UsersDao; import com.kuang.pojo.User; import com.kuang.service.MailService; import com.kuang.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService { @Autowired private UsersDao usersDao; @Autowired private MailService mailService; /** * 登錄驗(yàn)證 */ @Override public User login(String userName, String password){ return usersDao.login(userName,password); } /** * 忘記密碼,郵件發(fā)送找回 */ @Override public User retrieve(String email){ return usersDao.retrieve(email); } /** * 用戶注冊(cè),同時(shí)發(fā)送一封激活郵件 * @param user */ @Override public void register(User user) { usersDao.register(user); String code = user.getCode(); System.out.println("code:"+code); String subject = "來自智能化駕校管理系統(tǒng)的激活郵件"; ///checkCode?code=xxx即是我們點(diǎn)擊郵件鏈接之后進(jìn)行更改狀態(tài)的 String context = "<h1>此郵件為官方激活郵件!請(qǐng)點(diǎn)擊下面鏈接完成激活操作!</h1> <a href=\"http://localhost:8080/selectCode?code="+code+"\">激活請(qǐng)點(diǎn)擊:"+code+"</a> "; //發(fā)送激活郵件 mailService.sendHtmlMail (user.getEmail(),subject,context); } /** * 根據(jù)激活碼code進(jìn)行查詢用戶,之后再進(jìn)行修改狀態(tài) * @param code * @return */ @Override public User checkCode(String code) { return usersDao.checkCode(code); } /** * 激活賬戶,修改用戶狀態(tài) * @param user */ @Override public void updateUserStatus(User user) { usersDao.updateUserStatus(user); } }
8.定義核心業(yè)務(wù)接口UserController控制類
package com.kuang.contorller; import com.kuang.common.UUIDUtils; import com.kuang.pojo.User; import com.kuang.service.UserService; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.thymeleaf.util.StringUtils; import javax.annotation.Resource; import javax.servlet.http.HttpSession; @Controller public class LoginContorller { @Resource private UserService userService; @RequestMapping("/main") public String login(@RequestParam("UserNumber") String UserNumber, @RequestParam("Password") String Password, Model model, HttpSession session) { //具體的業(yè)務(wù) User user= userService.login(UserNumber,Password); if (user!=null){ session.setAttribute("loginUser",UserNumber); return "redirect:/Main"; }else if(StringUtils.isEmpty(UserNumber) && StringUtils.isEmpty(Password)){ model.addAttribute("msg","賬號(hào)或者密碼不能為空"); return "Login"; } else { model.addAttribute("msg","賬號(hào)或者密碼錯(cuò)誤"); return "Login"; } } /** * 注冊(cè) * @param user * @return */ @RequestMapping(value = "/registerUser") public String register(User user){ user.setStatus(0); String code = UUIDUtils.getUUID()+ UUIDUtils.getUUID(); user.setCode(code); userService.register(user); return "login"; } /** *校驗(yàn)郵箱中的code激活賬戶 * 首先根據(jù)激活碼code查詢用戶,之后再把狀態(tài)修改為"1" */ @RequestMapping(value = "/selectCode") public String checkCode(String code){ User user = userService.checkCode(code); System.out.println(user); //如果用戶不等于null,把用戶狀態(tài)修改status=1 if (user !=null){ user.setStatus(1); //把code驗(yàn)證碼清空,已經(jīng)不需要了 user.setCode(""); System.out.println(user); userService.updateUserStatus(user); } return "login"; } /** * 跳轉(zhuǎn)到登錄頁面 * @return login */ @RequestMapping(value = "/loginPage") public String login(){ return "login"; } }
9.編寫頁面內(nèi)容
在我們的項(xiàng)目開發(fā)過程中我們需要向后端發(fā)送請(qǐng)求,實(shí)現(xiàn)前后端的數(shù)據(jù)交互,這里使用了Thymeleaf模板引擎+form的方法。后臺(tái)按照post的地址進(jìn)行接收即可,需要注意的是,提交數(shù)據(jù)的時(shí)候,button
或者input
都設(shè)置成為submit
<div id="signup-box" class="signup-box widget-box no-border"> <div class="widget-body"> <div class="widget-main"> <h4 class="header green lighter bigger"> <i class="icon-group blue"></i> <span id="beatText1">注冊(cè)新用戶</span> </h4> <div class="space-6"></div> <p> 請(qǐng)輸入你的信息: </p> <form th:action="@{/registerUser}" method="post"> <fieldset> <label> <span class="block input-icon input-icon-right"> <input type="email" class="span12" id="ZCEmail" name="Email" placeholder="郵箱"/> <i class="icon-envelope"></i> </span> </label> <label> <span class="block input-icon input-icon-right"> <input type="text" class="span12" id="ZCUserNumber" name="UserNumber" placeholder="賬號(hào)"/> <i class="icon-user"></i> </span> </label> <label> <span class="block input-icon input-icon-right"> <input type="password" class="span12" id="ZCPassword" name="Password" placeholder="密碼"/> <i class="icon-lock"></i> </span> </label> <label> <span class="block input-icon input-icon-right"> <input type="password" class="span12" placeholder="確認(rèn)密碼"/> <i class="icon-retweet"></i> </span> </label> <label> <input type="checkbox"/> <span class="lbl"> 我接受本 <a href="#" rel="external nofollow" rel="external nofollow" >用戶協(xié)議</a> </span> </label> <div class="space-24"></div> <div class="clearfix"> <button type="reset" class="width-30 pull-left btn btn-small"> <i class="icon-refresh"></i> 重置 </button> <button type="submit" class="width-65 pull-right btn btn-small btn-success"> 注冊(cè) <i class="icon-arrow-right icon-on-right"></i> </button> </div> </fieldset> </form> </div> <div class="toolbar center"> <a href="#" rel="external nofollow" rel="external nofollow" onclick="show_box('login-box'); return false;" class="back-to-login-link"> <i class="icon-arrow-left"></i> 返回登陸 </a> </div>
10.完整的項(xiàng)目部署
總結(jié):使用Springboot實(shí)現(xiàn)發(fā)送郵件注冊(cè)激活的步驟大致如此,需要更加完整的項(xiàng)目邏輯可以自己完善
到此這篇關(guān)于Springboot實(shí)現(xiàn)發(fā)送郵件及注冊(cè)激活步驟的文章就介紹到這了,更多相關(guān)Springboot發(fā)送郵件注冊(cè)激活內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?Boot使用MyBatis進(jìn)行兩個(gè)表的關(guān)聯(lián)
本文主要介紹了Spring?Boot使用MyBatis進(jìn)行兩個(gè)表的關(guān)聯(lián),通過實(shí)例演示了如何使用MyBatis的XML映射文件和注解實(shí)現(xiàn)關(guān)聯(lián)操作,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09SpringBoot項(xiàng)目創(chuàng)建使用+配置文件+日志文件詳解
Spring的出現(xiàn)是為了簡化 Java 程序開發(fā),而 SpringBoot 的出現(xiàn)是為了簡化 Spring 程序開發(fā),這篇文章主要介紹了SpringBoot項(xiàng)目創(chuàng)建使用+配置文件+日志文件,需要的朋友可以參考下2023-02-02Java的Synchronized關(guān)鍵字學(xué)習(xí)指南(全面 & 詳細(xì))
這篇文章主要給大家介紹了關(guān)于Java的Synchronized關(guān)鍵字的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03深入了解SpringBoot中@ControllerAdvice的介紹及三種用法
這篇文章主要為大家詳細(xì)介紹了SpringBoot中@ControllerAdvice的介紹及三種用法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-02-02logback.xml動(dòng)態(tài)配置程序路徑的操作
這篇文章主要介紹了logback.xml動(dòng)態(tài)配置程序路徑的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02Java實(shí)現(xiàn)LeetCode(組合總和)
這篇文章主要介紹了Java實(shí)現(xiàn)LeetCode(組合總數(shù)),本文通過使用java實(shí)現(xiàn)leetcode的組合總數(shù)題目和實(shí)現(xiàn)思路分析,需要的朋友可以參考下2021-06-06Scala實(shí)現(xiàn)冒泡排序、歸并排序和快速排序的示例代碼
這篇文章主要介紹了Scala實(shí)現(xiàn)冒泡排序、歸并排序和快速排序的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06Springboot整合MybatisPlus的實(shí)現(xiàn)過程解析
這篇文章主要介紹了Springboot整合MybatisPlus的實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10