|
@@ -8,6 +8,7 @@ import com.aliyuncs.exceptions.ClientException;
|
|
|
import com.aliyuncs.exceptions.ServerException;
|
|
import com.aliyuncs.exceptions.ServerException;
|
|
|
import com.aliyuncs.http.MethodType;
|
|
import com.aliyuncs.http.MethodType;
|
|
|
import com.aliyuncs.profile.DefaultProfile;
|
|
import com.aliyuncs.profile.DefaultProfile;
|
|
|
|
|
+import com.java110.utils.cache.MappingCache;
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
@@ -20,6 +21,7 @@ pom.xml
|
|
|
<version>4.0.3</version>
|
|
<version>4.0.3</version>
|
|
|
</dependency>
|
|
</dependency>
|
|
|
*/
|
|
*/
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Created by wuxw on 2019/3/23.
|
|
* Created by wuxw on 2019/3/23.
|
|
|
*/
|
|
*/
|
|
@@ -31,48 +33,56 @@ public class AliSendMessageFactory {
|
|
|
|
|
|
|
|
public final static int DEFAULT_MESSAGE_CODE_LENGTH = 6;
|
|
public final static int DEFAULT_MESSAGE_CODE_LENGTH = 6;
|
|
|
|
|
|
|
|
|
|
+ public final static String ALI_SMS_DOMAIN = "ALI_SMS";
|
|
|
|
|
+
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 生成6位短信码
|
|
* 生成6位短信码
|
|
|
|
|
+ *
|
|
|
* @return
|
|
* @return
|
|
|
*/
|
|
*/
|
|
|
- public static String generateMessageCode(){
|
|
|
|
|
|
|
+ public static String generateMessageCode() {
|
|
|
return generateMessageCode(DEFAULT_MESSAGE_CODE_LENGTH);
|
|
return generateMessageCode(DEFAULT_MESSAGE_CODE_LENGTH);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 生成验证码
|
|
* 生成验证码
|
|
|
|
|
+ *
|
|
|
* @param limit 位数
|
|
* @param limit 位数
|
|
|
* @return
|
|
* @return
|
|
|
*/
|
|
*/
|
|
|
- public static String generateMessageCode(int limit){
|
|
|
|
|
|
|
+ public static String generateMessageCode(int limit) {
|
|
|
Random random = new Random();
|
|
Random random = new Random();
|
|
|
- String result="";
|
|
|
|
|
- for (int i=0;i<limit;i++)
|
|
|
|
|
- {
|
|
|
|
|
- result+=random.nextInt(10);
|
|
|
|
|
|
|
+ String result = "";
|
|
|
|
|
+ for (int i = 0; i < limit; i++) {
|
|
|
|
|
+ result += random.nextInt(10);
|
|
|
}
|
|
}
|
|
|
return result;
|
|
return result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static void sendMessage(String tel,String code) {
|
|
|
|
|
|
|
+ public static void sendMessage(String tel, String code) {
|
|
|
|
|
|
|
|
//开始发送验证码
|
|
//开始发送验证码
|
|
|
- logger.debug("发送号码为{},短信码为{}",tel,code);
|
|
|
|
|
- DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
|
|
|
|
|
|
|
+ logger.debug("发送号码为{},短信码为{}", tel, code);
|
|
|
|
|
+ DefaultProfile profile = DefaultProfile.getProfile(MappingCache.getValue(ALI_SMS_DOMAIN, "region"),
|
|
|
|
|
+ MappingCache.getValue(ALI_SMS_DOMAIN, "accessKeyId"),
|
|
|
|
|
+ MappingCache.getValue(ALI_SMS_DOMAIN, "accessSecret"));
|
|
|
IAcsClient client = new DefaultAcsClient(profile);
|
|
IAcsClient client = new DefaultAcsClient(profile);
|
|
|
|
|
|
|
|
CommonRequest request = new CommonRequest();
|
|
CommonRequest request = new CommonRequest();
|
|
|
- //request.setProtocol(ProtocolType.HTTPS);
|
|
|
|
|
- request.setMethod(MethodType.POST);
|
|
|
|
|
- request.setDomain("dysmsapi.aliyuncs.com");
|
|
|
|
|
- request.setVersion("2017-05-25");
|
|
|
|
|
- request.setAction("SendSms");
|
|
|
|
|
- request.putQueryParameter("RegionId", "cn-hangzhou");
|
|
|
|
|
|
|
+ request.setSysMethod(MethodType.POST);
|
|
|
|
|
+ request.setSysDomain("dysmsapi.aliyuncs.com");
|
|
|
|
|
+ request.setSysVersion("2017-05-25");
|
|
|
|
|
+ request.setSysAction("SendSms");
|
|
|
|
|
+ request.putQueryParameter("RegionId", MappingCache.getValue(ALI_SMS_DOMAIN, "region"));
|
|
|
request.putQueryParameter("PhoneNumbers", tel);
|
|
request.putQueryParameter("PhoneNumbers", tel);
|
|
|
|
|
+ request.putQueryParameter("SignName", MappingCache.getValue(ALI_SMS_DOMAIN, "signName"));
|
|
|
|
|
+ request.putQueryParameter("TemplateCode", MappingCache.getValue(ALI_SMS_DOMAIN, "TemplateCode"));
|
|
|
|
|
+ request.putQueryParameter("TemplateParam", "{\"code\":" + code + "}");
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
CommonResponse response = client.getCommonResponse(request);
|
|
CommonResponse response = client.getCommonResponse(request);
|
|
|
- logger.debug("发送验证码信息:{}",response.getData());
|
|
|
|
|
|
|
+ logger.debug("发送验证码信息:{}", response.getData());
|
|
|
} catch (ServerException e) {
|
|
} catch (ServerException e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
} catch (ClientException e) {
|
|
} catch (ClientException e) {
|