Redis实现短信验证码登录的示例代码
目录
- 效果图
- pom.xml
- applicatoin.yml
- Redis配置类
- controller
- serviceImpl
- mapper
效果图
发送验证码
输入手机号、密码以及验证码完成登录操作
pom.xml
核心依赖
<dependencies> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.22</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency> </dependencies>
applicatoin.yml
server: port: 8080 spring: application: name: redis-lettuce datasource: url: jdbc:mysql://aliyun-rds.mysql.rds.aliyuncs.com/illness?useSSL=false username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver # useSSL : 一般通过证书或者令牌进行安全认证,否则通过账号和密码进行连接 redis: database: 0 #Redis索引0~15,默认为0 host: ip port: 6379 password: 123456 #密码(默认为空) lettuce: # 这里标明使用lettuce配置 pool: max-active: 8 #连接池最大连接数(使用负值表示没有限制) max-wait: -1ms #连接池最大阻塞等待时间(使用负值表示没有限制) max-idle: 5 #连接池中的最大空闲连接 min-idle: 0 #连接池中的最小空闲连接 timeout: 10000ms #连接超时时间(毫秒)
Redis配置类
/** * Redis配置类 * * @author issavior */ @Configuration public class RedisConf { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { // 创建Template RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); // 设置连接工厂 redisTemplate.setConnectionFactory(redisConnectionFactory); // 设置序列化工具 GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer(); // key和 hashKey采用 string序列化 redisTemplate.setKeySerializer(RedisSerializer.string()); redisTemplate.setHashKeySerializer(RedisSerializer.string()); // value和 hashValue采用 JSON序列化 redisTemplate.setValueSerializer(jsonRedisSerializer); redisTemplate.setHashValueSerializer(jsonRedisSerializer); return redisTemplate; } }
controller
/** * @author issavior */ @RestController @RequestMapping("/user") public class UserController { /** * userService */ @Autowired private UserService userService; /** * 登录 * * @param issa issa * @return ResponseEntity<Issa> */ @PostMapping("/login") public ResponseEntity<Object> login(@RequestBody Issa issa) { return userService.login(issa); } /** * 获取验证码 * * @param phone phone * @return ResponseEntity<Object> */ @GetMapping("/{phone}") public ResponseEntity<Object> verificationCode(@PathVariable String phone) { return ResponseEntity.ok(userService.verificationCode(phone)); } /** * 注册 * * @param issa issa * @return ResponseEntity<Object> */ @PostMapping public ResponseEntity<Object> register(@RequestBody Issa issa) { issa.setId(UUID.randomUUID().toString(true)); return userService.register(issa); }
serviceImpl
/** * @author issavior */ @Service @Slf4j public class UserServiceImpl implements UserService { /** * redisTemplate */ @Autowired private RedisTemplate<String, Object> redisTemplate; /** * userMapper */ @Autowired private UserMapper userMapper; /** * 登录 * * @param issa 登录的参数 * @return ResponseEntity<Object> */ @Override public ResponseEntity<Object> login(Issa issa) { Issa user = userMapper.getUser(issa); if (user == null) { return ResponseEntity.status(400).body("手机号或密码错误"); } String phone = issa.getPhone(); String verificationCode = (String) redisTemplate.opsForValue().get("login:" + phone); String verifyCode = issa.getVerifyCode(); if (!Objects.equals(verifyCode, verificationCode)) { return ResponseEntity.status(400).body("请输入正确的验证码"); } return ResponseEntity.ok("登录成功"); } /** * 注册 * * @param issa 注册的参数 * @return ResponseEntity<Object> */ @Override public ResponseEntity<Object> register(Issa issa) { int user = userMapper.insertUser(issa); if (user != 1) { return ResponseEntity.status(400).body("注册失败"); } return ResponseEntity.ok("注册成功"); } /** * 获取验证码 * * @param phone 手机号 * @return Object */ @Override public Object verificationCode(String phone) { String randomCode = RandomUtil.randomNumbers(6); redisTemplate.opsForValue().set("login:" + phone, randomCode); log.info("验证码已经存入进redis服务器中:" + randomCode); String code = (String) redisTemplate.opsForValue().get("login:" + phone); if (code == null) { return "验证码获取失败"; } return "验证码获取成功【 " + code + " 】"; } }
mapper
/** * @author issavior */ public interface UserMapper { /** * 新增用户&注册 * * @param issa 参数 * @return int:新增成功返回1,否则返回0 */ @Insert("insert into issa(id,user_name,password,nick_name,sex,age,phone) values (#{id},#{userName},#{password},#{nickName},#{sex},#{age},#{phone})") int insertUser(Issa issa); /** * 用于登录时,根据手机号和密码判断是否有相关用户 * * @param issa issa * @return int */ @Select("select * from issa where phone = #{phone} and password = #{password}") Issa getUser(Issa issa); }
以上就是Redis实现短信验证码登录的示例代码的详细内容,更多关于Redis短信验证码登录的资料请关注我们其它相关文章!
赞 (0)