首页 > 其他分享 >整合阿里云短信服务

整合阿里云短信服务

时间:2023-05-28 21:12:00浏览次数:35  
标签:aliyuncs 短信 String request 阿里 整合 import com public

阿里云官网

1.登录官网https://www.aliyun.com/

2.短信服务,申请签名

 3.添加模板

 项目中依赖

<dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-core</artifactId>
</dependency>

配置文件

aliyun.sms.regionId=default
aliyun.sms.accessKeyId=LTAI5tM1Vtb****
aliyun.sms.secret=PAjXlhEOU6ehy*****

工具配置类

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class ConstantPropertiesUtils implements InitializingBean {

    @Value("${aliyun.sms.regionId}")
    private String regionId;

    @Value("${aliyun.sms.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.sms.secret}")
    private String secret;

    //对外提供
    public static String REGION_Id;
    public static String ACCESS_KEY_ID;
    public static String SECRECT;

    @Override
    public void afterPropertiesSet() throws Exception {
        REGION_Id=regionId;
        ACCESS_KEY_ID=accessKeyId;
        SECRECT=secret;
    }
}

生成随机验证码

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

public class RandomUtil {

    private static final Random random = new Random();

    private static final DecimalFormat fourdf = new DecimalFormat("0000");

    private static final DecimalFormat sixdf = new DecimalFormat("000000");

    public static String getFourBitRandom() {
        return fourdf.format(random.nextInt(10000));
    }

    public static String getSixBitRandom() {
        return sixdf.format(random.nextInt(1000000));
    }

    /**
     * 给定数组,抽取n个数据
     * @param list
     * @param n
     * @return
     */
    public static ArrayList getRandom(List list, int n) {

        Random random = new Random();

        HashMap<Object, Object> hashMap = new HashMap<Object, Object>();

        // 生成随机数字并存入HashMap
        for (int i = 0; i < list.size(); i++) {

            int number = random.nextInt(100) + 1;

            hashMap.put(number, i);
        }

        // 从HashMap导入数组
        Object[] robjs = hashMap.values().toArray();

        ArrayList r = new ArrayList();

        // 遍历数组并打印数据
        for (int i = 0; i < n; i++) {
            r.add(list.get((int) robjs[i]));
            System.out.print(list.get((int) robjs[i]) + "\t");
        }
        System.out.print("\n");
        return r;
    }

}

整合阿里云短信服务代码

import com.alibaba.fastjson.JSONObject;
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;
import com.atguigu.yygh.msm.service.MsmService;
import com.atguigu.yygh.msm.utils.ConstantPropertiesUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.HashMap;
import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {
    @Override
    public boolean send(String phone, String code) {
        if(StringUtils.isEmpty(phone)){
            return false;
        }
        //整合阿里云的短信服务发送,设置相关参数
        DefaultProfile profile = DefaultProfile.
                getProfile(ConstantPropertiesUtils.REGION_Id,
                        ConstantPropertiesUtils.ACCESS_KEY_ID,
                        ConstantPropertiesUtils.SECRECT);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //如果请求类型时https时设置
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        //手机号,左边参数固定不能改
        request.putQueryParameter("PhoneNumbers", phone);
        //签名名称
        request.putQueryParameter("SignName", "ixuetao");
        //模板code
        request.putQueryParameter("TemplateCode", "SMS_27947xxx");
        //验证码  使用json格式   {"code":"123456"}
        Map<String,Object> param = new HashMap();
        param.put("code",code);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

        //调用方法进行短信发送
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return false;
    }
}

 

标签:aliyuncs,短信,String,request,阿里,整合,import,com,public
From: https://www.cnblogs.com/ixtao/p/17438849.html

相关文章

  • springboot整合mybatis实现简单的crud操作
    使用MyBatis框架操作数据,在SpringBoot框架集成MyBatis,项目整体结构前提:准备一张student表。SETNAMESutf8mb4;SETFOREIGN_KEY_CHECKS=0;--------------------------------Tablestructureforuser------------------------------DROPTABLEIFEXISTS`student`......
  • Java:SpringBoot整合Canal+RabbitMQ组合实现MySQL数据监听
    canal[kə’næl],译意为水道/管道/沟渠,主要用途是基于MySQL数据库增量日志解析,提供增量数据订阅和消费目录一、MySQL设置二、启动Canal服务端三、通过Canal客户端消费数据四、通过RabbitMQ消费数据1、启动RabbitMQ2、修改canal配置3、消费RabbitMQ中的数据文档资料github:https......
  • springboot整合websocket
    一、引入依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId&g......
  • 短信发送
    短信发送前提:基于springboot使用阿里云提供的短信发送业务,如果使用完整版的短信发送功能,那么就需要申请签名和模板,这需要人工审核,需要一些工作日,所以使用短信服务的测试功能短信服务(aliyun.com)帮助文档:短信服务(aliyun.com)注意:申请签名和模板,以及账户里有一定余额添加依......
  • 阿里云服务器Linux MySQL root 密码忘记了如何操作?
    阿里云服务器Linux MySQL root密码忘记了如何操作?假如我们使用的MySQL数据库忘记的账号密码,是能够土工调节配置文件,然后跳过密码方式登录到数据库的。然后在数据库里面修改账号和密码,通常在默认情况下账号为root具体操作步骤如下:1】编辑MySQL配置文件my.cnf【注】在具体的操作......
  • 近万条一级分类经典短信大全ACCESS\EXCEL数据库
    近万条一级分类经典短信大全ACCESS数据库收集的是近万条常用经典短信,之所以称“一级分类”(意思是只有一个大类没有子类),原因是为了区别另外一个有二级分类的短信数据库。近万条一级分类经典短信大全ACCESS数据库中的短信都是经过索引没有收录重复的记录。大类分类情况是:爱情短信(1......
  • vue中离线使用阿里巴巴图标库iconfont
    1.打开iconfont阿里巴巴官网https://www.iconfont.cn2.新建项目(这样方便后期维护图标库) image.png3.把需要的图标添加到购物车 image.png4.打开购物车并选择添加至项目,然后确定 image.png5.这时候可以在项目中看到你选中的svg图标,这时候可以点击下载至本地 image.png6.打开下......
  • Fdog系列(三):使用腾讯云短信接口发送短信,数据库写入,部署到服务器,web收尾篇。
    文章目录1.前言2.使用腾讯云短信接口发送短信3.java连接数据库4.部署到服务器(如果你有的话)目录Fdog系列(一):思来想去,不如写一个聊天软件,那就从仿QQ注册页面开始吧。Fdog系列(二):html写完注册页面之后怎么办,用java写后台响应呀。文章中出现的源码获取方式:评论区留下邮箱地址。创作......
  • 外汇天眼:假短信催缴ETC通行费,填写资料秒被盗刷1万元!
    近年来诈骗犯罪不但持续增长,就连手法也愈来愈多元,虽然本质上都是骗取个资或金钱,但当它们以不同形态出现时,如果民众稍有不慎,可能就会落入诈骗集团的陷阱中。不久前有苦主在网络平台PTT上发文,分享自己遭假缴费简讯欺骗的惨痛教训,呼吁大家小心新型诈骗手法。据了解,受害者是在与同事吃......
  • 阿里云如何配置子域名及其对应 RAM 访问权限
    背景背景:假设只有一个二级域名domain.com,有多套环境的情况下,可能需要分配不同的三级子域名sub.domain.com,每个环境可能需要再配置四级子域名sub.sub.domain.com如果使用一个AK拥有所有域名的DNS权限,可能不太安全(即使互相信任,也无法避免误操作导致影响其他环境)需求:......