一、引入maven
< dependency>
< groupId> com. aliyun< / groupId>
< artifactId> aliyun- java- sdk- dysmsapi< / artifactId>
< version> 1.1 .0 < / version>
< / dependency>
< dependency>
< groupId> com. aliyun< / groupId>
< artifactId> aliyun- java- sdk- core< / artifactId>
< version> 4.0 .6 < / version>
< / dependency>
二、注入短信服务依赖
< bean id= "iSmsService" class = "com.xunwei.sbms.service.impl.SmsService" scope= "request" >
< property name= "accessKeyId" value= "" > < / property>
< property name= "accessKeySecret" value= "" > < / property>
< property name= "signName" value= "" > < / property>
< property name= "templateCode" value= "" > < / property>
< property name= "product" value= "" > < / property>
< property name= "domain" value= "dysmsapi.aliyuncs.com" > < / property>
< / bean>
三、具体实现
1.发送消息
public static SendSmsResponse sendSmsInfo ( String phone) throws ClientException {
System. setProperty ( "sun.net.client.defaultConnectTimeout" , "10000" ) ;
System. setProperty ( "sun.net.client.defaultReadTimeout" , "10000" ) ;
IClientProfile profile = DefaultProfile. getProfile ( "cn-hangzhou" , accessKeyId, accessKeySecret) ;
DefaultProfile. addEndpoint ( "cn-hangzhou" , "cn-hangzhou" , product, domain) ;
IAcsClient acsClient = new DefaultAcsClient ( profile) ;
SendSmsRequest request = new SendSmsRequest ( ) ;
request. setPhoneNumbers ( phone) ;
request. setSignName ( signName) ;
request. setTemplateCode ( templateCode) ;
String code = getCode ( ) ;
VerifyCodeMapDate. put ( phone, new Date ( ) . getTime ( ) ) ;
JSONObject codeJson = new JSONObject ( ) ;
codeJson. put ( "code" , code) ;
request. setTemplateParam ( codeJson. toJSONString ( ) ) ;
request. setOutId ( "yourOutId" ) ;
SendSmsResponse sendSmsResponse = acsClient. getAcsResponse ( request) ;
return sendSmsResponse;
}
2.校验信息
if ( VerifyCodeMapDate. size ( ) != 0 && VerifyCodeMapDate. containsKey ( phone+ "_" + code) ) {
Long time = new Date ( ) . getTime ( ) ;
Long date = VerifyCodeMapDate. get ( phone) ;
if ( time- date <= 2 * 60 * 1000 ) {
result. setCode ( 0 ) ;
result. setData ( "验证码正确" ) ;
} else {
result. setCode ( - 3 ) ;
result. setData ( "验证码已过期" ) ;
}
} else {
result. setCode ( - 1 ) ;
result. setData ( "验证码错误" ) ;
}
public static QuerySendDetailsResponse querySendDetails ( String bizId, String phone) throws ClientException {
System. setProperty ( "sun.net.client.defaultConnectTimeout" , "10000" ) ;
System. setProperty ( "sun.net.client.defaultReadTimeout" , "10000" ) ;
IClientProfile profile = DefaultProfile. getProfile ( "cn-hangzhou" , accessKeyId, accessKeySecret) ;
DefaultProfile. addEndpoint ( "cn-hangzhou" , "cn-hangzhou" , product, domain) ;
IAcsClient acsClient = new DefaultAcsClient ( profile) ;
QuerySendDetailsRequest request = new QuerySendDetailsRequest ( ) ;
request. setPhoneNumber ( phone) ;
request. setBizId ( bizId) ;
SimpleDateFormat ft = new SimpleDateFormat ( "yyyyMMdd" ) ;
request. setSendDate ( ft. format ( new Date ( ) ) ) ;
request. setPageSize ( 10 L) ;
request. setCurrentPage ( 1 L) ;
QuerySendDetailsResponse querySendDetailsResponse = acsClient. getAcsResponse ( request) ;
return querySendDetailsResponse;
}