RabbitMQ+Redis模拟手机验证码登录

依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

需要class RedisTemplateConf配置类

package com.ah.mq.sms;

import org.springframework.cache.annotation.*;
import org.springframework.context.annotation.*;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper; @Configuration
@EnableCaching
public class ConfRedisTemplate extends CachingConfigurerSupport {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
// 使用Jackson2JsonRedisSerializer来序列化/反序列化redis的value值
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(
Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL,
com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
// value
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); // 使用StringRedisSerializer来序列化/反序列化redis的key值
RedisSerializer<?> redisSerializer = new StringRedisSerializer();
// key
redisTemplate.setKeySerializer(redisSerializer);
redisTemplate.setHashKeySerializer(redisSerializer); redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}

RabbitMQ的配置类

package com.ah.mq.sms;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.*;
@Configuration
public class ConfigMQ {
static final String MQ_NAME = "sms";
@Bean
public Queue createQueue() {
// 创建一个命名消息队列
return new Queue(MQ_NAME);
}
}

业务类

package com.ah.mq.sms;

import java.util.*;
import java.util.concurrent.TimeUnit; import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*; @RestController
public class SmsLogin {
@Autowired
private AmqpTemplate mq;
@Autowired
private RedisTemplate<String, Object> redisTemplate; @GetMapping("/getSms/{tel}")
public Map<String, String> 获取手机验证码(@PathVariable String tel) {
// DUMMY:生成手机验证码
String code = "123456";
// 存入Redis
String key = "sms_" + tel;
long timeout = 1;
redisTemplate.opsForValue().set(key, code, timeout, TimeUnit.MINUTES);
// 发送到MQ
Map<String, String> map = new HashMap<>();
map.put("tel", tel);
map.put("code", code);
mq.convertAndSend(ConfigMQ.MQ_NAME, map);
return map;
// http://127.0.0.1:8080/getSms/18712345678
} @GetMapping("/login/{tel}/{code}")
public String 手机号_验证码登录(@PathVariable String tel, @PathVariable String code) {
// 获取Redis中的验证码
String sysCode = (String) redisTemplate.opsForValue().get("sms_" + tel);
// 判断
if (sysCode == null) {
return "No code in System!";
} else if (!sysCode.equalsIgnoreCase(code)) {
return "code Error!";
}
// DUMMY:登录操作,存Session、JWT等
return "login seccuss!";
// http://127.0.0.1:8080/login/18712345678/123456
}
} @Component
class SmsListener {
@RabbitListener(queues = ConfigMQ.MQ_NAME)
public void receive(Map<String, String> message) {
System.out.println("收到短信:" + message);
}
}

RabbitMQ+Redis模拟手机验证码登录的更多相关文章

  1. Spring Security 实现手机验证码登录

    思路:参考用户名密码登录过滤器链,重写认证和授权 示例如下(该篇示例以精简为主,演示主要实现功能,全面完整版会在以后的博文中发出): 由于涉及内容较多,建议先复制到本地工程中,然后在细细研究. 1. ...

  2. java通过HtmlUnit工具和J4L实现模拟带验证码登录

    1.HtmlUnit 1.1介绍 HtmlUnit是一个用java编写的无界面浏览器,建模html文档,通过API调用页面,填充表单,点击链接等等.如同正常浏览器一样操作.典型应用于测试以及从网页抓取 ...

  3. shiro整合shiro多验证登录(账号密码登录和使用手机验证码登录)

    1. 首先新建一个shiroConfig  shiro的配置类,代码如下: @Configuration是标识这个类是一个配置文件,在启动时会加载这个类里面的内容,这个配置文件的位置的一定一定一定不能 ...

  4. 使用redis进行手机验证码的验证、每天只能发送三次验证码 (redis安装在虚拟机linux系统中)

    文章目录 1.代码 2.测试结果 2.1.第一次发送 2.2.填写正确的验证码 2.3.填写错误的验证码 连续发送多次验证码 环境准备:虚拟机Linux系统,redis安装在虚拟机中. 前提条件:虚拟 ...

  5. vue实现短信验证码登录

    无论是移动端还是pc端登录或者注册界面都会见到手机验证码登录这个功能,输入手机号,得到验证码,最后先服务器发送请求,保存登录的信息,一个必不可少的功能 思路 1,先判断手机号和验证是否为空, 2,点击 ...

  6. SpringCloud微服务实战——搭建企业级开发框架(二十六):自定义扩展OAuth2实现短信验证码登录

    现在手机验证码登录似乎是每个网站必备的功能,OAuth2支持扩展自定义授权模式,前面介绍了如何在系统集成短信通知服务,这里我们进行OAuth2的授权模式自定义扩展,使系统支持短信验证码登录. 1.在g ...

  7. (转)request模拟知乎登录(无验证码机制

    原文:http://www.itnose.net/detail/6755805.html import request try: import cookielib #python2版本 except: ...

  8. request模拟知乎登录(无验证码机制)

    import request try: import cookielib #python2版本 except: import http.cookiejar as cookielib #python3版 ...

  9. SpringBoot + Spring Security 学习笔记(五)实现短信验证码+登录功能

    在 Spring Security 中基于表单的认证模式,默认就是密码帐号登录认证,那么对于短信验证码+登录的方式,Spring Security 没有现成的接口可以使用,所以需要自己的封装一个类似的 ...

随机推荐

  1. SpringBoot第二集:注解与配置(2020最新最易懂)

    2020最新SpringBoot第二集:基础注解/基础配置(2020最新最易懂) 一.Eclipse安装SpringBoot插件 Eclipse实现SpringBoot开发,为便于项目的快速构建,需要 ...

  2. cenos7 u disk install

    分类:             其他              2014-08-24 13:53     3406人阅读     评论(0)     收藏     举报 CentOS安装教程操作系统 ...

  3. iptables基础原理和使用简介

    概念简介 名称 Netfilter/iptables模块有两部分组成: Netfilter框架以及iptables,iptables又分为iptables(内核空间)和iptables命令行工具(用户 ...

  4. Spark集群和任务执行

    [前言:承接<Spark通识>篇] Spark集群组件 Spark是典型的Master/Slave架构,集群主要包括以下4个组件: Driver:Spark框架中的驱动器,运行用户编写Ap ...

  5. js练习题之查找数组中的位子

    输出描述: 如果数组中存在 item,则返回元素在数组中的位置,否则返回 -1 输入例子: indexOf([ 1, 2, 3, 4 ], 3) 输出例子: 2 function indexOf(ar ...

  6. C# 字符串处理类

    using System;using System.Collections.Generic;using System.Text;using System.Text.RegularExpressions ...

  7. 云计算之路-出海记:命令行下的 AWS

    俗话说"三百六十行,行行出状元",自从有了电脑之后,三百六十行又多了一行 -- 命令行.GUI 的诞生开创了繁荣的 PC "窗口"(windows)时代,互联网 ...

  8. File 方法

    File类说明 存储在变量,数组和对象中的数据是暂时的,当程序终止时他们就会丢失.为了能够永 久的保存程序中创建的数据,需要将他们存储到硬盘或光盘的文件中.这些文件可以移动,传送,亦可以被其他程序使用 ...

  9. Spider_基础总结4_bs.find_all()与正则及lambda表达式

    # beautifulsoup的 find()及find_all()方法,也会经常和正则表达式以及 Lambda表达式结合在一起使用: # 1-bs.find_all()与正则表达式的应用: # 语法 ...

  10. binary hacks读数笔记(装载)

    1.地址空间 在linux系统中,每个进程拥有自己独立的虚拟地址空间,这个虚拟地址空间的大小是由计算机硬件决定的,具体地说,是由CPU的位数决定的.比如,32位硬件平台决定的虚拟地址空间大小:0--2 ...