首页技术栈归档照片墙音乐日记随想收藏夹友链留言关于

Redis 配置与使用

写作时间:2026-07-07

spring: redis: host: 127.0.0.1 port: 6379


SendSms

```java
public interface SendSms {

    /**
     * 用于发送短信
     * @param phoneNumber :手机号
     * @param templateCode :模板编号
     * @param code :验证码
     * @return 是否发送成功
     */
    public boolean send(String phoneNumber, String templateCode, Map<String, Object> code);
}
```cpp

SendSmsImpl

```java
@Service
public class SendSmsImpl implements SendSms{
    /**
     * 用于发送短信
     *
     * @param phoneNumber  :手机号
     * @param templateCode :模板编号
     * @param code         :验证码
     * @return 是否发送成功
     */
    @Override
    public boolean send(String phoneNumber, String templateCode, Map<String, Object> code) {
        // 这里的 AccessKey ID 、 Secret就是 阿里云用户对于的值,复制过来即可
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "AccessKey ID", "AccessKey Secret");
        IAcsClient client = new DefaultAcsClient(profile);

        // 构建请求
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);//不要懂
        request.setSysDomain("dysmsapi.aliyuncs.com");//不要动
        request.setSysVersion("2017-05-25");//官方版本不要动
        request.setSysAction("SendSms");
        // 上面不需要改
        // 自定义参数 :
        // 手机号,这里 Value 就填用户的手机号,实际应用须要从表单获取
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        // 签名,这里的 Value 就是在阿里云上申请的 签名
        request.putQueryParameter("SignName", "阿里云短信测试");
        // 模板,这里的 Value 就是在阿里云上申请的模板的 模版CODE 值
        request.putQueryParameter("TemplateCode", templateCode);
        // 验证码,真实应用需要自动构建验证码
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(code));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            //成功返回
            return response.getHttpResponse().isSuccess();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}
```cpp

SmsApiController

```java
@RestController
@CrossOrigin // 跨域的支持
public class SmsApiController {
    @Autowired
    private SendSms sendSmsService;

    @Resource
    private RedisTemplate redisTemplate;

    @GetMapping("send/{phone}")
    public String sendSms(@PathVariable("phone") String phoneNumber){
        // 调用发送发放(模拟真实业务,整合 Redis)
        // 判断当前手机号是否存储在 Redis 中
        // 如果没有则发送短信
        // 如果有表示上一个验证码还未过期,不用发送
        String code = (String) redisTemplate.opsForValue().get(phoneNumber);
        if (!StringUtils.isEmpty(code)){
            return "[手机号: "+phoneNumber + "],[验证码: " + code +"],还未过期";
        }
        // 生成验证码
        code = UUID.randomUUID().toString().substring(0,6);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",code);
        boolean isSend = sendSmsService.send(phoneNumber, "SMS_154950909", map);
        // 如果发送成功,就放入 Redis
        if (isSend){
            //存在Redis中,5分钟过期
            redisTemplate.opsForValue().set(phoneNumber,code,5, TimeUnit.MINUTES);
            return "[手机号: "+phoneNumber + "],[验证码: " + code +"],发送成功";
        }else {
            return "[手机号: "+phoneNumber + "],[验证码: " + code +"],发送失败";
        }
    }   
}
avatar

yuanyourdomain

写代码,做研究,记录生活。

RECOMMENDED

MyBatis 动态 SQL

2026-07-08

Nginx 基础入门

2026-07-08

Maven 多模块与私服

2026-07-08