添加阿里云短信平台集成

This commit is contained in:
wangkang
2018-01-31 17:50:14 +08:00
parent 2df76ebf81
commit 92f7dbd4f4
4 changed files with 193 additions and 0 deletions
+31
View File
@@ -29,6 +29,8 @@
<mybatis-spring-boot-starter.version>1.3.1</mybatis-spring-boot-starter.version>
<jsqlparser.version>1.1</jsqlparser.version>
<aliyun-sdk-oss.version>2.8.2</aliyun-sdk-oss.version>
<aliyun-java-sdk-dysmsapi.version>1.0.0</aliyun-java-sdk-dysmsapi.version>
<aliyun-java-sdk-core.version>3.5.0</aliyun-java-sdk-core.version>
<poi.version>3.17</poi.version>
<aes.version>1.0</aes.version>
</properties>
@@ -43,6 +45,7 @@
<module>udf-starter-file</module>
<module>udf-starter-file-aliyun-oss</module>
<module>udf-starter-file-aliyun-oss-api</module>
<module>udf-starter-file-aliyun-dysms-api</module>
<module>udf-starter-mail</module>
<module>udf-starter-scheduler</module>
<module>udf-starter-scheduler-client</module>
@@ -130,6 +133,12 @@
<artifactId>udf-starter-file-aliyun-oss-api</artifactId>
<version>${udf-starter.version}</version>
</dependency>
<!-- udf-starter-file-aliyun-dysms-api -->
<dependency>
<groupId>org.itkk.udf</groupId>
<artifactId>udf-starter-file-aliyun-dysms-api</artifactId>
<version>${udf-starter.version}</version>
</dependency>
<!-- udf-starter-mail -->
<dependency>
<groupId>org.itkk.udf</groupId>
@@ -255,6 +264,17 @@
<layout>default</layout>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
<repository>
<id>maven</id>
<name>maven</name>
<url>http://repo1.maven.org/maven2/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<!-- 指定公司内网仓库 -->
<!-- <repository> <id>egridcloud-nexus</id> <name>Nexus egridcloud</name> <layout>default</layout> <url>http://192.168.1.11:8081/nexus/content/groups/public/</url>
</repository> -->
@@ -274,6 +294,17 @@
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>maven</id>
<name>maven</name>
<url>http://repo1.maven.org/maven2/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<!-- 指定公司内网仓库 -->
<!-- <pluginRepository> <id>egridcloud-nexus</id> <name>Nexus egridcloud</name> <url>http://192.168.1.11:8081/nexus/content/groups/public/</url>
<layout>default</layout> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots>
+41
View File
@@ -0,0 +1,41 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<!-- maven坐标 -->
<artifactId>udf-starter-file-aliyun-dysms-api</artifactId>
<name>udf-starter-file-aliyun-dysms-api</name>
<packaging>jar</packaging>
<url>http://itkk.org:81</url>
<!-- 父依赖 -->
<parent>
<groupId>org.itkk.udf</groupId>
<artifactId>udf-starter</artifactId>
<version>1.0</version>
</parent>
<!-- 依赖 -->
<dependencies>
<!-- udf-core-starter -->
<dependency>
<groupId>org.itkk.udf</groupId>
<artifactId>udf-starter-core</artifactId>
</dependency>
<!-- udf-starter-cache-redis -->
<dependency>
<groupId>org.itkk.udf</groupId>
<artifactId>udf-starter-cache-redis</artifactId>
</dependency>
<!-- aliyun-java-sdk-sms -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>${aliyun-java-sdk-core.version}</version>
</dependency>
<!-- aliyun-java-sdk-dysmsapi -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>${aliyun-java-sdk-dysmsapi.version}</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,40 @@
/**
* Config.java
* Created at 2016-11-19
* Created by Administrator
* Copyright (C) 2016 itkk.org, All rights reserved.
*/
package org.itkk.udf.file.aliyun.dysms.api;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.io.Serializable;
/**
* 描述 : AliyunDysmsProperties
*
* @author Administrator
*/
@Component
@ConfigurationProperties(prefix = "org.itkk.file.aliyun.dysms.properties")
@Data
public class AliyunDysmsProperties implements Serializable {
/**
* 描述 : ID
*/
private static final long serialVersionUID = 1L;
/**
* accessKeyId
*/
private String accessKeyId;
/**
* accessKeySecret
*/
private String accessKeySecret;
}
@@ -0,0 +1,81 @@
package org.itkk.udf.file.aliyun.dysms.api;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.UUID;
/**
* DysmsWarpper
*/
@Component
public class DysmsWarpper {
/**
* aliyunDysmsProperties
*/
@Autowired
private AliyunDysmsProperties aliyunDysmsProperties;
/**
* send
*
* @param phoneNumbers phoneNumbers
* @param signName signName
* @param templateCode templateCode
* @param templateParam templateParam
* @return boolean
* @throws ClientException ClientException
*/
public boolean send(String phoneNumbers, String signName, String templateCode, String templateParam) throws ClientException {
//设置返回值
Boolean result = false;
//设置超时时间-可自行调整
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
//初始化ascClient需要的几个参数
final String product = "Dysmsapi"; //短信API产品名称(短信产品名固定,无需修改)
final String domain = "dysmsapi.aliyuncs.com"; //短信API产品域名(接口地址固定,无需修改)
final String regionId = "cn-hangzhou";
//替换成你的AK
final String accessKeyId = aliyunDysmsProperties.getAccessKeyId(); //你的accessKeyId,参考本文档步骤2
final String accessKeySecret = aliyunDysmsProperties.getAccessKeySecret(); //你的accessKeySecret,参考本文档步骤2
//初始化ascClient,暂时不支持多region(请勿修改)
IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint(regionId, regionId, product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
//组装请求对象
SendSmsRequest request = new SendSmsRequest();
//使用post提交
request.setMethod(MethodType.POST);
//必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
request.setPhoneNumbers(phoneNumbers);
//必填:短信签名-可在短信控制台中找到
request.setSignName(signName);
//必填:短信模板-可在短信控制台中找到
request.setTemplateCode(templateCode);
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
//友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败
request.setTemplateParam(templateParam);
//可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
request.setOutId(UUID.randomUUID().toString());
//请求失败这里会抛ClientException异常
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
//请求成功
result = true;
}
//返回
return result;
}
}