123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- package com.llisoft.sms.service;
- import java.util.Objects;
- import javax.annotation.PostConstruct;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.scheduling.annotation.Async;
- import org.springframework.stereotype.Service;
- 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.sms.util.JsonUtil;
- /**
- * 阿里云SMS接口调用
- * https://help.aliyun.com/document_detail/101414.html
- * https://api.aliyun.com/?product=Dysmsapi&lang=JAVA#/
- * @author YangJie [2019年2月18日]
- */
- @Service
- public class AliyunService {
-
- private Logger logger = LoggerFactory.getLogger(AliyunService.class);
-
- @Value("${sms.accessKeyId}")
- private String accessKeyId;
- @Value("${sms.accessKeySecret}")
- private String accessKeySecret;
-
- // API客户端
- private IAcsClient smsClient;
- private IAcsClient afsClient;
-
-
- @PostConstruct
- public void init() { // 初始化
- 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));
- }
-
- /**
- * 发送短信
- * @param phone
- * @param code
- * @param type 类型(0麦塔、1青谷)
- */
- @Async
- public void sms(String sign, String phone, String code) {
- logger.info("阿里云发送验证码: {} : {}", phone, code);
- CommonRequest request = new CommonRequest();
- // 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", sign); // 签名
- request.putQueryParameter("TemplateCode", "SMS_216836530"); // 模板ID
- request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
- try {
- CommonResponse response = smsClient.getCommonResponse(request);
- logger.info("阿里云短信API返回: {}", JsonUtil.toJson(response));
- } catch (Exception e) {
- logger.error("阿里云短信API异常", e);
- }
- }
-
- /**
- * 验证码
- * @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;
- }
- }
|