SpringBoot-实现Aliyun短信接口

进入阿里云官网进行信息配置

阿里云官网:https://help.aliyun.com/

  1. 注册阿里云账号

  2. 获取您的 AccessKey 和 AccessKey Secert
    RAM控制台网址:https://ram.console.aliyun.com/overview
    2.1.点击左侧菜单栏人员管理,选择用户
    2.2.创建用户
    2.3.在用户登录名称/显示名称下点击用户名称
    2.4.找到用户 AccessKey模块,点击创建AccessKey
    点击确定,记住 AccessKey 和 AccessKey Secert
    在这里插入图片描述
    在这里插入图片描述

  3. 进行授权
    3.1.在RAM 访问控制下的左侧菜单栏授权管理,点击授权
    3.2.新增授权
    AliyunDysmsFullAccess:管理短信服务(SMS)的权限
    AliyunDysmsReadOnlyAccess:只读访问短信服务(SMS)的权限
    授权以上两项即可行
    在这里插入图片描述
    在这里插入图片描述

  4. 创建签名、模块
    短信服务地址: https://dysms.console.aliyun.com/dysms.htm#/overview
    4.1.进入短信服务,左侧菜单栏,国内消息
    4.2.新增签名和模块
    4.3.等待审核
    在这里插入图片描述
    在这里插入图片描述

SpringBoot实现阿里云短信

1.打开pom.xml文件,添加依赖

<!-- 阿里云短信SDK -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
            <version>1.1.0</version>
        </dependency>

2.创建AliyunConfig类

  /**
 * @Description: 阿里云短信接口配置类
 */
public class AliyunConfig {

    /* 短信API产品名称(短信产品名固定,无需修改) */
    private static final String product = "Dysmsapi";

    /* 短信API产品域名,接口地址固定,无需修改 */
    private static final String domain = "dysmsapi.aliyuncs.com";

    /* 此处需要替换成开发者自己的accessKeyId和accessKeySecret(在阿里云访问控制台寻找) */
    private static final String accessKeyId = "这里要写成你自己生成的"; //这里要写成你自己生成的
    private static final String accessKeySecret = "这里要写成你自己生成的";//这里要写成你自己生成的
    /* 短信发送 */
    public static SendSmsResponse sendSms(String phone) throws ClientException {

        /* 超时时间,可自主调整 */
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        /* 初始化acsClient,暂不支持region化 */
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        /* 组装请求对象-具体描述见控制台-文档部分内容 */
        SendSmsRequest request = new SendSmsRequest();
        /* 必填:待发送手机号 */
        request.setPhoneNumbers(phone);
        /* 必填:短信签名-可在短信控制台中找到 */
        request.setSignName("这里是你短信签名"); //这里是你短信签名
        /* 必填:短信模板code-可在短信控制台中找到 */
        request.setTemplateCode("这里是你的模板code"); //这里是你的模板code
        /* 可选:模板中的变量替换JSON串,如模板内容为"亲爱的用户,您的验证码为${code}"时,此处的值为 */
        String code=getMsgCode();
        request.setTemplateParam("{\"code\":\"" + code + "\"}");
        // hint 此处可能会抛出异常,注意catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        if(sendSmsResponse.getCode()!= null && sendSmsResponse.getCode().equals("OK")){
            System.out.println("短信发送成功!验证码:" + code);
        }else {
            System.out.println("短信发送失败!");
        }
        return sendSmsResponse;
    }

    /**
     * @Function: 生成验证码
     * @author:   yangxf
     * @Date:     2019/4/11 15:30
     */
    private static String getMsgCode() {
        int n = 6;
        StringBuilder code = new StringBuilder();
        Random ran = new Random();
        for (int i = 0; i < n; i++) {
            code.append(Integer.valueOf(ran.nextInt(10)).toString());
        }
        return code.toString();
    }
}

3.service层

   /**
     * 手机登录
     */
     public Map<String, Object> SmsVerification(String phone);

4.service实现层

   @Override
    public Map<String, Object> SmsVerification(String phone) {
        Map<String, Object> map = new HashMap<>();
        try {
            AliyunConfig.sendSms(phone);
            map.put("code", 200);
            map.put("msg", "短信验证发送成功");
            return map;
        } catch (ClientException e) {
            map.put("code", 400);
            map.put("msg", e.getMessage());
            return map;
        }
    }

5.controller层

    public Object SmsVerification(@RequestParam("phone") String phone) {
        return phoneLoginService.SmsVerification(phone);
    }

在这里插入图片描述

控制返回:成功,则说明短信已经发送成功。
核心代码是之前看到的一位博客写的,直接拿来用了,忘记那位博客的名字了(时间太久)

项目源码地址:https://gitee.com/li_yanhao/springboot_aliyuncode


版权声明:本文为fsadkljhfweidfne原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。