发表于: 2017-10-09 21:55:54
1 784
今天完成的事情:使用容联的第三方api 集成到了原来的登录接口
容联的接口文档比较详细 demo也很齐全。
1.接口声明文件:sdk\src\com\cloopen\rest\sdk\CCPRestSDK.java
2.接口函数定义:public HashMap
3.参数说明:
to: 字符串类型,短信接收手机号码集合,用英文逗号分开,如 "13810001000, 最多一次发送200个。
templateId: 字符串类型,模板Id,如使用测试模板,模板id为"1",如使用自己创建的模板,则使用自己创建的短信模板id即可。
datas: 字符串数组类型,内容数据,需定义成数组方式,如模板中有两个参数,定义方式为String{"3456","测试"}。
4.接口调用示例:
public static void main(String[] args) {
HashMap<String, Object> result = null;
//初始化SDK
CCPRestSmsSDK restAPI = new CCPRestSmsSDK();
//******************************注释*********************************************
//*初始化服务器地址和端口 *
//*沙盒环境(用于应用开发调试):restAPI.init("sandboxapp.cloopen.com", "8883");*
//*生产环境(用户应用上线使用):restAPI.init("app.cloopen.com", "8883"); *
//*******************************************************************************
restAPI.init("sandboxapp.cloopen.com", "8883");
//******************************注释*********************************************
//*初始化主帐号和主帐号令牌,对应官网开发者主账号下的ACCOUNT SID和AUTH TOKEN *
//*ACOUNT SID和AUTH TOKEN在登陆官网后,在“应用-管理控制台”中查看开发者主账号获取*
//*参数顺序:第一个参数是ACOUNT SID,第二个参数是AUTH TOKEN。 *
//*******************************************************************************
restAPI.setAccount("", "");
//******************************注释*********************************************
//*初始化应用ID *
//*测试开发可使用“测试Demo”的APP ID,正式上线需要使用自己创建的应用的App ID *
//*应用ID的获取:登陆官网,在“应用-应用列表”,点击应用名称,看应用详情获取APP ID*
//*******************************************************************************
restAPI.setAppId("");
//******************************注释****************************************************************
//*调用发送模板短信的接口发送短信 *
//*参数顺序说明: *
//*第一个参数:是要发送的手机号码,可以用逗号分隔,一次最多支持100个手机号 *
//*第二个参数:是模板ID,在平台上创建的短信模板的ID值;测试的时候可以使用系统的默认模板,id为1。 *
//*系统默认模板的内容为“【云通讯】您使用的是云通讯短信模板,您的验证码是{1},请于{2}分钟内正确输入”*
//*第三个参数是要替换的内容数组。 *
//**************************************************************************************************
//**************************************举例说明***********************************************************************
//*假设您用测试Demo的APP ID,则需使用默认模板ID 1,发送手机号是13800000000,传入参数为6532和5,则调用方式为 *
//*result = restAPI.sendTemplateSMS("13800000000","1" ,new String[]{"6532","5"}); *
//*则13800000000手机号收到的短信内容是:【云通讯】您使用的是云通讯短信模板,您的验证码是6532,请于5分钟内正确输入 *
//*********************************************************************************************************************
result = restAPI.sendTemplateSMS("1","" ,new String[]{"1234","5"});
System.out.println("SDKTestGetSubAccounts result=" + result);
if("000000".equals(result.get("statusCode"))){
//正常返回输出data包体信息(map)
HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
Set<String> keySet = data.keySet();
for(String key:keySet){
Object object = data.get(key);
System.out.println(key +" = "+object);
}
}else{
//异常返回输出错误码和错误信息
System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
}
}
实际在使用中 写了一个工具类 把手机号 和 验证码作为入参 其他的一些是固定参数
官方的类里面没有get方法 要自己写
import java.util.HashMap;
import java.util.Set;
/**
* Created by hfismyangel@163.com on 2017/7/13.
*/
public class SMSTest {
private String serverIP;
private String serverPort;
private String accountSid;
private String accountToken;
private String appID;
public String getServerIP() {
return serverIP;
}
public void setServerIP(String serverIP) {
this.serverIP = serverIP;
}
public String getServerPort() {
return serverPort;
}
public void setServerPort(String serverPort) {
this.serverPort = serverPort;
}
public String getAccountSid() {
return accountSid;
}
public void setAccountSid(String accountSid) {
this.accountSid = accountSid;
}
public String getAccountToken() {
return accountToken;
}
public void setAccountToken(String accountToken) {
this.accountToken = accountToken;
}
public String getAppID() {
return appID;
}
public void setAppID(String appID) {
this.appID = appID;
}
public void messageSend(String telephone,String code){
HashMap<String, Object> result = null;
CCPRestSmsSDK restAPI = new CCPRestSmsSDK();
restAPI.init(serverIP, serverPort);
// 初始化服务器地址和端口,生产环境配置成app.cloopen.com,端口是8883.
restAPI.setAccount(accountSid, accountToken);
// 初始化主账号名称和主账号令牌,登陆云通讯网站后,可在控制首页中看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN。
restAPI.setAppId(appID);
// 请使用管理控制台中已创建应用的APPID。
result = restAPI.sendTemplateSMS(telephone,"1" ,new String[]{code,"5"});
System.out.println("SDKTestGetSubAccounts result=" + result);
if("000000".equals(result.get("statusCode"))){
//正常返回输出data包体信息(map)
HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
Set<String> keySet = data.keySet();
for(String key:keySet){
Object object = data.get(key);
System.out.println(key +" = "+object);
}
}else{
//异常返回输出错误码和错误信息
System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
}
}
这里有个错误我 一直不能引用 下面再说
然后把固定的参数 使用spring配置 记得在web里面注册
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:sms.properties" ignore-unresolvable="true"/>
<bean id="smsApi" class="com.jnshu.until.SMSTest">
<property name="serverIP" value="${sms.serverIP}"/>
<property name="serverPort" value="${sms.serverPort}"/>
<property name="accountSid" value="${sms.accountSid}"/>
<property name="accountToken" value="${sms.accountToken}"/>
<property name="appID" value="${sms.appID}"/>
</bean>
在原有的controller里面新增一个发送短信的接口
@RequestMapping("/sendmessage")
public void sendMessage(@ModelAttribute("telephone") String telephoneRegister, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userIp = getIp(request);
logger.debug(userIp+"--------------------------");
String num = cacheUtil.get(userIp);
logger.debug(num+"----------------------------");
if (num == null) {
cacheUtil.put(userIp, 1);
}
if(num!=null&Integer.parseInt(num)<5){
cacheUtil.put(userIp, Integer.parseInt(cacheUtil.get(userIp))+1);
//短信验证
Random random = new Random();
Integer code = random.nextInt(9000) + 1000;
try {
smsTest.messageSend(telephoneRegister, code.toString());
} catch (Exception e) {
logger.debug("smsSystemException:" + e.toString());
}
request.getSession().setAttribute("code", code);
}
if(Integer.parseInt(num) > 10) {
logger.debug("今日验证码发送已达到限制");
request.getRequestDispatcher("/WEB-INF/statics/jsp/getcodefail.jsp").forward(request, response);
}
}
这里获取手机号 生成随机的数字 获取请求的ip 并且存入缓存
验证ip的次数 避免多次验证
更改原来的注册页 增加短信验证 邮件 图片上传
使用¥ajax的异步传输实现局部刷新,就是点发送短信验证码按钮,不是整个jsp页面跳转,而是局转跳转去访问控制器内对应的请求映射的处理器方法 ,发送短信验证码到用户填入的手机号中。
<script type="text/javascript">
$(function () {
$('#sendMessage').click(
function () {
$.ajax({
type:"POST",
url: "/sendmessage",
data:{"telephone":$("#telephone").val()},
dataType:JSON,
success:function (html) {
alert("请输入收到的验证码!")
}
})
}
);
});
</script>
<body>
<div align="center">
<form action="<%=basePath%>/register" method="post">
<table>
<tr>
<td><h1>用户注册:</h1></td>
</tr>
<tr>
<td>用户名:</td>
<td><input type="text" name="userName"/></td>
</tr>
<tr>
<td>手机:</td>
<td><input id="telephone" type="text" name="telephone"/></td>
</tr>
<tr>
<td>邮箱:</td>
<td><input type="text" name="email"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" name="password"/></td>
</tr>
<tr>
<td>短信验证码:</td>
<td><input type="text" name="telephoneRegister" id="telephoneRegister"/></td>
</tr>
<tr>
<td><input type="button" id="sendMessage" value="获取短信验证码"/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="注册"
/>
<input type="reset" value="重置"/></td>
</tr>
</table>
明天计划的事情:集成 邮件 图片上传
遇到的问题:
这里开始新写工具类的时候 引用官方的包一直错误
不能直接把这个jar粘贴复制到项目文件下
手动打进来 id随便起 pom文件加上就好了
引入Spring 报Could not resolve placeholder 'redis_max_active' in string value "${redis_max_active}"; nested excep
引入新的properties的时候 注意所有的引入 都要加上下面这个 就不会出现这个问题
<context:property-placeholder location="classpath:sms.properties" ignore-unresolvable="true"/>
调试的时候获取到了空值 异常了 没能收到短信
调了一下日志 本地没开redis 没法链接
收获:重建了项目 复习了以前的知识 换用方法不用注解 也用xml配置redis
就不贴代码了 也是模板 基本都一样
评论