短信验证码SMS
支持 腾讯云短信验证码 阿里云短信验证码 网易云信验证码
This commit is contained in:
@@ -277,6 +277,11 @@ subprojects {
|
||||
compile group: 'com.thoughtworks.xstream', name: 'xstream', version: '1.4.10'
|
||||
//local jars
|
||||
compile fileTree(dir: "${rootDir}/maxkey-lib/", include: '*.jar')
|
||||
//阿里云
|
||||
compile group: 'com.aliyun', name: 'aliyun-java-sdk-core', version: '4.5.1'
|
||||
//腾讯云
|
||||
compile group: 'com.tencentcloudapi', name: 'tencentcloud-sdk-java', version: '3.1.33'
|
||||
|
||||
//tomcat embed
|
||||
compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-core', version: '9.0.29'
|
||||
compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-logging-juli', version: '8.5.2'
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
package org.maxkey.crypto.password.opt.impl.sms.netease;
|
||||
|
||||
import com.aliyuncs.CommonRequest;
|
||||
import com.aliyuncs.CommonResponse;
|
||||
import com.aliyuncs.DefaultAcsClient;
|
||||
import com.aliyuncs.IAcsClient;
|
||||
import com.aliyuncs.http.MethodType;
|
||||
import com.aliyuncs.profile.DefaultProfile;
|
||||
import org.maxkey.crypto.password.opt.impl.SmsOtpAuthn;
|
||||
import org.maxkey.domain.UserInfo;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 阿里云短信验证.
|
||||
* @author shimingxy
|
||||
*
|
||||
*/
|
||||
public class SmsOtpAuthnAliyun extends SmsOtpAuthn {
|
||||
private static final Logger logger = LoggerFactory.getLogger(SmsOtpAuthnAliyun.class);
|
||||
|
||||
public SmsOtpAuthnAliyun() {
|
||||
optType = OptTypes.SMS;
|
||||
}
|
||||
|
||||
//请替换你在管理后台应用下申请的accessKeyId
|
||||
private String accessKeyId = "94395d754eb55693043f5d6a2b772ef3";
|
||||
//请替换你在管理后台应用下申请的accessSecret
|
||||
private String accessSecret = "05d5485357bc";
|
||||
// 短信模板ID
|
||||
private String templateCode = "SMS_187590021";
|
||||
|
||||
private String signName = "MaxKey";
|
||||
|
||||
@Override
|
||||
public boolean produce(UserInfo userInfo) {
|
||||
// 手机号
|
||||
String mobile = userInfo.getMobile();
|
||||
if (mobile != null && !mobile.equals("")) {
|
||||
try {
|
||||
DefaultProfile profile = DefaultProfile.getProfile(
|
||||
"cn-hangzhou", accessKeyId, accessSecret);
|
||||
IAcsClient client = new DefaultAcsClient(profile);
|
||||
|
||||
String token = this.genToken(userInfo);
|
||||
CommonRequest request = new CommonRequest();
|
||||
request.setSysMethod(MethodType.POST);
|
||||
request.setSysDomain("dysmsapi.aliyuncs.com");
|
||||
request.setSysVersion("2017-05-25");
|
||||
request.setSysAction("SendSms");
|
||||
request.putQueryParameter("RegionId", "cn-hangzhou");
|
||||
request.putQueryParameter("PhoneNumbers", mobile);
|
||||
request.putQueryParameter("SignName", signName);
|
||||
request.putQueryParameter("TemplateCode", templateCode);
|
||||
request.putQueryParameter("TemplateParam", "{\"code\":\"" + token + "\"}");
|
||||
CommonResponse response = client.getCommonResponse(request);
|
||||
logger.debug("responseString " + response.getData());
|
||||
//成功返回
|
||||
if (response.getData().indexOf("OK") > -1) {
|
||||
this.optTokenStore.store(
|
||||
userInfo,
|
||||
token,
|
||||
userInfo.getMobile(),
|
||||
OptTypes.SMS);
|
||||
return true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(" produce code error ", e);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validate(UserInfo userInfo, String token) {
|
||||
return this.optTokenStore.validate(userInfo, token, OptTypes.SMS, interval);
|
||||
}
|
||||
|
||||
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 getTemplateCode() {
|
||||
return templateCode;
|
||||
}
|
||||
|
||||
public void setTemplateCode(String templateCode) {
|
||||
this.templateCode = templateCode;
|
||||
}
|
||||
|
||||
public String getSignName() {
|
||||
return signName;
|
||||
}
|
||||
|
||||
public void setSignName(String signName) {
|
||||
this.signName = signName;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
package org.maxkey.crypto.password.opt.impl.sms.netease;
|
||||
|
||||
import com.tencentcloudapi.common.Credential;
|
||||
import com.tencentcloudapi.common.profile.ClientProfile;
|
||||
import com.tencentcloudapi.common.profile.HttpProfile;
|
||||
import com.tencentcloudapi.sms.v20190711.SmsClient;
|
||||
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
|
||||
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
|
||||
import org.maxkey.crypto.password.opt.impl.SmsOtpAuthn;
|
||||
import org.maxkey.domain.UserInfo;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
/**
|
||||
* 腾讯云短信验证.
|
||||
* @author shimingxy
|
||||
*
|
||||
*/
|
||||
public class SmsOtpAuthnTencentCloud extends SmsOtpAuthn {
|
||||
private static final Logger logger = LoggerFactory.getLogger(SmsOtpAuthnTencentCloud.class);
|
||||
|
||||
//
|
||||
String secretId;
|
||||
//
|
||||
String secretKey;
|
||||
//短信SDKAPPID
|
||||
String smsSdkAppid;
|
||||
//短信模板
|
||||
String templateId;
|
||||
//签名
|
||||
String sign;
|
||||
|
||||
public SmsOtpAuthnTencentCloud() {
|
||||
optType = OptTypes.SMS;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean produce(UserInfo userInfo) {
|
||||
// 手机号
|
||||
String mobile = userInfo.getMobile();
|
||||
if (mobile != null && !mobile.equals("")) {
|
||||
try {
|
||||
Credential cred = new Credential(secretId, secretKey);
|
||||
|
||||
HttpProfile httpProfile = new HttpProfile();
|
||||
httpProfile.setEndpoint("sms.tencentcloudapi.com");
|
||||
|
||||
ClientProfile clientProfile = new ClientProfile();
|
||||
clientProfile.setHttpProfile(httpProfile);
|
||||
|
||||
SmsClient client = new SmsClient(cred, "ap-beijing", clientProfile);
|
||||
String token = this.genToken(userInfo);
|
||||
String params = "{\"PhoneNumberSet\":[\"" + mobile + "\"],"
|
||||
+ "\"TemplateID\":\"" + templateId + "\",\"Sign\":\"" + sign + "\","
|
||||
+ "\"TemplateParamSet\":[\"" + token + "\",\"" + this.interval + "\"],"
|
||||
+ "\"SmsSdkAppid\":\"" + smsSdkAppid + "\"}";
|
||||
|
||||
SendSmsRequest req = SendSmsRequest.fromJsonString(params, SendSmsRequest.class);
|
||||
|
||||
SendSmsResponse resp = client.SendSms(req);
|
||||
|
||||
logger.debug("responseString " + SendSmsRequest.toJsonString(resp));
|
||||
if (resp.getSendStatusSet()[0].getCode().equalsIgnoreCase("Ok")) {
|
||||
this.optTokenStore.store(
|
||||
userInfo,
|
||||
token,
|
||||
userInfo.getMobile(),
|
||||
OptTypes.SMS);
|
||||
return true;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error(" produce code error ", e);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validate(UserInfo userInfo, String token) {
|
||||
return this.optTokenStore.validate(userInfo, token, OptTypes.SMS, interval);
|
||||
}
|
||||
|
||||
|
||||
public String getSecretId() {
|
||||
return secretId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void setSecretId(String secretId) {
|
||||
this.secretId = secretId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public String getSecretKey() {
|
||||
return secretKey;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void setSecretKey(String secretKey) {
|
||||
this.secretKey = secretKey;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public String getSmsSdkAppid() {
|
||||
return smsSdkAppid;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void setSmsSdkAppid(String smsSdkAppid) {
|
||||
this.smsSdkAppid = smsSdkAppid;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public String getTemplateId() {
|
||||
return templateId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void setTemplateId(String templateId) {
|
||||
this.templateId = templateId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public String getSign() {
|
||||
return sign;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void setSign(String sign) {
|
||||
this.sign = sign;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,7 +19,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 网易云信的短信验证.
|
||||
* 网易云信短信验证.
|
||||
* @author shimingxy
|
||||
*
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user