SpringBoot整合阿里云短信服务(2)-整合短信服务到项目中

SpringBoot整合阿里云短信服务(2)-整合短信服务到项目中

上文衔接

本文衔接上文,可以看一下:

https://blog.csdn.net/m0_72166275/article/details/139186976?spm=1001.2014.3001.5501

内容简介

上文我们已经整合好了jwt,本文我们开始实现短信验证码接口的实现。这里我选用阿里云的短信服务。本文侧重点在于设计思路,阿里云控制台开通短信服务,你跟着流程走一遍就可以。个人也是可以申请的。

在这里插入图片描述

短信验证码接口实现

1.依赖导入

导入阿里云短信服务SDK

<dependency>
     <groupId>com.aliyun</groupId>
     <artifactId>dysmsapi20170525</artifactId>
     <version>2.0.24</version>
 </dependency>

2.实现思路

            1.接收手机号参数。
            2.查询 Redis 中该手机号是否有未过期的验证码。
            3.如果有未过期的验证码:
                1.判断发送次数是否超过5次,如果超过5次,修改过期时间为24小时并抛出发送次数过多异常。
                2.如果发送次数未超过5次,再判断距离上次发送时间间隔是否少于60秒,如果少于60秒,抛出发送频繁异常。
            4.如果没有未过期的验证码,代表第一次发送:
                1.生成验证码并发送短信。
                2.将验证码信息存入 Redis,包括验证码、上次发送时间戳和发送次数(初始为1),设置过期时间为5分钟。

注意事项:

考虑到验证码本身的敏感性,虽然存入Redis是临时的,但考虑安全建议对验证码内容进行加密存储,增强数据的安全性。

考虑对接口调用方进行身份验证,确保只有合法的客户端可以请求发送验证码。

3.功能实现

创建发送短信工具类
package com.atguigu.cloud.springboot.util;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;



/**
 * @author mijiupro
 */
@Data
@Component
@ConfigurationProperties(prefix = "aliyun.sms")
@Slf4j
public class SmsUtil {

    private String accessKeyId;// 访问密钥id

    private String accessKeySecret;// 访问密钥secret

    private String endpoint;// 短信 API 的访问域名

    private String signName;// 短信签名

    private String templateCode;// 短信模板ID


    // 发送短信
    public Boolean sendSms(String phone, String code) {
        // 创建阿里云客户端
        Config config = new Config()
                .setAccessKeyId(accessKeyId)// 配置访问密钥 ID
                .setAccessKeySecret(accessKeySecret);// 配置密钥
        config.endpoint = endpoint;// 配置访问端点
        Client client;
        try {
            client = new Client(config);
        } catch (Exception e) {
            log.error("阿里云短信服务初始化失败", e);
            return false;
        }

        // 构建发送请求
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName(signName) // 设置签名
                .setTemplateCode(templateCode) // 设置模板
                .setPhoneNumbers(phone) // 设置手机号为参数传入的值
                .setTemplateParam("{\"code\":\"" + code + "\"}"); // 设置模板参数为传入的验证码

        RuntimeOptions runtime = new RuntimeOptions();// 运行时选择,可以设置不同的属性来配置运行时环境的参数。
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
            if (!"OK".equals(sendSmsResponse.getBody().getCode())) {
                log.error("短信发送失败:{}", sendSmsResponse.getBody().getMessage());
                return false;
            }
            log.info("短信发送成功");
            return true;

        } catch (Exception error) {
            log.error("短信发送失败:{}", error.getMessage());
            return false;
        }
    }
}
配置阿里云短信服务

application.yml:

spring:
  data:
    redis: # Redis????
      host: 127.0.0.1  # Redis????
      port: 6379  # Redis???
      #password: 123456  # ??Redis????
      database: 0  # ????????
aliyun:
  sms:
    #阿里云Orm管理控制台 AccessKey ID
    access-key-id: LTAI5tSVXXBhiNymsX84cC5P
    #阿里云Orm管理控制台 AccessKey Secret
    access-key-secret: 4kAkeoPF8p894doq0Oc8JV3ReeBvHV
    endpoint: dysmsapi.aliyuncs.com
    signName: baiLuo
    templateCode: SMS_467400065
  • access-key-id:阿里云Orm管理控制台 AccessKey ID
  • access-key-secret:阿里云Orm管理控制台 AccessKey Secret
  • endpoint:dysmsapi.aliyuncs.com
  • signName:前面设置的签名名称
  • templateCode:前面设置的模板CODE
接口代码实现

接口

package com.atguigu.cloud.springboot.service;

public interface CaptchaService {


    /**
     *  获取短信验证码
     * @param phone
     */

    void getSmsCaptcha(String phone);

}

实现类

package com.atguigu.cloud.springboot.service.impl;

import com.atguigu.cloud.springboot.exception.GeneralBusinessException;
import com.atguigu.cloud.springboot.service.CaptchaService;
import com.atguigu.cloud.springboot.util.SmsUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;

/**
 * @author mijiupro
 */
@Service
@Slf4j
public class CaptchaServiceImpl implements CaptchaService {
    private final StringRedisTemplate stringRedisTemplate;
    private final SmsUtil smsUtil;


    public CaptchaServiceImpl(StringRedisTemplate stringRedisTemplate, SmsUtil smsUtil) {
        this.stringRedisTemplate = stringRedisTemplate;
        this.smsUtil = smsUtil;
    }




    @Override
    public void getSmsCaptcha(String phone) {
        String hashKey = "login:sms:captcha:" + phone;
        BoundHashOperations<String, String, String> hashOps = stringRedisTemplate.boundHashOps(hashKey);

        // 初始检查
        String lastSendTimestamp = hashOps.get("lastSendTimestamp");
        String sendCount = hashOps.get("sendCount");
        String captcha = hashOps.get("captcha");
        hashOps.expire(5, TimeUnit.MINUTES); // 设置过期时间为5分钟

        // 判断发送次数是否超过限制
        if (StringUtils.isNotBlank(sendCount) && Integer.parseInt(sendCount) >= 5) {
            hashOps.expire(24, TimeUnit.HOURS); // 重新设置过期时间为24H
            throw new GeneralBusinessException("发送次数过多,请24H后再试");
        }

        // 判断发送频率是否过高
        if (StringUtils.isNotBlank(lastSendTimestamp)) {
            long lastSendTime = Long.parseLong(lastSendTimestamp);
            long currentTime = System.currentTimeMillis();
            long elapsedTime = currentTime - lastSendTime;
            long interval = 60 * 1000; // 60秒
            if (elapsedTime < interval) {
                throw new GeneralBusinessException("发送短信过于频繁,请稍后再试");
            }
        }

        // 更新发送次数
        int newSendCount = StringUtils.isNotBlank(sendCount) ? Integer.parseInt(sendCount) + 1 : 1;

        // 生成新验证码
        if (StringUtils.isBlank(captcha)) {
            captcha = RandomStringUtils.randomNumeric(6);}

        // 发送短信
        if (!smsUtil.sendSms(phone, captcha)) {
            throw new GeneralBusinessException("发送短信失败");
        }

        // 更新 Redis 中的信息
        hashOps.put("captcha", captcha);
        hashOps.put("lastSendTimestamp", String.valueOf(System.currentTimeMillis()));
        hashOps.put("sendCount", String.valueOf(newSendCount));

    }

}

控制器

package com.atguigu.cloud.springboot.controller;

import com.atguigu.cloud.springboot.service.CaptchaService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author mijiupro
 */
@RestController
@RequestMapping("/captcha")
public class CaptchaController {

    private final CaptchaService captchaService;
    public CaptchaController(CaptchaService captchaService) {
        this.captchaService = captchaService;
    }

    @GetMapping("/sms-captcha/{phone}")
    public void getSmsCaptcha(@PathVariable String phone) {
        System.out.println("phone:"+phone);
        captchaService.getSmsCaptcha(phone);
    }
}	

说明:

代码里面我直接返回void是因为我做了全局响应结果拦截统一封装,实际应用为了考虑防盗刷等因素并不会返回空。

创建RedisConfig配置类
package com.atguigu.cloud.springboot.config;

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.StringRedisSerializer;

@Configuration
public class RedisConfiguration {
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate redisTemplate = new RedisTemplate<>();
        //设置redis的连接工厂对象
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //设置redis key的序列化器
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        return redisTemplate;
    }
}

4.功能测试

我这里是整合了apipost调试工具的

在这里插入图片描述
在这里插入图片描述

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值