java实现发送短信
1.将必要的配置写yml
sms:enabled: trueurl: //代理地址ap-id: secret-key: jbjgbm: sign: template: xxx,您的验证码是:{code},有效期2分钟。
2.创建配置类,获取yml的配置信息
@Data
@Configuration
@ConfigurationProperties("sms")
public class SmsConfig {private boolean enabled;private String url;private String apId;private String secretKey;private String jbjgbm;private String sign;private String template;
}
3.使用工具类,搭建发送短信的请求
@Slf4j
@Service
public class SmsUtil {private static final String SMS_LIMIT_KEY_PREFIX = "sms:send:limit:";private static final Integer SMS_SEND_LIMIT = 5;private static final Integer SMS_SEND_SUCCESS = 1;private static final Integer SMS_SEND_FAILED = -1;private static final Integer SMS_SEND_OUT_LIMIT = -2;@Resourceprivate SmsConfig SmsConfig;public Integer sendMsg(String phoneNum, String smsContent) {if (SmsConfig.isEnabled()) {return postBigDataChannel(phoneNum, smsContent);} else {throw new ServiceException("未配置短信通道");}}public Integer postBigDataChannel(String phoneNum, String smsContent) {String today = LocalDate.now().toString();String key = SMS_LIMIT_KEY_PREFIX + today + 3;log.info("key: {}", key);
// if (RedisUtils.hasKey(key) && RedisUtils.getAtomicValue(key) >= SMS_SEND_LIMIT) {
// return SMS_SEND_OUT_LIMIT;
// }if (!RedisUtils.hasKey(key)) {RedisUtils.setAtomicValue(key, 0);}String dxId = Seq.getId();StringBuffer stringBuffer = new StringBuffer();stringBuffer.append(SmsConfig.getJbjgbm());stringBuffer.append(SmsConfig.getApId());stringBuffer.append(SmsConfig.getSecretKey());stringBuffer.append(dxId);stringBuffer.append("00");stringBuffer.append(phoneNum);stringBuffer.append(smsContent);stringBuffer.append(SmsConfig.getSign());stringBuffer.append("1");//参数校验序列,生成方法:将jbjgbm、apId、secretKey、dxId、dxType、mobiles、content、sign、priority按序拼接(无间隔符),// 通过MD5(32位小写)计算得出值。String mac = DigestUtil.md5Hex(stringBuffer.toString());Map<String, String> postData = new HashMap<>();postData.put("jbjgbm", SmsConfig.getJbjgbm());postData.put("apId", SmsConfig.getApId());postData.put("dxId", dxId);postData.put("dxType", "00");postData.put("mobiles", phoneNum);postData.put("content", smsContent);postData.put("sign", SmsConfig.getSign());postData.put("priority", "1");postData.put("mac", mac);log.info("短信平台地址:{},号码:{},内容:{}", SmsConfig.getUrl(), phoneNum, JSONUtil.toJsonStr(postData));String base64Str = Base64Encoder.encode(JSONUtil.toJsonStr(postData));Map<String, Object> postParam = new HashMap<>();postParam.put("data", base64Str);log.info("短信验证码路径:{}", SmsConfig.getUrl());try {HttpResponse execute = HttpRequest.post(SmsConfig.getUrl()).form(postParam).timeout(10000).execute();if (execute.getStatus() != HttpStatus.HTTP_OK) {log.error("发送短信失败{}", execute.getStatus());return SMS_SEND_FAILED;}} catch (Exception e) {log.error("发送短信失败{}", e.getMessage());return SMS_SEND_FAILED;}RedisUtils.incrAtomicValue(key);return SMS_SEND_SUCCESS;}
}
4.传输手机号与内容直接使用
try {smsUtil.sendMsg(phone, content);log.info("短信发送成功,手机号:{},信息:{}", phone, content);} catch (Exception e) {log.error("短信发送失败:{}", e.getMessage());}