SpringBoot--自动装配--阿里云短信模板

步骤:

依赖:


    <!-- 阿里云核心包 -->
    <dependency>
		<groupId>com.aliyun</groupId>
		<artifactId>aliyun-java-sdk-core</artifactId>
		<version>4.0.6</version>
	</dependency>
    <!-- 阿里云短信包 -->
	<dependency>
		<groupId>com.aliyun</groupId>
		<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
		<version>1.1.0</version>
    <dependency>

        1、编写属性类---SmsProperties

/**
 * 阿里云短信属性类
 */
@Data
@ConfigurationProperties(prefix = "XXX.sms")
public class SmsProperties {
    // 签名名称
    private String signName;
    // 模版CODE
    private String templateCode;
    // AccessKey ID
    private String accessKey;
    // AccessKey Secret
    private String secret;
}

        2、定义短信模板工具类---SmsTemplate

/**
 * 阿里云短信模板工具类
 */
public class SmsTemplate {

    private SmsProperties properties;

    public SmsTemplate(SmsProperties properties) {
        this.properties = properties;
    }

    /**
     * 调用阿里云平台发送短信
     */
    public void sendSms(String phoneNumbers, String code) {

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

        try {
            // 初始化acsClient, 暂不支持region化
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", properties.getAccessKey(), properties.getSecret());

            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com");

            IAcsClient acsClient = new DefaultAcsClient(profile);

            // 组装请求对象-具体描述见控制台-文档部分内容
            SendSmsRequest request = new SendSmsRequest();
            // 必填:待发送手机号
            request.setPhoneNumbers(phoneNumbers);
            // 必填:短信签名-可在短信控制台中找到
            request.setSignName(properties.getSignName());
            // 必填:短信模板-可在短信控制台中找到
            request.setTemplateCode(properties.getTemplateCode());
            // 可选:模板中的变量替换JSON串, 如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
            request.setTemplateParam("{\"code\":\"" + code + "\"}");

            // 选填-上行短信扩展码(无特殊需求用户请忽略此字段)
            // request.setSmsUpExtendCode("90997");
            // 可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
            request.setOutId("yourOutId");

            // hint 此处可能会抛出异常,注意catch
            acsClient.getAcsResponse(request);

        } catch (Exception e) {

        }
    }
}

        3、编写自动配置类---AutoConfiguration

@Configuration
@EnableConfigurationProperties({  // 指定某类生成bean
        SmsProperties.class
})
public class TanhuaAutoConfiguration {

    /**
     * 将返回值变成bean放到spring容器中
     */
    @Bean
    public SmsTemplate smsTemplate(SmsProperties smsProperties) {
        SmsTemplate smsTemplate = new SmsTemplate(smsProperties);
        return smsTemplate;
    }
}

        4、自动装配配置 ---resources目录下创建 META-INF/spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.config.AutoConfiguration

        5、application.yml配置(阿里云AccessKey ID、Secret 等)

XXX:
  sms:
    signName: 
    templateCode: 
    accessKey: 
    secret: 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值