一、要找一个提供短信接口的第三方平台,这里我使用的是榛子云
二、在注册后,就可以使用了
三、首先是在pom.xml中添加依赖
com.zhenzikj
zhenzisms
1.0.x
如不好使,可以自己引用jar包,引入步奏:
1.首先在新建libs文件夹(根目录或者resource目录下都可以),将需要引入的jar放进去
2.然后再pom中加入如下配置,告诉maven导入本地jar
com.zhenzikj
zhenzisms
1.0.x
system
${project.basedir}/libs/ZhenziSmsSDK.jar
3.在pom中给spring boot的打包插件设置一下includeSystemScope参数即可
org.springframework.boot
spring-boot-maven-plugin
true
这样就将包引入了,此外还要引入一个阿里巴巴的json包
com.alibaba
fastjson
1.2.49
system
${project.basedir}/libs/fastjson-1.2.49.jar
四、下载工程,将工程代码中需要的部分引入自己的代码里
1.验证码发送的controller
package com.foreknow.controller;
import com.alibaba.fastjson.JSONObject;
import com.foreknow.model.Member;
import com.foreknow.service.MemberService;
import com.zhenzi.sms.ZhenziSmsClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.util.Random;
@Controller
public class CodeController {
private static final long serialVersionUID = 1L;
//短信平台相关参数
private String apiUrl = "https://sms_developer.zhenzikj.com";
private String appId = "100862";
private String appSecret = "62358d10-bc0e-4152-a52c-578a8debc9b9";
@Autowired
private MemberService memberService;
@ResponseBody
@GetMapping("/fitness/code")
public boolean getCode(@RequestParam("memPhone") String memPhone, HttpSession httpSession){
Member member = memberService.selectByPhone(memPhone);
if (member!=null){
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(memPhone, "您的验证码为:" + code + ",该码有效期为5分钟,该码只能使用一次!");
json = JSONObject.parseObject(result);
if (json.getIntValue("code")!=0){//发送短信失败
return false;
}
//将验证码存到session中,同时存入创建时间
//以json存放,这里使用的是阿里的fastjson
json = new JSONObject();
json.put("memPhone",memPhone);
json.put("code",code);
json.put("createTime",System.currentTimeMillis());
// 将认证码存入SESSION
httpSession.setAttribute("code",json);
return true;
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
}
其中的局部变量是在榛子云的个人中心获取:
五、修改密码的controller
@ResponseBody
@PostMapping(value="/fitness/update",produces = "application/json; charset=utf-8")
public String updatePass(@RequestParam("username") String username,
@RequestParam("password") String password,
@RequestParam("memPhone") String memPhone,
@RequestParam("memCode") String memCode,
HttpSession httpSession) {
JSONObject json = (JSONObject)httpSession.getAttribute("code");
String msg = null;
if (json.getString("code").equals(memCode)){
if((System.currentTimeMillis() > json.getLong("createTime"))){
Member mem = memberService.selectByPhone(memPhone);
if (mem!=null&&mem.getUsername().equals(username)) {
int isRight = memberService.UpdateByPhone(password, memPhone);
if (isRight == 1) {
msg = JsonUtils.objectToJson("修改成功,请登录");
return msg;
}
}
msg = JsonUtils.objectToJson("修改失败,请确认用户名或者手机号");
return msg;
}
msg = JsonUtils.objectToJson("验证码已过期");
return msg;
}
msg = JsonUtils.objectToJson("修改失败,验证码输入有误");
return msg;
}
这里我自定义了一个jsonUtils
public class JsonUtils {
// 定义jackson对象
private static final ObjectMapper MAPPER = new ObjectMapper();
/**
* 将对象转换成json字符串。
* Title: pojoToJson
* Description:
* @param data
* @return
*/
public static String objectToJson(Object data) {
try {
String string = MAPPER.writeValueAsString(data);
return string;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
/**
* 将json结果集转化为对象
*
* @param jsonData json数据
* @param clazz 对象中的object类型
* @return
*/
public static T jsonToPojo(String jsonData, Class beanType) {
try {
T t = MAPPER.readValue(jsonData, beanType);
return t;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将json数据转换成pojo对象list
* Title: jsonToList
* Description:
* @param jsonData
* @param beanType
* @return
*/
public static List jsonToList(String jsonData, Class beanType) {
JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
try {
List list = MAPPER.readValue(jsonData, javaType);
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
六、前台Ajax获取处理返回值
function upd(){
var memCode = $("#regist_vcode").val();
var username = $("#regist_account").val();
var phone = $("#regist_phone").val();
var password = $("#regist_password2").val();
var mes = {"username":username,"memPhone":phone,"password":password,"memCode":memCode}
$.ajax({
type:'post',
url:'update',
data:mes,
async:true,
dataType:'json',
success:function(data){
var msg = JSON.stringify(data);
if(msg.match("成功")){
alert(msg)
document.location = "login";
}else {
alert(data);
}
}
})
}