|
@@ -1,5 +1,7 @@
|
|
|
package com.llisoft.service.sms.service;
|
|
|
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
@@ -12,8 +14,12 @@ import com.aliyuncs.CommonRequest;
|
|
|
import com.aliyuncs.CommonResponse;
|
|
|
import com.aliyuncs.DefaultAcsClient;
|
|
|
import com.aliyuncs.IAcsClient;
|
|
|
+import com.aliyuncs.afs.model.v20180112.AuthenticateSigRequest;
|
|
|
+import com.aliyuncs.afs.model.v20180112.AuthenticateSigResponse;
|
|
|
+import com.aliyuncs.exceptions.ClientException;
|
|
|
import com.aliyuncs.http.MethodType;
|
|
|
import com.aliyuncs.profile.DefaultProfile;
|
|
|
+import com.llisoft.common.util.JsonUtil;
|
|
|
|
|
|
/**
|
|
|
* 阿里云SMS接口调用
|
|
@@ -32,12 +38,15 @@ public class AliyunService {
|
|
|
private String accessKeySecret;
|
|
|
|
|
|
// API客户端
|
|
|
- private IAcsClient client;
|
|
|
+ private IAcsClient smsClient;
|
|
|
+ private IAcsClient afsClient;
|
|
|
|
|
|
@PostConstruct
|
|
|
public void init() { // 初始化
|
|
|
- DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessKeySecret);
|
|
|
- client = new DefaultAcsClient(profile);
|
|
|
+ smsClient = new DefaultAcsClient(DefaultProfile.getProfile("default", accessKeyId, accessKeySecret));
|
|
|
+ String regionId = "cn-hangzhou"; // 验证码官方文档写死
|
|
|
+ DefaultProfile.addEndpoint(regionId, "afs", "afs.aliyuncs.com");
|
|
|
+ afsClient = new DefaultAcsClient(DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret));
|
|
|
}
|
|
|
|
|
|
|
|
@@ -47,25 +56,53 @@ public class AliyunService {
|
|
|
* @param code
|
|
|
*/
|
|
|
@Async
|
|
|
- public void sendCode(String phone, String code) {
|
|
|
+ public boolean sms(String phone, String code) {
|
|
|
logger.info("阿里云发送验证码: {} : {}", phone, code);
|
|
|
CommonRequest request = new CommonRequest();
|
|
|
- //request.setProtocol(ProtocolType.HTTPS);
|
|
|
- request.setMethod(MethodType.POST);
|
|
|
- request.setAction("SendSms");
|
|
|
- request.setDomain("dysmsapi.aliyuncs.com");
|
|
|
- request.setVersion("2017-05-25"); // 固定字符串
|
|
|
+// request.setSysProtocol(ProtocolType.HTTPS);
|
|
|
+ request.setSysMethod(MethodType.POST);
|
|
|
+ request.setSysAction("SendSms");
|
|
|
+ request.setSysDomain("dysmsapi.aliyuncs.com");
|
|
|
+ request.setSysVersion("2017-05-25"); // 固定字符串
|
|
|
request.putQueryParameter("PhoneNumbers", phone);
|
|
|
request.putQueryParameter("SignName", "大鱼测试"); // 签名
|
|
|
request.putQueryParameter("TemplateCode", "SMS_6792308"); // 模板ID
|
|
|
request.putQueryParameter("TemplateParam", "{\"customer\":\""+code+"\"}");
|
|
|
try {
|
|
|
- CommonResponse response = client.getCommonResponse(request);
|
|
|
- logger.info("阿里云短信API返回: {}", response.getData());
|
|
|
+ CommonResponse response = smsClient.getCommonResponse(request);
|
|
|
+ logger.info("阿里云短信API返回: {}", JsonUtil.toJson(response));
|
|
|
+ return Objects.nonNull(response) && Objects.nonNull(response.getData());
|
|
|
} catch (Exception e) {
|
|
|
logger.error("阿里云短信API异常", e);
|
|
|
}
|
|
|
-
|
|
|
+ return false;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证码
|
|
|
+ * @param sessionId
|
|
|
+ * @param sig
|
|
|
+ * @param token
|
|
|
+ * @param ip
|
|
|
+ * @return
|
|
|
+ * @throws ClientException
|
|
|
+ */
|
|
|
+ public boolean afs(String appkey, String scene, String token, String sessionId, String sig, String ip) {
|
|
|
+ AuthenticateSigRequest request = new AuthenticateSigRequest();
|
|
|
+ request.setAppKey(appkey);// 应用类型标识。必填参数,后端填写。
|
|
|
+ request.setScene(scene);// 场景标识。必填参数,后端填写。
|
|
|
+ request.setToken(token);// 请求唯一标识。必填参数,从前端获取,不可更改。
|
|
|
+ request.setSessionId(sessionId);// 会话ID。必填参数,从前端获取,不可更改,android和ios只传这个参数即可
|
|
|
+ request.setSig(sig);// 签名串。必填参数,从前端获取,不可更改。
|
|
|
+ request.setRemoteIp(ip);// 客户端IP。必填参数,后端填写。
|
|
|
+ try { // response的code枚举:100验签通过,900验签失败
|
|
|
+ AuthenticateSigResponse response = afsClient.getAcsResponse(request);
|
|
|
+ logger.info("阿里云验证码API返回: {}", JsonUtil.toJson(response));
|
|
|
+ return Objects.nonNull(response) && response.getCode()==100;
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("阿里云验证码API异常", e);
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
}
|