springboot中通过发送验证码注册用户信息

2021-04-10 23:26

阅读:718

YPE html>

标签:new   public   current   set   验证   bsp   sel   pass   jquer   

1)、先写一个注册页面和登录页面;

register.html

"en" xmlns:th="http://www.thymeleaf.org">"UTF-8">
    用户注册
"@{/register}" method="post">
"${msg}">
账号:"text" name="account">
邮箱:"email" name="email">
验证码:"text" name="code">
密码:"password" name="password">
手机号:"text" name="phone">
"submit" value="注册">

login.html;

"en" xmlns:th="http://www.thymeleaf.org">
"UTF-8">
    登录页面
"@{/register}" method="post"> 用户名:"text" name="account" />
密码:"password" name="password" />
"submit" value="登录"/>

2)写一个rabbitmq配置类;

package com.seecen.redis.rabbitmq;



import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述:
 *   rabbitmq 配置类
 *   1.配置交换机
 *   2.配置队列
 *   3.配置队列与交换机绑定关系
 *   4.配置RabbitTemplate
 */
@Configuration
public class RabbitConfig {
    //注入连接工厂
    @Autowired
    private ConnectionFactory connectionFactory;
    /**
     * 配置rabbitTemplate
     * @return
     */
    @Bean
    public RabbitTemplate rabbitTemplate(){
        RabbitTemplate rabbitTemplate =
                new RabbitTemplate(connectionFactory);
        return rabbitTemplate;
    }
    /**
     * 定义队列
     * @return
     */
    @Bean
    public Queue emailQueue(){
        return new Queue("email.verifyCode");
    }
    /**
     * 定义交换机
     * @return
     */
    @Bean
    public DirectExchange emailExchange(){
        return new DirectExchange("email.exchange");
    }

    /**
     * 绑定队列到交换机
     * @return
     */
    @Bean
    public Binding emailVerifyCodeBinding(){

        return BindingBuilder.bind(emailQueue())//绑定队列
                .to(emailExchange())//指定交换机
                .with("email.verifyCode");//路由规则
    }
}

3)在控制层写一个发送验证码的方法和登录注册的方法;

AdminController.java

package com.seecen.redis.controller;

import com.seecen.redis.entity.TAdmin;
import com.seecen.redis.service.AdminService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/sendCode")
    public Map sendCode(String email){
        String key="verifyCode:" + email;
        Boolean exists = redisTemplate.hasKey(key);
        String code;
        if (exists){
            code = (String) redisTemplate.opsForValue().get(key);
        }else{
            Random random = new Random();
            code=random.nextInt(10000)+"";
            //将验证码放入redis缓存中,并设定超时时间为5分钟
            redisTemplate.opsForValue().set(key,code,5, TimeUnit.MINUTES);
            HashMap msg = new HashMap();
            msg.put("email",email);
            msg.put("code",code);
            //发送消息
            rabbitTemplate.convertAndSend(
                    "email.exchange",//交换机
                    "email.verifyCode",//路由key
                    msg);//消息内容
        }
        HashMap result = new HashMap();
        result.put("code",200);
        result.put("msg","验证码发送成功!");
        return result;
    }
}

indexController.javapackage com.seecen.redis.controller;


import com.seecen.redis.aop.Log;
import com.seecen.redis.aop.LogType;
import com.seecen.redis.entity.TAdmin;
import com.seecen.redis.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author bigpeng
 * @create 2020-07-21-16:18
 */
@Controller
@Slf4j
public class IndexController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private AdminService adminService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/toRegister")
    public String toRegister(){
        return "register.html";
    }
@PostMapping("/register") public String register(TAdmin tAdmin, String code, ModelMap modelMap){ //1.验证验证码是否正确 String cachedCode = (String) redisTemplate.opsForValue().get("verifyCode:" + tAdmin.getEmail()); if(!code.equals(cachedCode)){ modelMap.put("msg","验证码错误"); return "register.html"; } //2.验证用户名是否存在 TAdmin tAdmin1 = adminService.selectByAccount(tAdmin.getAccount()); if(tAdmin1!=null){ modelMap.put("msg","用户名已存在!"); return "register.html"; } //3、保存用户信息 adminService.insert(tAdmin); //4、将验证码删除 redisTemplate.delete("verifyCode:" + tAdmin.getEmail()); return "login.html"; } }

4)测试,可以在数据库中查看是否注册成功;

 

springboot中通过发送验证码注册用户信息

标签:new   public   current   set   验证   bsp   sel   pass   jquer   

原文地址:https://www.cnblogs.com/xie-qi/p/13363979.html


评论


亲,登录后才可以留言!