短信验证码
导入依赖:

<dependency> <groupId>com.cloopen</groupId> <artifactId>java-sms-sdk</artifactId> <version>1.0.1</version> </dependency>
html:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>注册</title> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script> </head> <body> login <form action="reCheck" method="post" > 姓名: <input type="text" name="userName" required> <br> 密码: <input type="password" name="userPwd" required> <br> 手机号: <input type="text" name="userPhone" id="userPhone" required> <br> <button type="button" onclick="sendcode1()">发送验证码</button> <br> 请输入验证码: <input type="text" name="userType" required> <br> <input type="submit" value="注册"> </form> <p style="color: crimson" text="${msg}"></p> <script> function sendcode1(){ var userPhone= $("#userPhone").val(); // var userPhone= $("#userPhone").val(); $.ajax({ url: "Check", type: "post", data:{ phone: userPhone }, success:function f(result){ if(result=="1"){ alert("验证码发送成功"); }else{ alert("发送失败"); } }, }); } </script> </body> </html>
controller:

//验证码验证 @ResponseBody @RequestMapping("Check") public int Check(String phone,HttpSession session){ System.out.println(1); HashMap<String, Object> result = null; CCPRestSmsSDK restAPI = new CCPRestSmsSDK(); restAPI.init("app.cloopen.com", "8883"); restAPI.setAccount("8a216da881ad97540181e6111a1c0ad1", "323d9096b28f40abbf0795098fbbe449"); restAPI.setAppId("8a216da881ad97540181e6111b080ad8"); System.out.println(phone); UserTable use=new UserTable(); use.setUserPhone(phone); use.setUserType("0710"); System.out.println(use); session.setAttribute("user",use); result = restAPI.sendTemplateSMS(phone,"1" ,new String[]{"0710","3 "}); 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); } return 1; }else{ //异常返回输出错误码和错误信息 System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg")); return 0; } }
注册验证:

@RequestMapping("reCheck") public String reCheck(StudenterTable stu,HttpSession session) throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException { System.out.println(stu.getStuName()+" "+stu.getStuPwd()+" "+stu.getStuAddress()); if(stu.getStuAddress().equals(stu.getStuAddress())&&stu.getStuClass().equals(stu.getStuClass())){ service.insert(stu); return "login"; }else{ session.setAttribute("msg","手机号或验证码有误"); return "regist"; } }
里边的字段 改成自己的!!
前端页面:

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>注册</title> 6 <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script> 7 </head> 8 <body> 9 Reist 10 11 <form action="reCheck" method="post" > 12 姓名: 13 <input type="text" name="stuName" required> 14 <br> 15 密码: 16 <input type="password" name="stuPwd" required> 17 <br> 18 手机号: 19 <input type="text" name="stuAddress" id="stuAddress" required> 20 <br> 21 <button type="button" onclick="sendcode1()">发送验证码</button> 22 <br> 23 请输入验证码: 24 <input type="text" name="stuClass" required> 25 <br> 26 <input type="submit" value="注册"> 27 </form> 28 <a href="login">去登录</a> 29 <p style="color: crimson" text="${msg}"></p> 30 31 32 <script> 33 function sendcode1(){ 34 var stuAddress= $("#stuAddress").val(); 35 // var userPhone= $("#userPhone").val(); 36 $.ajax({ 37 url: "Check", 38 type: "post", 39 data:{ 40 phone: stuAddress 41 }, 42 success:function f(result){ 43 if(result=="1"){ 44 alert("验证码发送成功"); 45 }else{ 46 alert("发送失败"); 47 } 48 }, 49 }); 50 } 51 </script> 52 </body> 53 </html>
仅供参考!