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

Springboot 实现阿里云发送短信

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

Springboot 实现阿里云发送短信

项目目录

一、导入依赖

	com.aliyun
    aliyun-java-sdk-core
    4.0.6


    com.aliyun
    aliyun-java-sdk-dysmsapi
    1.1.0

二、配置阿里云

在 resources 目录下新建配置文件 oss-conf.properties

# 阿里云配置
# 配置自己的accessKeyId
aliyun.access-key-id=xxxx
# 配置自己的accessKeySecret
aliyun.access-key-secret=xxxx

三、添加配置类

添加配置类 AliYunProperties

@Data
@ConfigurationProperties(prefix = "aliyun")
@PropertySource(value = {"classpath:oss-conf.properties"}, encoding = "UTF-8")
public class AliYunProperties {
    private String accessKeyId;
    private String accessKeySecret;
}
四、激活配置类

新建配置类 EnableConfig

@Configuration
@EnableConfigurationProperties({AliYunOssProperties.class, AliYunProperties.class})
@EnableRetry
public class EnableConfig {
}
五、添加 AliYunSmsService

添加类 AliYunSmsService 和 AliYunSmsServiceImpl

public interface AliYunSmsService {
    
    Boolean sendSms(String phone);
}
@Service
@Slf4j
@RequiredArgsConstructor
public class AliYunSmsServiceImpl implements AliYunSmsService {
    private final AliYunProperties aliYunProperties;

    @Override
    public Boolean sendSms(String phone) {
        // 短信API产品名称(无需修改)
        final String product = "Dysmsapi";
        // 短信API产品域名(无需修改)
        final String domain = "dysmsapi.aliyuncs.com";
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                aliYunProperties.getAccessKeyId(),
                aliYunProperties.getAccessKeySecret());
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        SendSmsRequest sendSmsRequest = getSendSmsRequest(phone);
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(sendSmsRequest);
        log.info("[发送短信]phone={}, result={}", phone, sendSmsResponse.getCode());
        return sendSmsResponse.getCode() != null && "OK".equals(sendSmsResponse.getCode());
    }

    
    private SendSmsRequest getSendSmsRequest(String phone) {
        SendSmsRequest request = new SendSmsRequest();
        request.setMethod(MethodType.POST);
        // 多个手机号逗号分割 上限1000个
        request.setPhoneNumbers(phone);
        // 短信签名(替换自己的签名)
        request.setSignName("连连商城");
        // 短信模板(替换自己的模板)
        request.setTemplateCode("SMS_222860055");
        request.setTemplateParam("{"code": "" + getCode() + ""}");
        return request;
    }

    
    public String getCode() {
        Random random = new Random();
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            result.append(random.nextInt(10));
        }
        return result.toString();
    }

}

至此,就可以使用 AliYunSmsService 来发送短信了!!

六、测试

新建一个 Controller 来测试一下

使用 Postman 发送请求

发送成功

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

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

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