安徽省公司短信平台
This commit is contained in:
parent
1b918cbbe7
commit
95d4e9ba5c
Binary file not shown.
|
|
@ -100,6 +100,15 @@
|
|||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- 省公司短信平台sms-util -->
|
||||
<dependency>
|
||||
<groupId>com.ahsbd</groupId>
|
||||
<artifactId>sms-util</artifactId>
|
||||
<version>1.0</version>
|
||||
<scope>system</scope>
|
||||
<systemPath>${project.basedir}/lib/sms-util-1.0.jar</systemPath>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
@ -108,6 +117,9 @@
|
|||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<includeSystemScope>true</includeSystemScope>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ import com.bonus.common.core.constant.SecurityConstants;
|
|||
import com.bonus.common.core.domain.R;
|
||||
import com.bonus.common.core.exception.ServiceException;
|
||||
import com.bonus.common.security.service.EmailService;
|
||||
import com.bonus.common.security.service.SmsService;
|
||||
import com.bonus.system.api.RemoteUserService;
|
||||
import com.bonus.system.api.model.LoginUser;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import com.bonus.common.core.domain.R;
|
|||
import com.bonus.common.core.exception.ServiceException;
|
||||
import com.bonus.common.core.utils.encryption.Sm4Utils;
|
||||
import com.bonus.common.security.service.EmailService;
|
||||
import com.bonus.common.security.service.SmsService;
|
||||
import com.bonus.config.SystemConfig;
|
||||
import com.bonus.system.api.RemoteUserService;
|
||||
import com.bonus.system.api.model.LoginUser;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,56 @@
|
|||
package com.bonus.auth.service;
|
||||
|
||||
import com.bonus.common.core.constant.CacheConstants;
|
||||
import com.bonus.common.core.exception.CaptchaException;
|
||||
import com.bonus.common.core.utils.StringUtils;
|
||||
import com.bonus.common.core.utils.VerificationCodeUtils;
|
||||
import com.bonus.auth.utils.SmsUtils;
|
||||
import com.bonus.common.redis.service.RedisService;
|
||||
import com.bonus.common.security.config.VerificationCodeConfig;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.bonus.common.core.utils.VerificationCodeUtils.CodeType.NUMERIC;
|
||||
|
||||
/**
|
||||
* @author bonus
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class SmsService {
|
||||
@Resource
|
||||
private VerificationCodeConfig verificationCodeConfig;
|
||||
@Resource
|
||||
private RedisService redisService;
|
||||
|
||||
/**
|
||||
* 生成手机验证码
|
||||
*
|
||||
* @return AjaxResult
|
||||
* @throws CaptchaException 自定义captcha 异常
|
||||
*/
|
||||
public void sendSimplePhone(String to) {
|
||||
if (StringUtils.isEmpty(to)) {
|
||||
throw new CaptchaException("手机号不能为空");
|
||||
}
|
||||
String code = VerificationCodeUtils.generateVerificationCode(NUMERIC);
|
||||
String str = verificationCodeConfig.getContent().replace("<code>", code);
|
||||
str = str.replace("<time>", verificationCodeConfig.getTime().toString());
|
||||
String s = SmsUtils.sendMsgToSms(Arrays.asList(to), str);
|
||||
log.error("省公司短信平台发送短信返回结果=" + s);
|
||||
if (StringUtils.isNotEmpty(s)) {
|
||||
if (s.contains("ok")) {
|
||||
String verifyKey = CacheConstants.VERIFICATION_CODE + StringUtils.nvl(to, "");
|
||||
redisService.setCacheObject(verifyKey, code, verificationCodeConfig.getTime(), TimeUnit.MINUTES);
|
||||
} else {
|
||||
throw new CaptchaException("获取短信失败");
|
||||
}
|
||||
} else {
|
||||
throw new CaptchaException("获取短信失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
package com.bonus.auth.service;
|
||||
|
||||
import com.bonus.auth.utils.SmsUtils;
|
||||
import com.bonus.common.core.constant.CacheConstants;
|
||||
import com.bonus.common.core.constant.Constants;
|
||||
import com.bonus.common.core.domain.R;
|
||||
|
|
@ -7,7 +8,6 @@ import com.bonus.common.core.exception.CaptchaException;
|
|||
import com.bonus.common.core.exception.ServiceException;
|
||||
import com.bonus.common.core.utils.StringUtils;
|
||||
import com.bonus.common.core.utils.VerificationCodeUtils;
|
||||
import com.bonus.common.core.utils.sms.SmsUtils;
|
||||
import com.bonus.common.core.web.domain.AjaxResult;
|
||||
import com.bonus.common.redis.service.RedisService;
|
||||
import com.bonus.common.security.utils.SecurityUtils;
|
||||
|
|
@ -19,6 +19,7 @@ import org.springframework.stereotype.Component;
|
|||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
|
|
@ -107,7 +108,7 @@ public class SysPasswordService {
|
|||
String code = VerificationCodeUtils.generateVerificationCode(VerificationCodeUtils.CodeType.NUMERIC);
|
||||
String str = "您的验证码为" + code + ",尊敬的客户,以上验证码3分钟有效,微服务平台提醒您:转发可能导致账号被盗,请勿将验证码泄露于他人";
|
||||
String verifyKey = CacheConstants.VERIFICATION_CODE + phone;
|
||||
String s = SmsUtils.smsToken(phone, str, "");
|
||||
String s = SmsUtils.sendMsgToSms(Arrays.asList(phone), str);
|
||||
if (StringUtils.isNotEmpty(s)) {
|
||||
if (s.contains("ok")) {
|
||||
redisService.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,54 @@
|
|||
package com.bonus.auth.utils;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.ah.sbd.SmsTool;
|
||||
import com.ah.sbd.utils.param.BatchSmsByContentParam;
|
||||
import com.ah.sbd.utils.param.SmsParam;
|
||||
import com.bonus.common.core.utils.StringUtils;
|
||||
import com.bonus.common.core.utils.encryption.Sm4Utils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SmsUtils {
|
||||
// 安徽省公司短信平台key
|
||||
public static final String ANHUI_COMPANY_SMS_KEY = "Wq1FZobZC5iJIAGKWh6gpKUmdGKBOQq7koHmx+Vg49g/dSjSFlw+2qRQmMmyXvJT";
|
||||
|
||||
/**
|
||||
* 发送消息到短信
|
||||
*
|
||||
* @param phoneNumbers 电话号码列表,可能包含无效或重复的号码
|
||||
* @param msg 要发送的短信消息内容
|
||||
* @return 发送短信的结果,具体形式依赖于SmsUtils.smsToken的实现
|
||||
* 此方法首先检查电话号码列表是否为空,如果为空,则直接返回空字符串。
|
||||
* 接下来,它会移除列表中所有为空的电话号码,
|
||||
* 然后对列表中超过11位的电话号码进行解密处理,
|
||||
* 确保只有有效的电话号码被用来发送短信。
|
||||
*/
|
||||
public static String sendMsgToSms(List<String> phoneNumbers, String msg) {
|
||||
// 检查电话号码列表和消息内容是否有效
|
||||
if (CollectionUtils.isEmpty(phoneNumbers) || StringUtils.isBlank(msg)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// 过滤掉无效的电话号码
|
||||
phoneNumbers.removeIf(StringUtils::isEmpty);
|
||||
|
||||
// 对超过11位的电话号码进行解密处理
|
||||
phoneNumbers.replaceAll(phoneNumber ->
|
||||
phoneNumber.length() > 11 ? Sm4Utils.decrypt(phoneNumber) : phoneNumber
|
||||
);
|
||||
|
||||
// 根据电话号码数量发送短信
|
||||
if (phoneNumbers.isEmpty()) {
|
||||
return "";
|
||||
} else if (phoneNumbers.size() == 1) {
|
||||
JSONObject sendResult = SmsTool.sendSms(new SmsParam(phoneNumbers.get(0), msg), ANHUI_COMPANY_SMS_KEY);
|
||||
return sendResult.toString();
|
||||
} else {
|
||||
JSONObject sendResult = SmsTool.sendSms(new BatchSmsByContentParam(phoneNumbers, msg), ANHUI_COMPANY_SMS_KEY);
|
||||
return sendResult.toString();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue