甲方前两天要求将CRM登录方式调整为短信的方式确保数据的安全。
之前没有搞过短信对接。查阅了资料,找到一个阿里云接口对接工具类,以为省事,没想打掉坑里去了不适用。
自己看着官方提供的文档实现了接口对接。短信登录已正常使用。
com.aliyun
aliyun-java-sdk-core
4.0.3
2.工具类方法
public class MoblieMessageUtil {
// 产品名称:云通信短信API产品,开发者无需替换
private static final String product = "Dysmsapi";
// 产品域名,开发者无需替换
private static final String domain = "dysmsapi.aliyuncs.com";
// 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
private static String accessKeyId = "*******";
private static String accessKeySecret = "********";
private static String signName = "*******";
private static String identifyingTempleteCode = "**********";
public static boolean sendSms(String mobile, String templateParam, String templateCode) throws ClientException, JsonParseException, JsonMappingException, IOException
{
boolean bool =false;
// // 可自助调整超时时间
// System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
// System.setProperty("sun.net.client.defaultReadTimeout", "10000");
// 初始化acsClient,暂不支持region化
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
//request.setProtocol(ProtocolType.HTTPS);
request.setMethod(MethodType.POST);
request.setDomain("dysmsapi.aliyuncs.com");
request.setVersion("2017-05-25");
request.setAction("SendSms");//发送短信的方法
request.putQueryParameter("RegionId", "cn-hangzhou");
request.putQueryParameter("PhoneNumbers", mobile);//必填
request.putQueryParameter("SignName", signName);//必填
request.putQueryParameter("TemplateCode", identifyingTempleteCode);//必填
request.putQueryParameter("TemplateParam", templateParam);//必填
try {
CommonResponse response = acsClient.getCommonResponse(request);
ObjectMapper objectMapper = new ObjectMapper();
HashMap map = objectMapper.readValue(response.getData(),HashMap.class);//将json字符串转化为Map
if ("OK".equals(map.get("Code"))) {
bool =true;
}
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
return bool;
}
public static boolean sendIdentifyingCode(String mobile, String msgCode) throws JsonParseException, JsonMappingException, IOException {
try {
return sendSms(mobile, "{\"code\":\"" + msgCode + "\"}", identifyingTempleteCode);
} catch (ClientException e) {
}
return false;
}
}
response.getData()返回的数据
response.getData(): 返回如下数据
{
"Message": "OK",
"RequestId": "873043ac-bcda-44db-9052-2e204c6ed20f",
"BizId": "607300000000000000^0",
"Code": "OK"
}
3.controller层发送短信的方法如下
@RequestMapping(value="login/getMsgCode")
@ResponseBody
public Map getMsgCode(String username,HttpServletRequest request)
{
Map resultMap = new HashMap<>();
try {
AdminUser pojo =new AdminUser();
pojo.setMobile(username);
pojo = adminUserService.selectOne(pojo);
if ((pojo == null) || (StringUtils.isBlank(pojo.getMobile()))) {
resultMap.put("error", -200);
resultMap.put("msg", "您输入的手机号不存在!");
return resultMap;
}
String code = (int)((Math.random()*9+1)*100000)+"";
Boolean errMsg =MoblieMessageUtil.sendIdentifyingCode(username, code);//调用短信接口
pojo.setPasswordSalt(code);
pojo.setMobile(username);
adminUserService.update(pojo);//
if (errMsg) {
resultMap.put("error", 100);
resultMap.put("msg", "成功");
return resultMap;
}
resultMap.put("error",-200);
resultMap.put("msg", errMsg);
}
catch (Exception e) {
logger.error(e.getMessage(), e);
}
return resultMap;
}
还有一个需要对短信的验证。我将短信验证码存入到表中,
验证时,取出验证码与输入验证码进行比对
希望能够提供帮助。