阿里云发送短信功能

一、前提条件

阿里云开通短信服务

1、开通短信服务后,在控制台进入短信管理,进行申请签名、短信模板
在这里插入图片描述
2、开通AccessKey(可以选择子用户组accesskey,然后给用户组开通短信管理权限)
在这里插入图片描述
3、短信教程中,找到sdk教程,下载demo测试
在这里插入图片描述
4、demo
下载demo运行测试(修改accesskey等信息)

// This file is auto-generated, don't edit it. Thanks.
package com.aliyun.sample;

import com.aliyun.tea.*;
import com.aliyun.dysmsapi20170525.*;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.*;
import com.aliyun.teaopenapi.models.*;

public class Sample {

/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
Config config = new Config()
// 您的AccessKey ID
.setAccessKeyId(accessKeyId)
// 您的AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// 访问的域名
config.endpoint = "dysmsapi.aliyuncs.com";
return new com.aliyun.dysmsapi20170525.Client(config);
}

public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.dysmsapi20170525.Client client = Sample.createClient("accessKeyId", "accessKeySecret");
SendSmsRequest sendSmsRequest = new SendSmsRequest();
// 复制代码运行请自行打印 API 的返回值
client.sendSms(sendSmsRequest);
}
}

二、项目验证

项目是基于SpringBoot2.5.1开发,在项目使用到了redis做一个验证码的存储。
结构:
在这里插入图片描述

pom依赖
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>dysmsapi20170525</artifactId>
    <version>2.0.4</version>
</dependency>
<!-- redis依赖包 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- springboot2.x 整合redis需要pool连接池-->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.6.0</version>
</dependency>
application.properties配置
#redis配置
#Redis服务器地址
spring.redis.host=127.0.0.1
#Redis服务器连接端口
spring.redis.port=6379
#Redis数据库索引(默认为0)
spring.redis.database=0  
#连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=50
#连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.lettuce.pool.max-wait=3000
#连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=20
#连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=2
#连接超时时间(毫秒)
spring.redis.timeout=5000
CodeController类
package com.xiaolu.controller;

import com.xiaolu.service.CodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: xiaolu
 * Description: 验证码接口
 * @Date: created on 2021/6/15
 */
@RestController
public class CodeController {

    @Autowired
    private CodeService codeService;

    /**
     * 给当前手机发送一条验证码
     * @param phone 手机号
     * @return 发送结果
     * @throws Exception
     */
    @RequestMapping("/sendCode")
    public String sendCode(@RequestParam String phone) {
        return codeService.sendCode(phone);
    }

    /**
     * 根据电话号码获取缓存中的验证码
     * @param phone
     * @return
     */
    @RequestMapping("/getCode")
    public String getCode(@RequestParam String phone) {
        return codeService.getCode(phone);
    }
}
codeSerice接口
package com.xiaolu.service;

/**
 * @Author: xiaolu
 * Description: 发送短信业务接口
 * @Date: created on 2021/6/15
 */
public interface CodeService {
    String sendCode(String phone);
    String getCode(String phone);
}
CodeServiceImpl类
package com.xiaolu.service;

import com.xiaolu.utils.CodeUtils;
import com.xiaolu.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @Author: xiaolu
 * Description: 发送短信业务类
 * @Date: created on 2021/6/15
 */
@Service
public class CodeServiceImpl implements CodeService{
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 发送验证码
     * @param phone
     * @return
     */
    @Override
    public String sendCode(String phone) {
        // 查询redis中的验证码是否过期
        String code = redisUtils.get(phone);
        if (StringUtils.hasText(code)) {
            return "验证码伤未过期,验证码为:" + code;
        }

        code = String.valueOf((int)((Math.random()*9+1)*1000)); // 随机生成4位验证码
        try {
            /**
             * 发送验证码给用户
             * phone 接受验证码的手机号
             * templateCode 短信模板id
             * code 验证码
             */
            CodeUtils.sendCode(phone, code);
            redisUtils.set(phone, code, 3); // 设置验证码3分钟过期
            return "验证码发送成功:为:" + code;
        } catch (Exception e) {
            e.printStackTrace();
            return "验证码发送失败";
        }
    }

    /**
     * 查询缓存中的验证码
     * @param phone
     * @return
     */
    @Override
    public String getCode(String phone) {
        return redisUtils.get(phone);
    }
}
CodeUtils发送短信工具类
package com.xiaolu.utils;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.beans.factory.annotation.Value;

/**
 * @Author: xiaolu
 * Description: 验证码发送工具类
 * @Date: created on 2021/6/16
 */
public class CodeUtils {

    private static String accessKeyId="你的keyid"; // 阿里云accesskey
    private static String accessKeySecret="你的secret"; // 阿里云secret
    private static String templateCode="你的短信id"; // 阿里云短信模板id
    private static String signName="你的签名"; // 短信签名,【签名】

    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    /**
     * 发送验证码
     * @param phone 电话
     * @param code 验证码
     * @throws Exception 发送出错异常
     */
    public static void sendCode(String phone, String code) throws Exception {

        com.aliyun.dysmsapi20170525.Client client = CodeUtils.createClient( accessKeyId, accessKeySecret);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phone)
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setTemplateParam("{\"code\": \"" + code + "\"}");
        SendSmsResponse resp = client.sendSms(sendSmsRequest);
    }

}
RedisUtils类
package com.xiaolu.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @Author: xiaolu
 * Description: redis缓存工具类
 * @Date: created on 2021/6/16
 */
@Component
public class RedisUtils {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 存储String类型数据到redis
     * @param key  存储的键
     * @param value 存储的值
     * @return 存储成功返回true,否则返回false
     */
    public boolean set(String key, String value) {
        boolean reslut = false;
        try {
            redisTemplate.opsForValue().set(key, value);
            reslut = true;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return reslut;
    }
    public boolean set(String key, String value, int minute) {
        boolean reslut = false;
        try {
            redisTemplate.opsForValue().set(key, value, minute, TimeUnit.MINUTES); // 时间单位位分钟
            reslut = true;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return reslut;
    }

    /**
     * 根据键取得redis上的内容
     * @param key 键
     * @return 结果
     */
    public String get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 更新缓存
     */
    public boolean getAndSet(final String key, String value) {
        boolean result = false;
        try {
            redisTemplate.opsForValue().getAndSet(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 删除缓存
     */
    public boolean delete(final String key) {
        boolean result = false;
        try {
            redisTemplate.delete(key);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
  • 4
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值