1.首先寻找平台:https://www.yuntongxun.com/(我使用的免费60多条)
2.下载相关配置文件:
3.目录介绍:
4.将jar包导入自己的项目
5.根据自己的的需求修改Demo示例中的配置信息
6.根据自己的需求修改示例:
package com.xx.util;
import java.util.HashMap;
import java.util.Random;
import javax.servlet.http.HttpSession;
import com.cloopen.rest.sdk.CCPRestSDK;
public class Note {
public static boolean getNote(String phone,HttpSession session){
HashMap<String, Object> result = null;
Random random = new Random();
String oo="";
boolean status=false;
for (int i = 0; i < 6; i++) {
int a = random.nextInt(10);
oo=oo+a;
}
CCPRestSDK restAPI = new CCPRestSDK();
restAPI.init("app.cloopen.com", "8883");// 初始化服务器地址和端口,格式如下,服务器地址不需要写https://
restAPI.setAccount("8a216da86bb683bbb9975e802e2", "d58f54a9f0334d5ff1b6884256b");// 初始化主帐号和主帐号TOKEN(自己配置)
restAPI.setAppId("8a216da86b16bbb99764802e9");// 初始化应用ID
result = restAPI.sendTemplateSMS("17862976922","1" ,new String[]{"您的验证码为"+oo+",请于60s内正确输入,如非本人操作,请忽略此短信。"});
if("000000".equals(result.get("statusCode"))){
//将验证码放到session中
session.setAttribute("note", oo);
status=true;
System.out.println("给"+phone+"发送验证码成功!");
}else{
//System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
System.out.println("给"+phone+"发送验证码失败!");
}
return status;
}
}
7.服务层代码(可以不看,根据自己的需求调用session中放的String类型的note((String)session.getAttribute("note")))
package com.xx.serviceImpl;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xx.bean.Users;
import com.xx.dao.UsersDao;
import com.xx.service.UserService;
import com.xx.util.Note;
@Service
public class UserServiceImpl implements UserService{
@Autowired UsersDao usersDao;
@Override
public Users login(Map<String, Object> map,HttpSession session) {
String phone = "^1[3|4|5|7|8][0-9]{9}$";
String email = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
Users users=null;
if (((String) map.get("usPhone1")).matches(phone)&&(!"".equals(map.get("usPass")))) {
System.out.println("手机密码");
users=usersDao.loginUsers(map);
}else if (((String) map.get("usPhone1")).matches(email)&&(!"".equals(map.get("usPass")))) {
System.out.println("邮箱");
map.put("usEmail", map.get("usPhone1"));
map.remove("usPhone1");
users=usersDao.loginUsers(map);
}else {
System.out.println("进入手机验证");
Note.getNote(map.get("usPhone1").toString(), session);
String note=(String)session.getAttribute("note");
if(note.equals(map.get("note"))){
users=usersDao.selectByPhone1(map.get("usPhone1").toString());
}
}
if (users!=null) {
session.setAttribute("users", users);
}
return users;
}
}
其中几个重要的信息获取:
服务层代码参考:
package com.xx.control;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xx.bean.Users;
import com.xx.service.UserService;
@Controller
public class UsersControl {
@Resource UserService userService;
//登录
@RequestMapping(value="login",method=RequestMethod.POST)
@ResponseBody
public String login(@RequestParam Map<String, Object> map,HttpSession session){
System.out.println("有人在登录:");
String result="no";
Users users=userService.login(map, session);
if(users!=null){
result="yes";
System.out.println(users.getUsName()+users.getUsPhone1()+"登录成功!");
}
return result;
}
}