找回密码
 立即注册
查看: 11|回复: 0

5,阿里云短信和redis部署

[复制链接]

180

主题

5

精华

184

金币

技术维护QQ:515138

积分
393
发表于 昨天 16:41 | 显示全部楼层 |阅读模式
阿里云短信:
1,依赖:
  1. <dependency>
  2.   <groupId>com.aliyun</groupId>
  3.   <artifactId>dysmsapi20170525</artifactId>
  4.   <version>4.4.0</version>
  5. </dependency>
复制代码
2,引用工具类

3,调用接口发送验证码

Redis部署:
1,redis依赖
  1. <dependency>
  2.             <groupId>org.springframework.boot</groupId>
  3.             <artifactId>spring-boot-starter-data-redis</artifactId>
  4.         </dependency>
复制代码
2,配置文件config/RedisConfig
  1. package com.jinbaozi.common.config;
  2. import jakarta.annotation.Resource;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.data.redis.connection.RedisConnectionFactory;
  6. import org.springframework.data.redis.core.RedisTemplate;
  7. import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
  8. import org.springframework.data.redis.serializer.StringRedisSerializer;
  9. //不配置也行,但是中午的时候可能会乱码
  10. @Configuration
  11. public class RedisConfig {
  12.     @Resource
  13.     public RedisConnectionFactory connectionFactory;
  14.     @Bean
  15.     public RedisTemplate redisTemplate(){
  16.         RedisTemplate<String, Object> template = new RedisTemplate<>();
  17.         template.setConnectionFactory(connectionFactory);
  18.         template.setKeySerializer(new StringRedisSerializer());
  19.         template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
  20.         return template;
  21.     }
  22. }
复制代码
完整应用如下:
  1. package com.jinbaozi.user.service.impl;
  2. import com.jinbaozi.common.utils.AliyunSmsUtil;
  3. import com.jinbaozi.user.entity.User;
  4. import com.jinbaozi.user.mapper.UserMapper;
  5. import com.jinbaozi.user.service.IUserService;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.data.redis.core.RedisTemplate;
  9. import org.springframework.stereotype.Service;
  10. import java.util.Random;
  11. import java.util.concurrent.TimeUnit;
  12. /**
  13. * <p>
  14. *  服务实现类
  15. * </p>
  16. *
  17. * @author jinbaozi.com
  18. * @since 2026-02-02
  19. */
  20. @Service
  21. public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
  22.     @Autowired
  23.     private AliyunSmsUtil aliyunSmsUtil;
  24.     @Autowired
  25.     private RedisTemplate<String, Object> redisTemplate;
  26.     /**
  27.      * 根据用户名查询用户数量
  28.      *
  29.      * @param username
  30.      * @return
  31.      */
  32.     @Override
  33.     public Integer countUserByName(String username) {
  34.         return this.baseMapper.countUserByName(username);
  35.     }
  36.     /**
  37.      * 获取验证码的key
  38.      *
  39.      * @param phone
  40.      * @param code
  41.      * @return
  42.      */
  43.     private String getCaptchaKey(String phone,String code){
  44.         return "captcha:" + phone + "-" + code;
  45.     }
  46.     /**
  47.      * 发送短信
  48.      *
  49.      * @param phone
  50.      */
  51.     @Override
  52.     public void sendSms(String phone) {
  53.         // 生成4位随机数,生成一个0到9999之间的随机整数,并通过格式化确保其为4位数(不足4位时前面补0)
  54.         Random random = new Random();
  55.         String code = String.format("%04d",random.nextInt(10000));
  56.         // 发送验证码
  57.         aliyunSmsUtil.sendSms(phone, code);
  58.         log.debug("-----------> 发送验证码:" + code);
  59.         // 存入redis
  60.         redisTemplate.opsForValue().set(getCaptchaKey(phone,code),phone, 5, TimeUnit.MINUTES);
  61.     }
  62.     /**
  63.      * 校验验证码
  64.      *
  65.      * @param phone
  66.      * @param code
  67.      * @return
  68.      */
  69.     @Override
  70.     public boolean checkCapthcha(String phone, String code) {
  71.         String key = getCaptchaKey(phone,code);
  72.         Object obj = redisTemplate.opsForValue().get(key);
  73.         return obj!=null ? true : false;
  74.     }
  75. }
复制代码



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
网站建设,公众号小程序开发,系统定制,软件App开发,技术维护【联系我们】手机/微信:17817817816 QQ:515138

QQ|Archiver|自丢网 ( 粤ICP备2024252464号-1 )

GMT+8, 2026-2-4 09:02

专注于网站建设,公众号小程序制作,商城小程序,系统定制,软件App开发

【联系我们】手机/微信:17817817816 QQ:515138

快速回复 返回顶部 返回列表