RabbitMQ+Redis模拟手机验证码登录
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模拟手机验证码登录的更多相关文章
- Spring Security 实现手机验证码登录
思路:参考用户名密码登录过滤器链,重写认证和授权 示例如下(该篇示例以精简为主,演示主要实现功能,全面完整版会在以后的博文中发出): 由于涉及内容较多,建议先复制到本地工程中,然后在细细研究. 1. ...
- java通过HtmlUnit工具和J4L实现模拟带验证码登录
1.HtmlUnit 1.1介绍 HtmlUnit是一个用java编写的无界面浏览器,建模html文档,通过API调用页面,填充表单,点击链接等等.如同正常浏览器一样操作.典型应用于测试以及从网页抓取 ...
- shiro整合shiro多验证登录(账号密码登录和使用手机验证码登录)
1. 首先新建一个shiroConfig shiro的配置类,代码如下: @Configuration是标识这个类是一个配置文件,在启动时会加载这个类里面的内容,这个配置文件的位置的一定一定一定不能 ...
- 使用redis进行手机验证码的验证、每天只能发送三次验证码 (redis安装在虚拟机linux系统中)
文章目录 1.代码 2.测试结果 2.1.第一次发送 2.2.填写正确的验证码 2.3.填写错误的验证码 连续发送多次验证码 环境准备:虚拟机Linux系统,redis安装在虚拟机中. 前提条件:虚拟 ...
- vue实现短信验证码登录
无论是移动端还是pc端登录或者注册界面都会见到手机验证码登录这个功能,输入手机号,得到验证码,最后先服务器发送请求,保存登录的信息,一个必不可少的功能 思路 1,先判断手机号和验证是否为空, 2,点击 ...
- SpringCloud微服务实战——搭建企业级开发框架(二十六):自定义扩展OAuth2实现短信验证码登录
现在手机验证码登录似乎是每个网站必备的功能,OAuth2支持扩展自定义授权模式,前面介绍了如何在系统集成短信通知服务,这里我们进行OAuth2的授权模式自定义扩展,使系统支持短信验证码登录. 1.在g ...
- (转)request模拟知乎登录(无验证码机制
原文:http://www.itnose.net/detail/6755805.html import request try: import cookielib #python2版本 except: ...
- request模拟知乎登录(无验证码机制)
import request try: import cookielib #python2版本 except: import http.cookiejar as cookielib #python3版 ...
- SpringBoot + Spring Security 学习笔记(五)实现短信验证码+登录功能
在 Spring Security 中基于表单的认证模式,默认就是密码帐号登录认证,那么对于短信验证码+登录的方式,Spring Security 没有现成的接口可以使用,所以需要自己的封装一个类似的 ...
随机推荐
- window.open浏览器弹出新窗口被拦截—原因分析和解决方案
最近在做项目的时候碰到了使用window.open被浏览器拦截的情况,在本机实验没问题,到了服务器就被拦截了,火狐有拦截提示,360浏览器拦截提示都没有,虽然在自己的环境可以对页面进行放行,但是对用户 ...
- 联赛模拟测试25 C. Repulsed 贪心+树形DP
题目描述 分析 考虑自底向上贪心 \(f[x][k]\) 表示 \(x\) 下面距离为 \(k\) 的需要灭火器的房间数,\(g[x][k]\) 表示 \(x\) 下面距离为 \(k\) 的多余灭火器 ...
- 在VirtualBox中调整Raspbian分辨率
参考自一路阳光随行发表的virtualBox设置虚拟机分辨率大小中ubuntu虚拟机分辨率的设置方法. 启动Raspbian虚拟机,点击 窗口主菜单里的设备->安装增强功能.系统后会自动挂载增强 ...
- 利用transformer进行中文文本分类(数据集是复旦中文语料)
利用TfidfVectorizer进行中文文本分类(数据集是复旦中文语料) 利用RNN进行中文文本分类(数据集是复旦中文语料) 利用CNN进行中文文本分类(数据集是复旦中文语料) 和之前介绍的不同,重 ...
- Netty源码解析 -- 事件循环机制实现原理
本文主要分享Netty中事件循环机制的实现. 源码分析基于Netty 4.1 EventLoop 前面分享服务端和客户端启动过程的文章中说过,Netty通过事件循环机制(EventLoop)处理IO事 ...
- K8s之实践Pod深入理解
K8s之实践Pod深入理解 1.同一pod下的nginx+php+mysql nginx+php+mysql.yaml文件 --- apiVersion: v1 kind: Secret meta ...
- linux + MongoDB 安装 + 部署 + 讲解 (满满干货看完记得收藏噢)
话不多说开始了! 安装 安装就依据菜鸟教程的进行安装 传送门 => https://www.runoob.com/mongodb/mongodb-linux-install.html 好啦!现在 ...
- Sql 解析XML 解决方案
1. 1.@XML 为数据传入的XML格式 2. root 为根目录 3. <A>为对应需要插入的表,详见一对多或者多对多的xml格式 ...
- 13 SOAP
13 SOAP SOAP(原为Simple Object Access Protocol的首字母缩写,即简单对象访问协议)是交换数据的一种协议规范,使用在计算机网络Web服务(web service) ...
- linux nf_conntrack 连接跟踪机制
PRE_ROUTING和LOCAL_OUT点可以看作是整个netfilter的入口,而POST_ROUTING和LOCAL_IN可以看作是其出口; 报文到本地:PRE_ROUTING----LOCAL ...