手机发送短信、验证码—MAS
直接上酸菜
SmsUtils.java
package cn.com.sinosoft.cas.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
import org.apereo.cas.configuration.CasConfigurationProperties;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.fastjson.JSON;
import cn.com.sinosoft.cas.vo.SendReq;
import cn.com.sinosoft.cas.vo.SendRes;
/**
* 发送短信
*
* @author wzk
*
*/
public class SmsUtils {
@Autowired
private CasConfigurationProperties casProperties;
// 扩展码,根据向移动公司申请的通道填写,如果申请的精确匹配通道,则填写空字符串(""),否则添加移动公司允许的扩展码
private static String addSerial = "";
String mac = "";
/**
* 多用户发送短信信息
*
* @param mobiles 手机号码逗号分隔
* @param captchaCode 短信内容
* @return 返回true表示成功,false表示失败
* @throws IOException
*/
public boolean sendMsg(String mobiles, String captchaCode) throws IOException {
Map<String, String> properties = casProperties.getCustom().getProperties();
// 地址
String url = properties.get("bzcs.sms.url");
// 集团客户名称
String ecName = properties.get("bzcs.sms.ecName");
// 用户名
String apId = properties.get("bzcs.sms.apId");
// 密码
String secretKey = properties.get("bzcs.sms.secretKey");
// 网关签名编码,必填
String sign = properties.get("bzcs.sms.sign");
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowDatestr = sdf.format(calendar.getTimeInMillis());
captchaCode += nowDatestr; // 短信内容后跟个日期时间(可有可无),需求要求
SendReq sendReq = new SendReq();
sendReq.setApId(apId);
sendReq.setEcName(ecName);
sendReq.setSecretKey(secretKey);
sendReq.setContent(captchaCode);
sendReq.setMobiles(mobiles);
sendReq.setAddSerial(addSerial);
sendReq.setSign(sign);
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(sendReq.getEcName());
stringBuffer.append(sendReq.getApId());
stringBuffer.append(sendReq.getSecretKey());
stringBuffer.append(sendReq.getMobiles());
stringBuffer.append(sendReq.getContent());
stringBuffer.append(sendReq.getSign());
stringBuffer.append(sendReq.getAddSerial());
sendReq.setMac(Md5Util.MD5(stringBuffer.toString()).toLowerCase());
String reqText = JSON.toJSONString(sendReq);
String encode = Base64.encodeBase64String(reqText.getBytes("UTF-8"));
String resStr = sendPost(url, encode);
SendRes sendRes = JSON.parseObject(resStr, SendRes.class);
if (sendRes.isSuccess() && !"".equals(sendRes.getMsgGroup()) && "success".equals(sendRes.getRspcod())) {
return true;
} else {
return false;
}
}
/**
* 向指定 URL 发送POST方法的请求
*
* @param url 发送请求的 URL
* @param param 请求参数
* @return 所代表远程资源的响应结果
*/
private static String sendPost(String url, String param) {
OutputStreamWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("contentType", "utf-8");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
conn.setDoOutput(true);
conn.setDoInput(true);
out = new OutputStreamWriter(conn.getOutputStream());
out.write(param);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += "\n" + line;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
public SmsUtils() {
}
}
SendReq.java

package cn.com.sinosoft.cas.vo; import lombok.Data; @Data public class SendReq { // 集团客户名称 private String ecName; // 用户名 private String apId; // 密码 private String secretKey; // 手机号码逗号分隔。(如“18137282928,18137282922,18137282923”) private String mobiles; // 发送短信内容 private String content; // 网关签名编码,必填,签名编码在中国移动集团开通帐号后分配,可以在云MAS网页端管理子系统-SMS接口管理功能中下载。 private String sign; // 扩展码,根据向移动公司申请的通道填写,如果申请的精确匹配通道,则填写空字符串(""),否则添加移动公司允许的扩展码。 private String addSerial; // API输入参数签名结果,签名算法:将ecName,apId,secretKey,mobiles,content // ,sign,addSerial按照顺序拼接,然后通过md5(32位小写)计算后得出的值 private String mac; }
SendRes.java

package cn.com.sinosoft.cas.vo; import lombok.Data; @Data public class SendRes { // 响应状态码 private String rspcod; // 消息批次号,由云MAS平台生成,用于验证短信提交报告和状态报告的一致性(取值msgGroup)注:如果数据验证不通过msgGroup为空 private String msgGroup; // 数据校验结果 private boolean success; }