榛子云短信平台用户中心
http://sms_developer.zhenzikj.com/zhenzisms_user/index.html 然后再pom中加入如下配置,告诉maven导入本地jar
<dependency>
<groupId>com.zhenzikj</groupId>
<artifactId>zhenzisms</artifactId>
<version>1.0.x</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/ZhenziSmsSDK.jar</systemPath>
</dependency>
在pom中给spring boot的打包插件设置一下includeSystemScope参数即可
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
这样就将包引入了,此外还要引入一个阿里巴巴的json包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.49</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/fastjson-1.2.49.jar</systemPath>
</dependency>
方案一:存 session,用不同浏览器会出现问题,只能部署一台服务器
/**
* 获取验证码
* @param tellPhone
* @param httpSession
* @return
*/
@GetMapping("/getCode")
public String getCode(@RequestParam("tellPhone") String tellPhone,HttpSession httpSession ){
if (!TString.isEmpty(tellPhone)){
try {
JSONObject json = null;
String code = String.valueOf(new Random().nextInt(999999));
System.out.println("验证码是:"+code+".....................");
// String success = Integer.toString(code);
// return success;
ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);
String result = client.send(tellPhone, "您的验证码为:" + code + ",该码有效期为5分钟,该码只能使用一次!");
json = JSONObject.parseObject(result);
if (json.getIntValue("code")!= 0) {//发送短信失败
return createModel(new ResultBean().setMsg("发送短信失败").setSuccess(false));
}
//将验证码存到session中,同时存入创建时间
//以json存放,这里使用的是阿里的fastjson
json = new JSONObject();
json.put("tellPhone",tellPhone);
json.put("code",code);
json.put("createTime",System.currentTimeMillis());
// 将认证码存入SESSION
httpSession.setAttribute("code",json);
return createModel(new ResultBean().setMsg("发送短信成功").setSuccess(true));
} catch (Exception e) {
e.printStackTrace();
}
}
return createModel(new ResultBean().setMsg("发送短信失败").setSuccess(false));
}
@PostMapping(value = "checkCode")
public String checkCode (String code,HttpSession httpSession){
//校验验证码
String what = httpSession.getAttribute("code").toString();
if (!TString.isEmpty(what)){
JSONObject jb = JSONObject.parseObject(what);
if (!jb.getString("code").equals(code)) {
return createModel(new ResultBean().setMsg("验证码不对").setSuccess(false));
}
return createModel(new ResultBean().setMsg("验证通过").setSuccess(true));
}
return createModel(new ResultBean().setMsg("session为空").setSuccess(false));
}
方案二:存redis
/**
* 获取验证码
* 1、先校验手机号码格式 TODO
* @param tellPhone
* @param httpSession
* @return
*/
@GetMapping("/getCode")
public String getCode(@RequestParam("tellPhone") String tellPhone, HttpSession httpSession ){
if (!TString.isEmpty(tellPhone)){
try {
JSONObject json = null;
String code = String.valueOf(new Random().nextInt(999999));
System.out.println("验证码是:"+code+".....................");
// String success = Integer.toString(code);
// return success;
ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);
String result = client.send(tellPhone, "您的验证码为:" + code + ",该码有效期为5分钟,该码只能使用一次!");
json = JSONObject.parseObject(result);
if (json.getIntValue("code")!= 0) {//发送短信失败
return createModel(new ResultBean().setMsg("发送短信失败").setSuccess(false));
}
//将验证码存到session中,同时存入创建时间
//以json存放,这里使用的是阿里的fastjson
json = new JSONObject();
json.put("tellPhone",tellPhone);
json.put("code",code);
json.put("createTime",System.currentTimeMillis());
// 将认证码存入SESSION
// httpSession.setAttribute("code",json);
RedisUtil.putOpsObject(tellPhone , json.toJSONString());
return createModel(new ResultBean().setMsg("发送短信成功").setSuccess(true));
} catch (Exception e) {
e.printStackTrace();
}
}
return createModel(new ResultBean().setMsg("发送短信失败").setSuccess(false));
}
/**
* 检验验证
* @param map
* @param httpSession
* @return
*/
@PostMapping(value = "checkCode")
public String checkCode (@RequestBody Map<String,String> map, HttpSession httpSession){
if (TMap.isEmpty(map)){
return createModel(new ResultBean().setMsg("验证码为空").setSuccess(false));
}
//校验验证码
Map json = JSON.parseObject((String) RedisUtil.getOpsObject(map.get("tellPhone")));
try{
if (!TObject.isEmpty(json)){
if (!json.get("code").equals(map.get("code"))) {
return createModel(new ResultBean().setMsg("验证码不对").setSuccess(false));
}
return createModel(new ResultBean().setMsg("验证通过").setSuccess(true));
}
return createModel(new ResultBean().setMsg("获取对比验证码为空").setSuccess(false));
}catch (Exception e) {
e.printStackTrace();
}
return createModel(new ResultBean().setMsg("请重新获取验证码").setSuccess(false));
}
参考文档:https://www.jianshu.com/p/ab3a13c5a8cb