阿里和云之讯短信发送服务

简介: 阿里和云之讯短信发送服务

1 云之讯短信验证码

短信验证码选用云之讯第三方短信平台:https://www.ucpaas.com/

选择原因:注册赠送10元,无实名认证也可以发验证码短信。现在也没了3元好像

.1 创建应用

自行注册、登录后进行创建应用:

默认情况下,短信只能发送给自己注册的手机号,为了测试方便需要添加测试手机号(最多6个):

1.2 创建短信模板

发送短信需要创建短信模板,模板中采用{1}、{2}的形式作为参数占位。

模板处于待审核状态是不可以使用的,需要审核通过后才能使用,审核通过后会有短信通知。

1.3 发送短信api

地址:http://docs.ucpaas.com/doku.php?id=%E7%9F%AD%E4%BF%A1:sendsms

1.4 编写代码

配置RestTemplateConfig:

package com.oldlu.sso.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.converter.StringHttpMessageConverter;
import java.nio.charset.Charset;
@Configuration
public class RestTemplateConfig {
    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        RestTemplate restTemplate = new RestTemplate(factory);
        // 支持中文编码
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(Charset.forName("UTF-8")));
        return restTemplate;
    }
    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(5000);//单位为ms
        factory.setConnectTimeout(5000);//单位为ms
        return factory;
    }
}

编写SmsService:

package com.oldlu.sso.service;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Service
public class SmsService {
    @Autowired
    private RestTemplate restTemplate;
    private static final ObjectMapper MAPPER = new ObjectMapper();
    /**
     * 发送验证码短信
     *
     * @param mobile
     */
    public String sendSms(String mobile) {
        String url = "https://open.ucpaas.com/ol/sms/sendsms";
        Map<String, Object> params = new HashMap<>();
        params.put("sid", "*******");
        params.put("token", "*******");
        params.put("appid", "*******");
        params.put("templateid", "*****");
        params.put("mobile", mobile);
        // 生成4位数验证 如果是多个参数用,分隔
        params.put("param", RandomUtils.nextInt(100000, 999999));
        ResponseEntity<String> responseEntity = this.restTemplate.postForEntity(url, params, String.class);
        String body = responseEntity.getBody();
        try {
            JsonNode jsonNode = MAPPER.readTree(body);
            //000000 表示发送成功
            if (StringUtils.equals(jsonNode.get("code").textValue(), "000000")) {
                return String.valueOf(params.get("param"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

1.5 编写测试用例

package com.oldlu.sso.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class TestSmsService {
    @Autowired
    private SmsService smsService;
    @Test
    public void sendSms(){
        this.smsService.sendSms("158****7944");
    }
}

1.6 编写接口服务

编写ErrorResult:

package com.oldlu.sso.vo;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class ErrorResult {
    private String errCode;
    private String errMessage;
}

SmsController:

package com.oldlu.sso.controller;
import com.oldlu.sso.service.SmsService;
import com.oldlu.sso.vo.ErrorResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
@RequestMapping("user")
public class SmsController {
    private static final Logger LOGGER = LoggerFactory.getLogger(SmsController.class);
    @Autowired
    private SmsService smsService;
    /**
     * 发送验证码
     *
     * @return
     */
    @PostMapping("login")
    public ResponseEntity<Object> sendCheckCode(@RequestBody Map<String, Object> param) {
        ErrorResult.ErrorResultBuilder resultBuilder = ErrorResult.builder().errCode("000000").errMessage("发送短信验证码失败");
        try {
            String phone = String.valueOf(param.get("phone"));
            Map<String, Object> sendCheckCode = this.smsService.sendCheckCode(phone);
            int code = ((Integer) sendCheckCode.get("code")).intValue();
            if (code == 3) {
                return ResponseEntity.ok(null);
            }else if(code == 1){
                resultBuilder.errCode("000001").errMessage(sendCheckCode.get("msg").toString());
            }
        } catch (Exception e) {
            LOGGER.error("发送短信验证码失败", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(resultBuilder.build());
    }
}

SmsService:

package com.oldlu.sso.service;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
@Service
public class SmsService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SmsService.class);
    @Autowired
    private RestTemplate restTemplate;
    private static final ObjectMapper MAPPER = new ObjectMapper();
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    /**
     * 发送验证码
     *
     * @param mobile
     * @return
     */
    public Map<String, Object> sendCheckCode(String mobile) {
        Map<String, Object> result = new HashMap<>(2);
        try {
            String redisKey = "CHECK_CODE_" + mobile;
            String value = this.redisTemplate.opsForValue().get(redisKey);
            if (StringUtils.isNotEmpty(value)) {
                result.put("code", 1);
                result.put("msg", "上一次发送的验证码还未失效");
                return result;
            }
            String code = this.sendSms(mobile);
            if (null == code) {
                result.put("code", 2);
                result.put("msg", "发送短信验证码失败");
                return result;
            }
            //发送验证码成功
            result.put("code", 3);
            result.put("msg", "ok");
            //将验证码存储到Redis,2分钟后失效
            this.redisTemplate.opsForValue().set(redisKey, code, Duration.ofMinutes(2));
            return result;
        } catch (Exception e) {
            LOGGER.error("发送验证码出错!" + mobile, e);
            result.put("code", 4);
            result.put("msg", "发送验证码出现异常");
            return result;
        }
    }
}
package com.itheima.sso.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result {
    private boolean success;
    private int code;
    private String message;
}
 public Result sendCheckCode(String phone) {
        String checkCodeKEY = "CHECK_CODE_" + phone;
        String redisCode = redisTemplate.opsForValue().get(checkCodeKEY);
        if (StringUtils.isNotEmpty(redisCode)){
            return new Result(true,1,"上一次发送的验证码还未失效");
        }
        String code = this.sendSms(phone);
        if (code == null){
            return new Result(false,2,"发送验证码失效");
        }
        //存储在redis中
        this.redisTemplate.opsForValue().set(checkCodeKEY,code, Duration.ofHours(24));
        return new Result(true,3,"发送验证码成功");
    }

2 阿里云短信服务

云之讯平台如果不能审核通过的话,可以使用阿里云短信服务进行发送。

2.1 申请签名与模板

https://dysms.console.aliyun.com/dysms.htm?spm=5176.12818093.0.ddysms.2a4316d0ql6PyD

说明:申请签名时,个人用户只能申请一个并且签名的名称必须为“ABC商城”,否则审核不通过。

申请模板

审核时间需要12小时,请耐心等待

2.2 设置用户权限

在阿里云中,需要在RAM服务中创建用户以及权限,才能通过api进行访问接口。

创建用户

创建完成后要保存AccessKey Secret和AccessKey ID,AccessKey Secret只显示这一次,后面将不再显示。

添加权限

2.3 示例代码

文档:https://help.aliyun.com/document_detail/101414.html?spm=a2c4g.11186623.6.625.18705ffa8u4lwj:

package com.oldlu.sso.service;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
/*
pom.xml
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>4.5.3</version>
</dependency>
*/
public class SendSms {
    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "LTAI4G7d2Q9CHc741gighjTF", "uKOOGdIKvmoGhHlej8cJY8H3nlU6Fj");
        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");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", "158****7944"); //目标手机号
        request.putQueryParameter("SignName", "ABC商城"); //签名名称
        request.putQueryParameter("TemplateCode", "SMS_204756062"); //短信模板code
        request.putQueryParameter("TemplateParam", "{\"code\":\"123456\"}");//模板中变量替换
        try {
            CommonResponse response = client.getCommonResponse(request);
            //{"Message":"OK","RequestId":"EC2D4C9A-0EAC-4213-BE45-CE6176E1DF23","BizId":"110903802851113360^0","Code":"OK"}
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

2.4 实现发送短信方法

配置文件:aliyun.properties

    aliyun.sms.regionId = cn-hangzhou
    aliyun.sms.accessKeyId = LTAI4G7d2Q9CHc741gighjTF
    aliyun.sms.accessKeySecret = uKOOGdIKvmoGhHlej8cJY8H3nlU6Fj
    aliyun.sms.domain= dysmsapi.aliyuncs.com
    aliyun.sms.signName= ABC商城
    aliyun.sms.templateCode= SMS_204756062

需要注意中文编码问题

读取配置:

package com.oldlu.sso.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:aliyun.properties")
@ConfigurationProperties(prefix = "aliyun.sms")
@Data
public class AliyunSMSConfig {
    private String regionId;
    private String accessKeyId;
    private String accessKeySecret;
    private String domain;
    private String signName;
    private String templateCode;
}
//SmsService.java
/**
     * 通过阿里云发送验证码短信
     *
     * @param mobile
     */
    public String sendSmsAliyun(String mobile) {
        DefaultProfile profile = DefaultProfile.getProfile(
                this.aliyunSMSConfig.getRegionId(),
                this.aliyunSMSConfig.getAccessKeyId(),
                this.aliyunSMSConfig.getAccessKeySecret());
        IAcsClient client = new DefaultAcsClient(profile);
        String code = RandomUtils.nextInt(100000, 999999) +"";
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(this.aliyunSMSConfig.getDomain());
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", this.aliyunSMSConfig.getRegionId());
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", this.aliyunSMSConfig.getSignName());
        request.putQueryParameter("TemplateCode", this.aliyunSMSConfig.getTemplateCode());
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            if(StringUtils.contains(response.getData(), "\"Code\":\"OK\"")){
                return code;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
       return null;
    }


目录
相关文章
|
自然语言处理 算法 安全
算法备案如何申请
算法备案大模型申请难度较高,主要体现在材料准备复杂、技术描述严谨、安全评估严格及审核标准模糊等方面,对企业的合规能力与专业水平提出较高要求。
|
人工智能 IDE 程序员
从 AI Coding 演进路径看通义灵码 AI 程序员的发布,让更多 idea 变成产品
通义灵码 2.0 不仅正式发布 AI 程序员,还升级了很多基础能力,使用场景多样。繁星计划的推出更为大学生提供了免费的智能编码助手,助力科技创新。让不具备编码能力的人也可以将 idea 变成产品,帮助到更多开发者和泛开发者。
|
网络协议 数据安全/隐私保护 网络架构
|
数据可视化
Open3d Point cloud outlier removal 点云异常值移除
Open3d Point cloud outlier removal 点云异常值移除
1038 1
|
机器学习/深度学习 算法 关系型数据库
【文献学习】DeepReceiver: A Deep Learning-Based Intelligent Receiver for Wireless Communications in the Ph
文章介绍了一种名为DeepReceiver的基于深度学习的无线通信智能接收机模型,它通过输入IQ信号并输出恢复的信息比特流,能够适应不同的调制和编码方式,在噪声、射频损伤、信道衰落等非理想条件下进行性能验证。
720 0
|
Oracle 关系型数据库 MySQL
|
索引 Windows
技术笔记:RTF文件格式
技术笔记:RTF文件格式
1447 0
|
机器学习/深度学习 人工智能 分布式计算
阿里云PAI:一站式AI研发平台,引领深度学习潮流
阿里云PAI:一站式AI研发平台,引领深度学习潮流 随着人工智能的飞速发展,深度学习框架已经成为AI研发的核心工具。然而,选择合适的深度学习框架并不容易,需要考虑的因素包括计算性能、易用性、支持的算法组件等多种因素。今天,我们就来介绍一款一站式AI研发平台——阿里云PAI,看看它如何解决这些痛点。
1230 1
|
人工智能 自然语言处理 API
万媒易发:以RPA自动化和AIGC为基础实现多平台分发
在当今数字化时代,信息传播的速度越来越快,多平台分发成为了内容创作者们必须面对的重要挑战之一。为了解决这一难题,我们可以借助RPA(Robotic Process Automation)自动化和AIGC(Artificial Intelligence Generated Content)技术,实现高效的多平台内容发布。本文将介绍如何利用“万媒易发”工具来简化这一过程,提高内容分发的效率。