RabbitMQ+Redis模拟手机验证码登录
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了RabbitMQ+Redis模拟手机验证码登录,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3866字,纯文字阅读大概需要6分钟。
内容图文
![RabbitMQ+Redis模拟手机验证码登录](/upload/InfoBanner/zyjiaocheng/873/d2ed37566a5d42b9ae969a32a9f3015c.jpg)
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模拟手机验证码登录全部内容,希望文章能够帮你解决RabbitMQ+Redis模拟手机验证码登录所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。