zoukankan      html  css  js  c++  java
  • 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);
    	}
    }
    
  • 相关阅读:
    jquery获得url的get参数
    WampServer更改或重置数据库密码
    phpexcel乱码问题
    5kcrm增加权限管理中的模块(签到统计)
    windows关于定时执行的php脚本
    php 中引入邮箱服务 , 利用第三方的smtp邮件服务
    thinkphp 多个字段的不同关系的查询条件实现 .
    redis与memcached有什么区别
    MYSQL语句大全
    使用HttpClient工具类发起Restful API调用
  • 原文地址:https://www.cnblogs.com/tigerlion/p/13042608.html
Copyright © 2011-2022 走看看