阿里云短信:
1,依赖:
- <dependency>
- <groupId>com.aliyun</groupId>
- <artifactId>dysmsapi20170525</artifactId>
- <version>4.4.0</version>
- </dependency>
复制代码 2,引用工具类
3,调用接口发送验证码
Redis部署:
1,redis依赖
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-redis</artifactId>
- </dependency>
复制代码 2,配置文件config/RedisConfig
- package com.jinbaozi.common.config;
-
- import jakarta.annotation.Resource;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.data.redis.connection.RedisConnectionFactory;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
- import org.springframework.data.redis.serializer.StringRedisSerializer;
- //不配置也行,但是中午的时候可能会乱码
- @Configuration
- public class RedisConfig {
- @Resource
- public RedisConnectionFactory connectionFactory;
-
- @Bean
- public RedisTemplate redisTemplate(){
- RedisTemplate<String, Object> template = new RedisTemplate<>();
-
- template.setConnectionFactory(connectionFactory);
-
- template.setKeySerializer(new StringRedisSerializer());
- template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
-
-
- return template;
- }
- }
复制代码 完整应用如下:
- package com.jinbaozi.user.service.impl;
-
- import com.jinbaozi.common.utils.AliyunSmsUtil;
- import com.jinbaozi.user.entity.User;
- import com.jinbaozi.user.mapper.UserMapper;
- import com.jinbaozi.user.service.IUserService;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.stereotype.Service;
-
- import java.util.Random;
- import java.util.concurrent.TimeUnit;
-
- /**
- * <p>
- * 服务实现类
- * </p>
- *
- * @author jinbaozi.com
- * @since 2026-02-02
- */
- @Service
- public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
- @Autowired
- private AliyunSmsUtil aliyunSmsUtil;
- @Autowired
- private RedisTemplate<String, Object> redisTemplate;
-
- /**
- * 根据用户名查询用户数量
- *
- * @param username
- * @return
- */
- @Override
- public Integer countUserByName(String username) {
- return this.baseMapper.countUserByName(username);
- }
- /**
- * 获取验证码的key
- *
- * @param phone
- * @param code
- * @return
- */
- private String getCaptchaKey(String phone,String code){
- return "captcha:" + phone + "-" + code;
- }
- /**
- * 发送短信
- *
- * @param phone
- */
- @Override
- public void sendSms(String phone) {
- // 生成4位随机数,生成一个0到9999之间的随机整数,并通过格式化确保其为4位数(不足4位时前面补0)
- Random random = new Random();
- String code = String.format("%04d",random.nextInt(10000));
- // 发送验证码
- aliyunSmsUtil.sendSms(phone, code);
- log.debug("-----------> 发送验证码:" + code);
- // 存入redis
- redisTemplate.opsForValue().set(getCaptchaKey(phone,code),phone, 5, TimeUnit.MINUTES);
- }
-
- /**
- * 校验验证码
- *
- * @param phone
- * @param code
- * @return
- */
- @Override
- public boolean checkCapthcha(String phone, String code) {
- String key = getCaptchaKey(phone,code);
- Object obj = redisTemplate.opsForValue().get(key);
- return obj!=null ? true : false;
- }
- }
复制代码
|