栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring boot集成阿里云短信服务

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring boot集成阿里云短信服务

1.导入相关pom依赖

            com.aliyun
            aliyun-java-sdk-dysmsapi
            2.2.1
        
        
            com.aliyun
            aliyun-java-sdk-core
            4.3.3
        
2.登录阿里云官网开通短信服务 并且获取一些重要配置信息(我把我信息给你们了 没啥钱 别乱用就行)

给自己使用用户授权(没有用户的自己创建用户) 

查看模板 签名信息等

  

3.创建bean管理短信服务需要用的参数

yml:(keyid keysecrect通过查看自己子用户获得)

aliyun:
  sms: #短信服务
    endpoint: cn-shanghai
    keyid: LTAI5t917KtK4jMysr7q
    keysercret: TpCng6xABP3hqehI9NyMAVQ
    SignName: 阿里云短信测试 #模板 从阿里云获取
    TemplateCode: SMS_154950909 #模板号
@Data
@Component
@ConfigurationProperties(prefix = "aliyun.sms")
public class SmsProperties {
    private String endpoint;
    private String keyid;
    private String keysercret;
    private String SignName;
    private String TemplateCode;
}
 4.创建短信发送服务
    @Autowired
    SmsProperties smsProperties;
    //发送验证码
    @Override
    public void send(String tel, String code) throws ClientException {
        DefaultProfile profile=DefaultProfile.getProfile(smsProperties.getEndpoint(),smsProperties.getKeyid(),smsProperties.getKeysercret());
        IAcsClient client=new DefaultAcsClient(profile);
        SendSmsRequest request=new SendSmsRequest();
        request.setSignName(smsProperties.getSignName());
        request.setTemplateCode(smsProperties.getTemplateCode());
        request.setPhoneNumbers(tel);//设置手机号
        Map param=new HashMap<>();
        param.put("code",code);
        Gson gson=new Gson();
        String json = gson.toJson(param);
        request.setTemplateParam(json); //设置随机验证码
        //发送过程
        SendSmsResponse response=client.getAcsResponse(request);
        log.info(response.getMessage());
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1028957.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号