最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot郵箱鏈接注冊驗證及注冊流程

 更新時間:2021年07月24日 09:34:07   作者:yuwenS  
這篇文章給大家介紹Spring Boot郵箱鏈接注冊驗證問題及注冊流程分析,通過實例代碼給大家分享實現(xiàn)過程,感興趣的朋友跟隨小編一起看看吧

簡單介紹

注冊流程
【1】前端提交注冊信息
【2】后端接受數(shù)據(jù)
【3】后端生成一個UUID做為token,將token作為redis的key值,用戶數(shù)據(jù)作為redis的value值,并設(shè)置key的時長
【4】后端根據(jù)用戶信息中的郵箱地址信息,檢驗用戶是否已經(jīng)注冊,如果沒有,生成注冊鏈接發(fā)送到用戶郵箱,如果已經(jīng)注冊,提示用戶該郵箱地址已被注冊
【5】用戶點擊郵件中的注冊鏈接
【6】后端判斷redis中token是否過期,沒有將用戶信息保存到數(shù)據(jù)庫,提示用戶注冊成功
項目源碼:https://gitee.com/residual-temperature/email-link-demo.git
郵箱效果圖

實現(xiàn)過程

1、pom文件要加入的jar包

   <!-- 郵件相關(guān) -->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-mail</artifactId>
       </dependency>

       <!-- redis相關(guān) -->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-data-redis</artifactId>
       </dependency>

2、application.yml文件中要加入的配置

spring:        
  redis:
    host:     # redis地址
    port: 6379   # redis端口號(默認(rèn)6379)
    password:     # redis密碼
  mail:
    host: smtp.qq.com    # 郵箱協(xié)議
    username: 地址          # 發(fā)送的郵箱地址
    password:  授權(quán)碼      # 郵箱的授權(quán)碼

3、定義實體類

@Repository
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
  private long id;
  private String account;
  private String password;
  private String username;
  
}

注意

此處沒有g(shù)et(),set()方法是因為導(dǎo)入了lombok包

4、redis的config配置
對象的保存需要序列化,所以需要自定義RedisTemplete

@Configuration
public class RedisConfig {
    //編寫自己的配置類
    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        //為了開發(fā)方便一般使用<String,Object>
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        //JSON序列化的配置
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        //String的序列化
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        //key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);

        //hash采用String的序列方式
        template.setHashKeySerializer(stringRedisSerializer);

        //value序列化采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);

        //hash的Value序列化采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}

5、驗證鏈接生成和郵箱發(fā)送的工具類CodeUtils的配置

@Component
public class CodeUtils {

    @Resource
    JavaMailSender mailSender;

    @Resource
    RedisTemplate<String, User> redisTemplate;

    // 生成鏈接,并給接收的郵箱發(fā)送郵件
    public boolean sendCode(User user){
        MimeMessage message = mailSender.createMimeMessage();
        try{
            MimeMessageHelper messageHelper = new MimeMessageHelper(message);
            String token = UUID.randomUUID().toString(); // 生成UUID
            redisTemplate.opsForValue().set(token,user);
            redisTemplate.expire(token,300, TimeUnit.SECONDS);
            messageHelper.setFrom("發(fā)送方的郵箱地址"); //發(fā)送方的郵箱地址,而不是接收方的郵箱地址
            messageHelper.setTo(user.getAddress()); // 接收方的郵箱地址
            messageHelper.setSubject("注冊");  // 郵箱標(biāo)題
            String html = "<html>\n" +
                    "<body>\n" +
                    "<p>請點擊下方鏈接注冊</p>\n" +
                    "<a href=\"http://localhost:8081/lookCode/"+token+"\">http://localhost:8081/lookCode/"+token+"</a>" +
                    "</body>\n" +
                    "</html>";
            messageHelper.setText(html,true); // 郵箱內(nèi)容
            mailSender.send(message);  // 發(fā)送郵箱
            System.out.println("發(fā)送成功");
            return true;
        }catch (Exception e){
            System.out.println("發(fā)送失敗");
            return false;
        }
    }

    // 判斷token是否過期
    public boolean eqToken(String token){
        return redisTemplate.hasKey(token);
    }

    // 根據(jù)token查詢用戶的信息
    public User findUser(String token){
        return redisTemplate.opsForValue().get(token);
    }

}

6、UserMapper的配置

@Mapper
@Repository
public interface UserMapper {

    // 添加用戶 注解開發(fā)sql語句
    @Insert("insert into user(account,password,username) values (#{account},#{password},#{username})")
    public int addUser(User user);

}

7、UserService的配置

public interface UserService {

    // 添加用戶
    public boolean adduser(User user);

    // 根據(jù)用戶注冊信息進(jìn)行注冊鏈接的的生成和發(fā)送
    public boolean sendCode(User user);

    // 用戶點擊注冊鏈接判斷token是否過期
    public boolean eqToken(String token);

}

8、UserService的實現(xiàn)類UserServiceImpl的配置

@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;

    @Resource
    CodeUtils codeUtils;

    /**
     * 添加注冊的用戶信息
     * @param user 注冊的用戶信息
     * @return 是否添加成功
     */
    @Override
    public boolean adduser(User user) {
        return userMapper.addUser(user) > 0;
    }

    /**
     * 生成鏈接和發(fā)送鏈接
     * @param address 接收的郵箱地址
     * @param user 注冊的用戶信息
     */
    @Override
    public boolean sendCode(User user) {
       if ( codeUtils.sendCode(user)) // 調(diào)用驗證鏈接生成工具類中的生成鏈接和發(fā)送郵件函數(shù)
           return true;
       else
           return false;
    }

    /**
     * 判斷token是否過期
     * @param token 用戶注冊所接收的token
     * @return 注冊成功與否
     */
    @Override
    public boolean eqToken(String token) {
        boolean flag = codeUtils.eqToken(token);

        if (flag){
            User user = codeUtils.findUser(token);
            adduser(user);
            return true;
        }else {
            return false;
        }
    }
}

9、UserController的配置

@RestController
public class UserController {

    @Resource
    UserService userService;

    // 根據(jù)用戶注冊信息進(jìn)行注冊鏈接的的生成和發(fā)送
    @PostMapping("/sendCode")
    public Map<String,String> sendCode(@RequestBody User user){
        boolean flag = userService.sendCode(user);
        Map<String,String> map = new HashMap<>();
        if (flag){
            map.put("msg","郵件發(fā)送成功,請前往您的郵箱進(jìn)行注冊驗證");
            return map;
        }else {
            map.put("msg","郵件發(fā)送失敗");
            return map;
        }
    }

    // 判斷是否注冊成功
    @GetMapping("/lookCode/{token}")
    public Map<String,String> lookCode(@PathVariable("token")String token){
        boolean flag = userService.eqToken(token);
        Map<String,String> map = new HashMap<>();
        if (flag){
            map.put("msg","注冊成功");
            /* 后續(xù)的操作 ... ...*/
            return map;
        }else {
            map.put("msg","注冊碼過期,請重新注冊");
            return map;
        }
    }
}

因為沒有寫前端頁面,所以就用postman和頁面來演示

postman測試

傳入user對象

返回結(jié)果

郵箱鏈接

點擊注冊鏈接之后

注冊成功之后數(shù)據(jù)庫前后對比

注冊成功之前

注冊成功之后

總結(jié)

可能會遇到的問題
【1】有些內(nèi)部網(wǎng)絡(luò)不支持發(fā)送郵箱,如果保證代碼沒錯,可以換個網(wǎng)絡(luò)試試
【2】如果是在本地測試,連接的是本地redis,記得開啟本地的redis

到此這篇關(guān)于Spring Boot郵箱鏈接注冊驗證及注冊流程的文章就介紹到這了,更多相關(guān)Spring Boot郵箱注冊驗證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • springboot讀取resources下文件的方式詳解

    springboot讀取resources下文件的方式詳解

    最近寫讀取模板文件做一些后續(xù)的處理,將文件放在了項目的resources下,發(fā)現(xiàn)了一個好用的讀取方法,下面這篇文章主要給大家介紹了關(guān)于springboot讀取resources下文件的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • SpringBoot如何取消內(nèi)置Tomcat啟動并改用外接Tomcat

    SpringBoot如何取消內(nèi)置Tomcat啟動并改用外接Tomcat

    這篇文章主要介紹了SpringBoot如何取消內(nèi)置Tomcat啟動并改用外接Tomcat,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • Springboot測試類沒有bean注入問題解析

    Springboot測試類沒有bean注入問題解析

    這篇文章主要介紹了Springboot測試類沒有bean注入問題解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • 一道關(guān)于java異常處理的題目

    一道關(guān)于java異常處理的題目

    本文給大家分享一道關(guān)于java異常處理的題目,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • 一次mybatis連接查詢遇到的坑實戰(zhàn)記錄

    一次mybatis連接查詢遇到的坑實戰(zhàn)記錄

    這篇文章主要給大家介紹了關(guān)于一次mybatis連接查詢遇到的坑的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Java 求解如何把二叉搜索樹轉(zhuǎn)換為累加樹

    Java 求解如何把二叉搜索樹轉(zhuǎn)換為累加樹

    這篇文章主要介紹了Java 求解把二叉搜索樹轉(zhuǎn)換為累加樹的代碼,總之需要觀察示例節(jié)點的規(guī)律,需要記錄上個節(jié)點的情況,注意引入前驅(qū)節(jié)點pre,具體實例代碼跟隨小編一起看看吧
    2021-11-11
  • 詳解springboot解決CORS跨域的三種方式

    詳解springboot解決CORS跨域的三種方式

    本文主要介紹了詳解springboot解決CORS跨域的三種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Spring中多配置文件及引用其他bean的方式

    Spring中多配置文件及引用其他bean的方式

    本文給大家介紹spring中多配置文件及引用其他bean的方式,涉及到spring配置文件的相關(guān)知識,感興趣的朋友一起學(xué)習(xí)吧
    2016-03-03
  • Java中的LinkedBlockingQueue源碼解析

    Java中的LinkedBlockingQueue源碼解析

    這篇文章主要介紹了Java中的LinkedBlockingQueue源碼解析,LinkedBlockingQueue底層是一個鏈表(可以指定容量,默認(rèn)是Integer.MAX_VALUE),維持了兩把鎖,一把鎖用于入隊,一把鎖用于出隊,并且使用一個AtomicInterger類型的變量保證線程安全,需要的朋友可以參考下
    2023-12-12
  • JAVA使用JDBC技術(shù)操作SqlServer數(shù)據(jù)庫實例代碼

    JAVA使用JDBC技術(shù)操作SqlServer數(shù)據(jù)庫實例代碼

    本篇文章主要介紹了JAVA使用JDBC技術(shù)操作SqlServer數(shù)據(jù)庫實例代碼,具有一定的參考價值,有興趣的可以了解一下。
    2017-01-01

最新評論

汕尾市| 荥经县| 南和县| 永泰县| 宽甸| 神木县| 沙田区| 永济市| 岢岚县| 栖霞市| 哈巴河县| 左贡县| 石城县| 华坪县| 瓦房店市| 福建省| 缙云县| 安化县| 达孜县| 襄汾县| 阿图什市| 平度市| 信丰县| 宁远县| 丽水市| 北宁市| 满城县| 环江| 出国| 曲靖市| 横峰县| 和政县| 宜良县| 集贤县| 勐海县| 望奎县| 泸水县| 宁国市| 从化市| 浑源县| 车险|