Spring MVC 中 短信驗證碼功能的實現(xiàn)方法
在外部網(wǎng)站中短信的驗證很有必要,比如在實現(xiàn)注冊、驗證用戶信息等的情況下。在SpringMVC中的實現(xiàn)如下:
短信接口
短信接口,有些企業(yè)會購買的有移動的短信平臺接口。如果是個人或者是小企業(yè)可以使用一些云服務(wù)的。比如百度的API Store上面的。
我使用的是:http://apistore.baidu.com/apiworks/servicedetail/1018.html
當(dāng)然短信接口肯定都是要付費的,而且是基于模板的,具體的使用說明可以看這個網(wǎng)址里面的使用說明。
前端界面
前端的界面,可能如下,點擊獲取驗證碼,然后按鈕變?yōu)榛疑⑶业褂嫊r。(手機號是我的~~)

HTML代碼就不寫了,JS如下:vailidationCode是獲取驗證碼按鈕的ID。phone是手機號碼的ID,手機號碼只是簡單的驗證了,如果是要更精確,使用正則,其中的url的sendSms是后臺的springMVC的路徑。
$("#validationCode").click(function(){
var phone = $("#phone").val();
if($("#phone").val() && $("#phone").val().length == 11){
$.ajax({
cache : false,
url : "sendSms",
data : {phone : phone}
});
updateButtonStatus();
}else {
alert("請輸入合法的手機號");
}
});
var countdown=60;
function updateButtonStatus(){
var phone = $("#validationCode");
if (countdown == 0) {
phone.attr("disabled","false");
phone.val("免費獲取驗證碼");
countdown = 60;
return;
} else {
phone.attr("disabled","true");
phone.val("重新發(fā)送(" + countdown + ")");
countdown--;
}
setTimeout(function() {
updateButtonStatus() }
,1000)
}
后端代碼
@RequestMapping(value = "/sendSms")
@ResponseBody
public String sendSMS(@RequestParam("phone") String phone, HttpServletRequest request){
StringBuilder code = new StringBuilder();
Random random = new Random();
// 生成6位驗證碼
for (int i = 0; i < 6; i++) {
code.append(String.valueOf(random.nextInt(10)));
}
HttpSession session = request.getSession();
session.setAttribute(VALIDATE_PHONE, phone);
session.setAttribute(VALIDATE_PHONE_CODE, code.toString());
session.setAttribute(SEND_CODE_TIME, new Date().getTime());
String smsText = "您的驗證碼是:"+code;
SMSUtil.send(phone,smsText);
return "success";
}
其中的SMSUtil是封裝的上面的短信接口的發(fā)送類。參考如下,其中的API_KEY改成自己的。
public class SMSUtil {
static String httpUrl = "http://apis.baidu.com/kingtto_media/106sms/106sms";
final static String API_KEY = "xxxx";
public static String send(String phone,String content) {
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
try {
String httpArg = "mobile="+phone+"&content="+URLEncoder.encode(content,"UTF-8")+"&tag=2";
httpUrl = httpUrl + "?" + httpArg ;
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("GET");
// 填入apikey到HTTP header
connection.setRequestProperty("apikey",API_KEY);
connection.connect();
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close();
result = sbf.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
前臺的表單提交前還需要使用ajax做一下表單的驗證,驗證一下驗證碼是否正確:
@RequestMapping("/validate")
@ResponseBody
protected String validate(HttpServletRequest request,@RequestParam("phone") String inputPhone,@RequestParam ("code") String inputCode){
HttpSession session = request.getSession();
String code = (String) session.getAttribute(VALIDATE_PHONE_CODE);
String phone = (String) session.getAttribute(VALIDATE_PHONE);
if(phone.equals(inputPhone) && code.equalsIgnoreCase(inputCode)){
return "success";
}else{
return "failure";
}
}
以上所述是小編給大家介紹的Spring MVC 中 短信驗證碼功能的實現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- SpringBoot實現(xiàn)前端驗證碼圖片生成和校驗
- Spring Security OAuth2集成短信驗證碼登錄以及第三方登錄
- Spring Boot 驗證碼的生成和驗證詳解
- Springboot實現(xiàn)阿里云通信短信服務(wù)有關(guān)短信驗證碼的發(fā)送功能
- SpringBoot實現(xiàn)短信驗證碼校驗方法思路詳解
- 實例詳解Spring Boot實戰(zhàn)之Redis緩存登錄驗證碼
- SpringBoot 集成Kaptcha實現(xiàn)驗證碼功能實例詳解
- Spring Security Oauth2.0 實現(xiàn)短信驗證碼登錄示例
- Spring框架生成圖片驗證碼實例
- springboot實現(xiàn)郵箱驗證碼功能
相關(guān)文章
關(guān)于泛型擦除問題的解決--Mybatis查詢類型轉(zhuǎn)換
這篇文章主要介紹了關(guān)于泛型擦除問題的解決--Mybatis查詢類型轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
Springboot內(nèi)置的工具類之CollectionUtils示例講解
這篇文章主要介紹了Springboot內(nèi)置的工具類之CollectionUtils,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
java封裝空值建議使用Optional替代null的方法示例解析
這篇文章主要為大家介紹了java封裝空值建議使用Optional替代null的方法原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11
SpringAop @Aspect織入不生效,不執(zhí)行前置增強織入@Before方式
這篇文章主要介紹了SpringAop @Aspect織入不生效,不執(zhí)行前置增強織入@Before方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12

