阿里云短信服务

阿里云短信服务

1、了解阿里云用户权限操作

  • 开启子用户
  • 创建一个用户组(添加权限 SMS)
  • 创建一个用户(具体用来操作的账户)
  • 得到 AccessKey (ID,密码)

2、开通短信服务

  • 找到一个短信控制台面板
  • 查看帮助文档:

3、添加短信模板

  • 短信的具体内容
  • 等待审核通过(理由不能瞎写)

4、添加签名

  • 公司名称
  • 等待审核、

5、编写测试代码

  • 新建一个SpringBoot项目

  • 导入依赖

    <!--导入依赖-->
    <!-- 这个版本为 V1.0版本,现已更新到V2.0版本所以我们使用最新版的
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-core</artifactId>
      <version>4.6.1</version>
    </dependency>
    -->
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>dysmsapi20170525</artifactId>
        <version>2.0.24</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.79</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    
  • 添加配置

    # 配置端口号
    server.port=9090
    
    # 配置Redis
    spring.redis.host=localhost
    spring.redis.port=6379
    
  • 写一个测试类,测试短信是否可以发送成功

    package com.shan;
    
    import com.alibaba.fastjson.JSONObject;
    import com.aliyun.tea.TeaException;
    import org.junit.jupiter.api.Test;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import java.util.HashMap;
    
    @SpringBootTest
    class SpringbootSmsApplicationTests {
    
        public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
            com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                    // 必填,您的 AccessKey ID
                    .setAccessKeyId(accessKeyId)
                    // 必填,您的 AccessKey Secret
                    .setAccessKeySecret(accessKeySecret);
            // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
            config.endpoint = "dysmsapi.aliyuncs.com";
            return new com.aliyun.dysmsapi20170525.Client(config);
        }
    
        @Test
        void contextLoads() throws Exception {
            // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
            // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html
            com.aliyun.dysmsapi20170525.Client client =
                    SpringbootSmsApplicationTests.createClient(
                            ("AccessKey"),
                            ("AccessKeySerect"));
    
            //构建一个短信验证码
            HashMap<String, Object> map = new HashMap<>();
            map.put("code", 2233);
    
            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                    .setPhoneNumbers("你的电话号码") //接收短信的手机号码。
                    .setSignName("SignName") //短信签名名称。
                    .setTemplateCode("模板ID")//短信模板 Code。
                    .setTemplateParam(JSONObject.toJSONString(map)); 
    
            try {
                // 复制代码运行请自行打印 API 的返回值
                client.sendSmsWithOptions(sendSmsRequest, new com.aliyun.teautil.models.RuntimeOptions());
                System.out.println("短信发送成功");
            } catch (TeaException error) {
                // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
                // 错误 message
                System.out.println(error.getMessage());
                // 诊断地址
                System.out.println(error.getData().get("Recommend"));
                com.aliyun.teautil.Common.assertAsString(error.message);
                System.out.println("短信发送失败");
            } catch (Exception _error) {
                TeaException error = new TeaException(_error.getMessage(), _error);
                // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
                // 错误 message
                System.out.println(error.getMessage());
                // 诊断地址
                System.out.println(error.getData().get("Recommend"));
                com.aliyun.teautil.Common.assertAsString(error.message);
                System.out.println("短信发送失败");
            }
        }
    
    }
    
  • 新建一个服务类SendService,服务实现类SendServiceImpl

    Service层:

    package com.shan.service;
    
    import java.util.Map;
    
    public interface SendSms {
        public boolean sendState(String PhoneNumbers, String TemplateCode, Map<String, Object> code) throws Exception;
    }
    
    

    ServiceImpl层:

    package com.shan.service.impl;
    
    import com.alibaba.fastjson.JSONObject;
    import com.aliyun.tea.TeaException;
    import com.shan.service.SendSms;
    import org.springframework.stereotype.Service;
    
    import java.util.Map;
    
    @Service
    public class SendSmsImpl implements SendSms {
    
        public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
            com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                    // 必填,您的 AccessKey ID
                    .setAccessKeyId(accessKeyId)
                    // 必填,您的 AccessKey Secret
                    .setAccessKeySecret(accessKeySecret);
            // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
            config.endpoint = "dysmsapi.aliyuncs.com";
            return new com.aliyun.dysmsapi20170525.Client(config);
        }
        @Override
        public boolean sendState(String PhoneNumbers, String TemplateCode, Map<String, Object> code) throws Exception {
            com.aliyun.dysmsapi20170525.Client client = SendSmsImpl.createClient("AccessKey ID", "AccessKey Secret"); //注意填写自己的ID和密码
            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                    .setPhoneNumbers(PhoneNumbers) //接收短信的手机号码。
                    .setSignName("SingName") //短信签名名称。
                    .setTemplateCode(TemplateCode)
                    .setTemplateParam(JSONObject.toJSONString(code)); //短信模板 Code。
    
            try {
                // 复制代码运行请自行打印 API 的返回值
                client.sendSmsWithOptions(sendSmsRequest, new com.aliyun.teautil.models.RuntimeOptions());
                return true;
            } catch (TeaException error) {
                // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
                // 错误 message
                System.out.println(error.getMessage());
                // 诊断地址
                System.out.println(error.getData().get("Recommend"));
                com.aliyun.teautil.Common.assertAsString(error.message);
                return false;
            } catch (Exception _error) {
                TeaException error = new TeaException(_error.getMessage(), _error);
                // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
                // 错误 message
                System.out.println(error.getMessage());
                // 诊断地址
                System.out.println(error.getData().get("Recommend"));
                com.aliyun.teautil.Common.assertAsString(error.message);
                return false;
            }
        }
        
    }
    
    
  • 在写一个控制层 SmsApiController

    package com.shan.controller;
    
    import com.aliyun.credentials.utils.StringUtils;
    import com.shan.service.SendSms;
    import com.shan.utilSms.SendUtilSms;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.web.bind.annotation.CrossOrigin;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.annotation.Resource;
    import java.util.HashMap;
    import java.util.UUID;
    import java.util.concurrent.TimeUnit;
    
    @RestController
    @CrossOrigin //启动跨域传输
    public class SmsApiController {
    
        @Autowired
        private SendSms sendSms;
    
        @Resource
        private RedisTemplate<String,String> redisTemplate;
    
        @GetMapping("/send/{phone}")
        public String Code(@PathVariable("phone") String phone) throws Exception {
            String code = redisTemplate.opsForValue().get(phone);
            if (!StringUtils.isEmpty(code)) {
                return phone + "验证码:" + code + "已发送,于五分钟内有效~";
            }
            //生成随机验证码
            code = new SendUtilSms().code();
            HashMap<String,Object> param = new HashMap<>();
            param.put("code",code);
            //判断是否发送成功
            boolean isSend = sendSms.sendState(phone, "SMS_465376546",param);
    
            if (isSend) {
                redisTemplate.opsForValue().set(phone,code,5, TimeUnit.SECONDS);
                return phone + "已发送验证码:" + code + ",成功~";
            }else {
                return "发送失败";
            }
    
        }
    
    }
    
    
  • Redis的实现需要再添加一个配置 Redis Config

    package com.shan.config;
    
    import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
    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.GenericToStringSerializer;
    
    @Configuration
    public class RedisConfig {
        //通用RedisConfig配置
        @Bean
        public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
            RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
            redisTemplate.setConnectionFactory(redisConnectionFactory);
            // 使用 GenericFastJsonRedisSerializer 替换默认序列化
            GenericFastJsonRedisSerializer genericFastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
            // 设置key和value的序列化规则
            redisTemplate.setKeySerializer(new GenericToStringSerializer<>(Object.class));
            redisTemplate.setValueSerializer(genericFastJsonRedisSerializer);
            // 设置hashKey和hashValue的序列化规则
            redisTemplate.setHashKeySerializer(new GenericToStringSerializer<>(Object.class));
            redisTemplate.setHashValueSerializer(genericFastJsonRedisSerializer);
            // 设置支持事物
            redisTemplate.setEnableTransactionSupport(true);
            redisTemplate.afterPropertiesSet();
            return redisTemplate;
        }
    
    }
    
    
  • PS:因为我的短信服务选择只能发送4位数字,所以简单写了一个四位数生成器

    package com.shan.utilSms;
    
    import java.util.Random;
    
    public class SendUtilSms {
    
        public String code() {
           //随机生成4位纯数字
            int i = UUID.randomUUID().toString().hashCode();
            i = i < 0 ? -i : i; // 防止出现负数
            return String.valueOf(i).substring(0, 4);
        }
    
    }
    
    
  • 启动项目输入要测试的电话号码即可~

本篇博客主要借鉴于B站狂神说,狂神真的很厉害,希望大家多多支持!

  • 11
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值