Skip to content

Commit

Permalink
替换腾讯云短信
Browse files Browse the repository at this point in the history
  • Loading branch information
heavyrain2012 committed Dec 6, 2021
1 parent afca47c commit e36b757
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 51 deletions.
11 changes: 5 additions & 6 deletions config/tencent_sms.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
sms.appid=1422232392
sms.appkey=be36a26f23fa111c26aab3fd39b4d4a5
sms.templateId=222222
sms.sign=野火IM
#super code放到application.properites文件中了
#sms.superCode=66666
sms.secretId=AKIsaepMSEL91dsMESAUMO2smphIdgSxB8oD
sms.secretKey=91dADocdksuw23AEFCD78lsdudf35ta0
sms.appId=1432000001
sms.templateId=592276
sms.sign=北京野火无限网络科技
11 changes: 8 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@
</dependency>

<dependency>
<groupId>com.github.qcloudsms</groupId>
<artifactId>qcloudsms</artifactId>
<version>1.0.6</version>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<version>3.1.410</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -188,6 +188,11 @@
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>

<build>
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/cn/wildfirechat/app/ServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -207,7 +206,7 @@ public RestResult sendCode(String mobile) {
authDataSource.clearRecode(mobile);
return RestResult.error(restCode);
}
} catch (JSONException e) {
} catch (Exception e) {
// json解析错误
e.printStackTrace();
authDataSource.clearRecode(mobile);
Expand Down
110 changes: 94 additions & 16 deletions src/main/java/cn/wildfirechat/app/sms/SmsServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@
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 com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -53,19 +57,93 @@ public RestResult.RestCode sendCode(String mobile, String code) {

private RestResult.RestCode sendTencentCode(String mobile, String code) {
try {
String[] params = {code};
SmsSingleSender ssender = new SmsSingleSender(mTencentSMSConfig.appid, mTencentSMSConfig.appkey);
SmsSingleSenderResult result = ssender.sendWithParam("86", mobile,
mTencentSMSConfig.templateId, params, mTencentSMSConfig.sign, "", "");
if (result.result == 0) {
return RestResult.RestCode.SUCCESS;
} else {
LOG.error("Failure to send SMS {}", result);
return RestResult.RestCode.ERROR_SERVER_ERROR;
}
} catch (HTTPException e) {
e.printStackTrace();
} catch (IOException e) {
/* 必要步骤:
* 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey。
* 这里采用的是从环境变量读取的方式,需要在环境变量中先设置这两个值。
* 你也可以直接在代码中写死密钥对,但是小心不要将代码复制、上传或者分享给他人,
* 以免泄露密钥对危及你的财产安全。
* CAM密匙查询: https://console.cloud.tencent.com/cam/capi*/
Credential cred = new Credential(mTencentSMSConfig.secretId, mTencentSMSConfig.secretKey);

// 实例化一个http选项,可选,没有特殊需求可以跳过
HttpProfile httpProfile = new HttpProfile();
// 设置代理
// httpProfile.setProxyHost("真实代理ip");
// httpProfile.setProxyPort(真实代理端口);
/* SDK默认使用POST方法。
* 如果你一定要使用GET方法,可以在这里设置。GET方法无法处理一些较大的请求 */
httpProfile.setReqMethod("POST");
/* SDK有默认的超时时间,非必要请不要进行调整
* 如有需要请在代码中查阅以获取最新的默认值 */
httpProfile.setConnTimeout(60);
/* SDK会自动指定域名。通常是不需要特地指定域名的,但是如果你访问的是金融区的服务
* 则必须手动指定域名,例如sms的上海金融区域名: sms.ap-shanghai-fsi.tencentcloudapi.com */
httpProfile.setEndpoint("sms.tencentcloudapi.com");

/* 非必要步骤:
* 实例化一个客户端配置对象,可以指定超时时间等配置 */
ClientProfile clientProfile = new ClientProfile();
/* SDK默认用TC3-HMAC-SHA256进行签名
* 非必要请不要修改这个字段 */
clientProfile.setSignMethod("HmacSHA256");
clientProfile.setHttpProfile(httpProfile);
/* 实例化要请求产品(以sms为例)的client对象
* 第二个参数是地域信息,可以直接填写字符串ap-guangzhou,或者引用预设的常量 */
SmsClient client = new SmsClient(cred, "ap-guangzhou",clientProfile);
/* 实例化一个请求对象,根据调用的接口和实际情况,可以进一步设置请求参数
* 你可以直接查询SDK源码确定接口有哪些属性可以设置
* 属性可能是基本类型,也可能引用了另一个数据结构
* 推荐使用IDE进行开发,可以方便的跳转查阅各个接口和数据结构的文档说明 */
SendSmsRequest req = new SendSmsRequest();

/* 填充请求参数,这里request对象的成员变量即对应接口的入参
* 你可以通过官网接口文档或跳转到request对象的定义处查看请求参数的定义
* 基本类型的设置:
* 帮助链接:
* 短信控制台: https://console.cloud.tencent.com/smsv2
* sms helper: https://cloud.tencent.com/document/product/382/3773 */

/* 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId,示例如1400006666 */
req.setSmsSdkAppId(mTencentSMSConfig.appId);

/* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名,签名信息可登录 [短信控制台] 查看 */
req.setSignName(mTencentSMSConfig.sign);

/* 国际/港澳台短信 SenderId: 国内短信填空,默认未开通,如需开通请联系 [sms helper] */
String senderid = "";
req.setSenderId(senderid);

/* 用户的 session 内容: 可以携带用户侧 ID 等上下文信息,server 会原样返回 */
String sessionContext = "xxx";
req.setSessionContext(sessionContext);

/* 短信号码扩展号: 默认未开通,如需开通请联系 [sms helper] */
String extendCode = "";
req.setExtendCode(extendCode);

/* 模板 ID: 必须填写已审核通过的模板 ID。模板ID可登录 [短信控制台] 查看 */
req.setTemplateId(mTencentSMSConfig.templateId);

/* 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]
* 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号 */
String[] phoneNumberSet = {mobile};
req.setPhoneNumberSet(phoneNumberSet);

/* 模板参数: 若无模板参数,则设置为空 */
String[] templateParamSet = {code};
req.setTemplateParamSet(templateParamSet);

/* 通过 client 对象调用 SendSms 方法发起请求。注意请求方法名与请求对象是对应的
* 返回的 res 是一个 SendSmsResponse 类的实例,与请求对象对应 */
SendSmsResponse res = client.SendSms(req);

// 输出json格式的字符串回包
System.out.println(SendSmsResponse.toJsonString(res));

// 也可以取出单个值,你可以通过官网接口文档或跳转到response对象的定义处查看返回字段的定义
System.out.println(res.getRequestId());
return RestResult.RestCode.SUCCESS;
} catch (TencentCloudSDKException e) {
e.printStackTrace();
}
return RestResult.RestCode.ERROR_SERVER_ERROR;
Expand Down
46 changes: 23 additions & 23 deletions src/main/java/cn/wildfirechat/app/sms/TencentSMSConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,41 @@
@ConfigurationProperties(prefix="sms")
@PropertySource(value = "file:config/tencent_sms.properties", encoding = "UTF-8")
public class TencentSMSConfig {
int appid;
String appkey;
int templateId;
String sign;
String superCode;
public String secretId;
public String secretKey;
public String appId;
public String templateId;
public String sign;

public int getAppid() {
return appid;
public String getSecretId() {
return secretId;
}

public void setAppid(int appid) {
this.appid = appid;
public void setSecretId(String secretId) {
this.secretId = secretId;
}

public String getAppkey() {
return appkey;
public String getSecretKey() {
return secretKey;
}

public void setAppkey(String appkey) {
this.appkey = appkey;
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}

public int getTemplateId() {
public String getAppId() {
return appId;
}

public void setAppId(String appId) {
this.appId = appId;
}

public String getTemplateId() {
return templateId;
}

public void setTemplateId(int templateId) {
public void setTemplateId(String templateId) {
this.templateId = templateId;
}

Expand All @@ -45,12 +53,4 @@ public String getSign() {
public void setSign(String sign) {
this.sign = sign;
}

public String getSuperCode() {
return superCode;
}

public void setSuperCode(String superCode) {
this.superCode = superCode;
}
}

0 comments on commit e36b757

Please sign in to comment.