sms 短信服务说明
官网:https://help.aliyun.com/document_detail/57535.html
短信服务
api 短信发送流程
# 短信发送准备:短信签名、短信模板
AddSmsSign:添加短信签名,通过QuerySmsSign查看短信签名状态的审核状态
AddSmsTemplate:添加短信模板,通过QuerySmsTemplate查看短信模板的审核状态
说明:发送短信前需要先申请短信签名、短信模板,并确保短信签名以及模板已经审核通过发送测试短信可使用测试专用的签名、模板,免去了申请流程# 短信发送:单条发送、批量发送
SendSms:单条短信发送,短信群发(向最多1000个不用的手机号发送相同的内容,群发有一定延迟)
SendBatchSms:批量短信发送(向多个不同的手机号码发送不同签名和模板内容的短信)# 短信发送查询
QuerySendDetails:查询短信发送详情
相关依赖
<!-- aliyun sms --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>aliyun-sms-spring-boot-starter</artifactId></dependency><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency><!-- aliyun上下文 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>aliyun-spring-boot-dependencies</artifactId><version>${aliyun-spring-boot.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
相关类与接口
SmsContextAutoConfiguration
@Configuration(proxyBeanMethods = false
)
@EnableConfigurationProperties({SmsProperties.class}) //创建SmsProperties配置bean
@ConditionalOnClass(name = {"com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest"}
)
@ConditionalOnProperty(name = {"alibaba.cloud.sms.enabled"},matchIfMissing = true
)
public class SmsContextAutoConfiguration {public SmsContextAutoConfiguration() {}
}
SmsProperties
@ConfigurationProperties(prefix = "alibaba.cloud.sms"
)
public class SmsProperties {public static final String SMS_PRODUCT = "Dysmsapi";public static final String SMS_DOMAIN = "dysmsapi.aliyuncs.com";private String reportQueueName;private String upQueueName;private String connectTimeout = "10000";private String readTimeout = "10000";public SmsProperties() {}
SmsAutoConfiguration
@Configuration(proxyBeanMethods = false
)
@EnableConfigurationProperties
@ConditionalOnClass({SendSmsRequest.class})
@ConditionalOnProperty(value = {"alibaba.cloud.sms.enabled"},matchIfMissing = true
)
public class SmsAutoConfiguration {public SmsAutoConfiguration() {}@Beanpublic SmsServiceImpl smsService(AliCloudProperties aliCloudProperties, SmsProperties smsProperties) {return new SmsServiceImpl(aliCloudProperties, smsProperties);} //创建SmsServiceImpl@Beanpublic SmsInitializerEventListener smsInitializePostListener(SmsProperties smsProperties, ISmsService smsService) {return new SmsInitializerEventListener(smsProperties, smsService);} //创建SmsInitializerEventListener
}
SmsServiceImpl:短信操作类,如发送、批量发送、查询发送详情等
public final class SmsServiceImpl extends AbstractSmsService {private static final Logger log = LoggerFactory.getLogger(SmsServiceImpl.class);private SmsProperties smsProperties;private AliCloudProperties aliCloudProperties;public SmsServiceImpl(AliCloudProperties aliCloudProperties, SmsProperties smsProperties) {this.aliCloudProperties = aliCloudProperties;this.smsProperties = smsProperties;}public SendSmsResponse sendSmsRequest(SendSmsRequest sendSmsRequest) throws ClientException {public SendSmsResponse sendSmsRequest(SendSmsRequest sendSmsRequest, String accessKeyId, String accessKeySecret) throws ServerException, ClientException {public boolean startSmsReportMessageListener(SmsReportMessageListener smsReportMessageListener) {String messageType = "SmsReport";String queueName = this.smsProperties.getReportQueueName();return this.startReceiveMsg(messageType, queueName, smsReportMessageListener);}public boolean startSmsUpMessageListener(SmsUpMessageListener smsUpMessageListener) {String messageType = "SmsUp";String queueName = this.smsProperties.getUpQueueName();return this.startReceiveMsg(messageType, queueName, smsUpMessageListener);}private boolean startReceiveMsg(String messageType, String queueName, SmsMessageListener messageListener) {public SendBatchSmsResponse sendSmsBatchRequest(SendBatchSmsRequest sendBatchSmsRequest) throws ServerException, ClientException {public SendBatchSmsResponse sendSmsBatchRequest(SendBatchSmsRequest sendBatchSmsRequest, String accessKeyId, String accessKeySecret) throws ClientException {public QuerySendDetailsResponse querySendDetails(QuerySendDetailsRequest request, String accessKeyId, String accessKeySecret) throws ClientException {public QuerySendDetailsResponse querySendDetails(QuerySendDetailsRequest request) throws ClientException {
AbstractSmsService
public abstract class AbstractSmsService implements ISmsService {private ConcurrentHashMap<String, IAcsClient> acsClientConcurrentHashMap = new ConcurrentHashMap();public AbstractSmsService() {}public IAcsClient getHangZhouRegionClientProfile(String accessKeyId, String accessKeySecret) {return (IAcsClient)this.acsClientConcurrentHashMap.computeIfAbsent(this.getKey("cn-hangzhou", accessKeyId, accessKeySecret), (iacsClient) -> {return new DefaultAcsClient(DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret));});}private String getKey(String regionId, String accessKeyId, String accessKeySecret) {return regionId + ":" + accessKeyId + ":" + accessKeySecret;}
}
ISmsService
public interface ISmsService {IAcsClient getHangZhouRegionClientProfile(String accessKeyId, String secret);SendSmsResponse sendSmsRequest(SendSmsRequest sendSmsRequest) throws ServerException, ClientException;SendSmsResponse sendSmsRequest(SendSmsRequest sendSmsRequest, String accessKeyId, String accessKeySecret) throws ServerException, ClientException;SendBatchSmsResponse sendSmsBatchRequest(SendBatchSmsRequest sendBatchSmsRequest) throws ServerException, ClientException;SendBatchSmsResponse sendSmsBatchRequest(SendBatchSmsRequest sendSmsRequest, String accessKeyId, String accessKeySecret) throws ServerException, ClientException;boolean startSmsReportMessageListener(SmsReportMessageListener smsReportMessageListener);boolean startSmsUpMessageListener(SmsUpMessageListener smsUpMessageListener);QuerySendDetailsResponse querySendDetails(QuerySendDetailsRequest request, String accessKeyId, String accessKeySecret) throws ClientException;QuerySendDetailsResponse querySendDetails(QuerySendDetailsRequest request) throws ClientException;
}
AliCloudContextAutoConfiguration
@Configuration(proxyBeanMethods = false
)
@EnableConfigurationProperties({AliCloudProperties.class})
public class AliCloudContextAutoConfiguration {public AliCloudContextAutoConfiguration() {}
}
AliCloudProperties
@ConfigurationProperties("alibaba.cloud")
public class AliCloudProperties implements AliCloudConfiguration {public static final String PROPERTY_PREFIX = "alibaba.cloud";public static final String ACCESS_KEY_PROPERTY = "alibaba.cloud.access-key";public static final String SECRET_KEY_PROPERTY = "alibaba.cloud.secret-key";private String accessKey;private String secretKey;public AliCloudProperties() {}