Explorar el Código

添加阿里云短信配置

heavyrain2012 hace 5 años
padre
commit
3f463d059f

+ 6 - 1
README.md

@@ -10,7 +10,8 @@ mvn package
 应用使用的是腾讯云短信功能,需要申请到```appid/appkey/templateId```这三个参数,并配置到```tencent_sms.properties```中去。用户也可以自行更换为自己喜欢的短信提供商。在没有短信供应商的情况下,为了测试可以使用```superCode```,设置好后,客户端可以直接使用```superCode```进行登陆。上线时一定要注意删掉```superCode```。
 
 #### 修改配置
-本演示服务有3个配置文件在工程的```config```目录下,分别是```application.properties```, ```im.properties```和```tencent_sms.properties```。请正确配置放到jar包所在的目录下的```config```目录下。
+本演示服务有4个配置文件在工程的```config```目录下,分别是```application.properties```, ```im.properties```, ```aliyun_sms.properties```和```tencent_sms.properties```。请正确配置放到jar包所在的目录下的```config```目录下。
+> ```application.properties```配置中的```sms.verdor```决定是使用那个短信服务商,1为腾讯短信,2为阿里云短信
 
 #### 运行
 在```target```目录找到```app-XXXX.jar```,把jar包和放置配置文件的```config```目录放到一起,然后执行下面命令:
@@ -23,3 +24,7 @@ java -jar app-XXXXX.jar
 
 #### LICENSE
 UNDER MIT LICENSE. 详情见LICENSE文件
+
+
+#### 使用阿里云短信
+请参考说明[使用阿里云短信](./aliyun_sms.md)

+ 31 - 0
aliyun_sms.md

@@ -0,0 +1,31 @@
+# 阿里云短信功能说明
+
+## 短信对接
+1. 在[这里](https://usercenter.console.aliyun.com/#/manage/ak)申请阿里云***accessKeyId***和***accessSecret***
+2. 开通短信服务,并申请短信签名和短信模版。注意申请短信签名和模版都是需要审核的,可以同时申请,以便节省您的时间
+3. 修改```config```目录下的```aliyun_sms.properities```,填入上述四个参数。比如
+    ```$xslt
+    alisms.accessKeyId=LTXXXXXXXXXXXXtW
+    alisms.accessSecret=4pXXXXXXXXXXXXXXXXXXXXXXXXXXXXyU
+    alisms.signName=野火IM
+    alisms.templateCode=SMS_170000000
+    ```
+4. 修改默认使用阿里云短信,在```application.properites```文件中修改```sms.vendor```为***2***
+5. 运行测试。
+
+> 上述几个参数如果不明白,可以参考[阿里云文档](https://help.aliyun.com/document_detail/55284.html?spm=a2c4e.11153987.0.0.5861aeecePRLPH)
+
+## 迁移阿里云短信功能
+指导如何把阿里云短信功能迁移到客户应用服务中
+1. 引入jar包
+    ```$xslt
+    <dependency>
+        <groupId>com.aliyun</groupId>
+        <artifactId>aliyun-java-sdk-core</artifactId>
+        <version>4.1.0</version>
+    </dependency>
+    ```
+
+2. 拷贝除了```Application.java```以外的所有源码到客户应用服务器.
+
+3. 拷贝配置文件到客户应用服务,需要注意配置文件会依赖特定的路径,请放置正确的路径

+ 4 - 0
config/aliyun_sms.properties

@@ -0,0 +1,4 @@
+alisms.accessKeyId=MTAI82gOTQQTuKtW
+alisms.accessSecret=4p7HlgMTOQWHsX82IICabcea556677
+alisms.signName=\u91CE\u706BIM
+alisms.templateCode=SMS_170843232

+ 3 - 0
config/application.properties

@@ -1,4 +1,7 @@
+spring.message.encoding=UTF-8
 server.port=8888
+
+# 短信服务提供商,1是腾讯,2是阿里云
 sms.verdor=1
 sms.super_code=66666
 

+ 7 - 0
pom.xml

@@ -134,6 +134,13 @@
 			<systemPath>${project.basedir}/src/lib/common-${project.version}.jar</systemPath>
 		</dependency>
 
+
+		<dependency>
+			<groupId>com.aliyun</groupId>
+			<artifactId>aliyun-java-sdk-core</artifactId>
+			<version>4.1.0</version>
+		</dependency>
+
 	</dependencies>
 
 	<build>

+ 47 - 0
src/main/java/cn/wildfirechat/app/AliyunSMSConfig.java

@@ -0,0 +1,47 @@
+package cn.wildfirechat.app;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.PropertySource;
+
+@Configuration
+@ConfigurationProperties(prefix="alisms")
+@PropertySource(value = "file:config/aliyun_sms.properties",encoding = "utf-8")
+public class AliyunSMSConfig {
+    String accessKeyId;
+    String accessSecret;
+    String signName;
+    String templateCode;
+
+    public String getAccessKeyId() {
+        return accessKeyId;
+    }
+
+    public void setAccessKeyId(String accessKeyId) {
+        this.accessKeyId = accessKeyId;
+    }
+
+    public String getAccessSecret() {
+        return accessSecret;
+    }
+
+    public void setAccessSecret(String accessSecret) {
+        this.accessSecret = accessSecret;
+    }
+
+    public String getSignName() {
+        return signName;
+    }
+
+    public void setSignName(String signName) {
+        this.signName = signName;
+    }
+
+    public String getTemplateCode() {
+        return templateCode;
+    }
+
+    public void setTemplateCode(String templateCode) {
+        this.templateCode = templateCode;
+    }
+}

+ 57 - 1
src/main/java/cn/wildfirechat/app/SmsServiceImpl.java

@@ -1,8 +1,17 @@
 package cn.wildfirechat.app;
 
+import com.aliyuncs.CommonRequest;
+import com.aliyuncs.CommonResponse;
+import com.aliyuncs.DefaultAcsClient;
+import com.aliyuncs.IAcsClient;
+import com.aliyuncs.exceptions.ClientException;
+import com.aliyuncs.exceptions.ServerException;
+import com.aliyuncs.http.MethodType;
+import com.aliyuncs.profile.DefaultProfile;
 import com.github.qcloudsms.SmsSingleSender;
 import com.github.qcloudsms.SmsSingleSenderResult;
 import com.github.qcloudsms.httpclient.HTTPException;
+import com.google.gson.Gson;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -16,22 +25,32 @@ public class SmsServiceImpl implements SmsService {
     private static final Logger LOG = LoggerFactory.getLogger(SmsServiceImpl.class);
 
 
+    private static class AliyunCommonResponse {
+        String Message;
+        String Code;
+    }
+
     @Value("${sms.verdor}")
     private int smsVerdor;
 
     @Autowired
     private TencentSMSConfig mTencentSMSConfig;
 
+    @Autowired
+    private AliyunSMSConfig aliyunSMSConfig;
+
     @Override
     public RestResult.RestCode sendCode(String mobile, String code) {
         if (smsVerdor == 1) {
             return sendTencentCode(mobile, code);
+        } else if(smsVerdor == 2) {
+            return sendAliyunCode(mobile, code);
         } else {
             return RestResult.RestCode.ERROR_SERVER_NOT_IMPLEMENT;
         }
     }
 
-    public RestResult.RestCode sendTencentCode(String mobile, String code) {
+    private RestResult.RestCode sendTencentCode(String mobile, String code) {
         try {
             String[] params = {code};
             SmsSingleSender ssender = new SmsSingleSender(mTencentSMSConfig.appid, mTencentSMSConfig.appkey);
@@ -51,4 +70,41 @@ public class SmsServiceImpl implements SmsService {
         return RestResult.RestCode.ERROR_SERVER_ERROR;
     }
 
+    private RestResult.RestCode sendAliyunCode(String mobile, String code) {
+        DefaultProfile profile = DefaultProfile.getProfile("default", aliyunSMSConfig.getAccessKeyId(), aliyunSMSConfig.getAccessSecret());
+        IAcsClient client = new DefaultAcsClient(profile);
+
+        String templateparam = "{\"code\":\"" + code + "\"}";
+        CommonRequest request = new CommonRequest();
+        request.setMethod(MethodType.POST);
+        request.setDomain("dysmsapi.aliyuncs.com");
+        request.setVersion("2017-05-25");
+        request.setAction("SendSms");
+        request.putQueryParameter("PhoneNumbers", mobile);
+        request.putQueryParameter("SignName", aliyunSMSConfig.getSignName());
+        request.putQueryParameter("TemplateCode", aliyunSMSConfig.getTemplateCode());
+        request.putQueryParameter("TemplateParam", templateparam);
+        try {
+            CommonResponse response = client.getCommonResponse(request);
+            System.out.println(response.getData());
+            if (response.getData() != null) {
+                AliyunCommonResponse aliyunCommonResponse = new Gson().fromJson(response.getData(), AliyunCommonResponse.class);
+                if (aliyunCommonResponse != null) {
+                    if (aliyunCommonResponse.Code.equalsIgnoreCase("OK")) {
+                        return RestResult.RestCode.SUCCESS;
+                    } else {
+                        System.out.println("Send aliyun sms failure with message:" + aliyunCommonResponse.Message);
+                    }
+                }
+            }
+        } catch (ServerException e) {
+            e.printStackTrace();
+        } catch (ClientException e) {
+            e.printStackTrace();
+        }
+
+
+        return RestResult.RestCode.ERROR_SERVER_ERROR;
+    }
+
 }