溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

javaweb項(xiàng)目中怎么手機(jī)短信登錄

發(fā)布時(shí)間:2021-07-24 14:58:28 來源:億速云 閱讀:132 作者:Leah 欄目:編程語言

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)javaweb項(xiàng)目中怎么手機(jī)短信登錄,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

一、首先需要注冊個(gè)第三方的賬戶,比如秒嘀科技等,然后拿到三個(gè)參數(shù)值:QUERAY_PATH ACCOUNT_SIDAUTH_TOKEN

二、編寫獲取驗(yàn)證碼類getMessage.java

private static final String QUERAY_PATH="xxxx";private static final String ACCOUNT_SID="xxx";private static final String AUTH_TOKEN="xxx";/** * @Title: getCode  * @Description: TODO( 發(fā)送驗(yàn)證碼 )  * @param @param phone * @param @return  設(shè)定文件  * @return String  返回類型  * @throws */public static String getCode(String phone){String ran = smsCode();String timestamp = getStamp();String sig = getMD5(ACCOUNT_SID, AUTH_TOKEN, timestamp); String tamp = "您的驗(yàn)證碼為"+ran+",請于{2}分鐘內(nèi)正確輸入,如非本人操作,請忽略此短信。"; OutputStreamWriter out = null;BufferedReader br = null;StringBuilder sb = new StringBuilder();try {URL url = new URL(QUERAY_PATH);HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("POST");connection.setDoInput(true);connection.setDoOutput(true);connection.setConnectTimeout(5000);connection.setReadTimeout(10000);connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");String args = getArgs(ACCOUNT_SID, tamp, phone, timestamp, sig, "JSON"); out.write(args); out.flush();  br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));String temp="";while ((temp=br.readLine())!=null) {sb.append(temp);}} catch (Exception e) {e.printStackTrace();}JSONObject json = new JSONObject(sb.toString());String code = json.getString("respCode");String defaultrespcode = "00000"; if(defaultrespcode.equals(code)){return ran;}else{return code;}}/** * @Title: getArgs  * @Description: TODO( 參數(shù)拼接 )  * @param @param accountSid * @param @param smsContent * @param @param to * @param @param timestamp * @param @param sig * @param @param respDataType * @param @return  設(shè)定文件  * @return String  返回類型  * @throws */public static String getArgs(String accountSid,String smsContent,String to,String timestamp,String sig,String respDataType){return "accountSid="+accountSid+"&smsContent="+smsContent+"&to="+to+"&timestamp="+timestamp+"&sig="+sig+"&respDataType="+respDataType;}/** * @Title: getStamp  * @Description: TODO( 獲取時(shí)間戳 )  * @param @return  設(shè)定文件  * @return String  返回類型  * @throws */public static String getStamp(){returnnew SimpleDateFormat("yyyyMMddHHmmss").format(new Date());}/** * @Title: getMD5  * @Description: TODO(sig簽名 )  * @param @param sid * @param @param token * @param @param timestamp * @param @return  設(shè)定文件  * @return String  返回類型  * @throws */public static String getMD5(String sid,String token,String timestamp){StringBuilder sBuilder = new StringBuilder();String source = sid + token + timestamp; try {MessageDigest instance = MessageDigest.getInstance("MD5");byte[] digest = instance.digest(source.getBytes());for (byte b : digest) {String hexString = Integer.toHexString(b&0xff);if(hexString.length()==1){sBuilder.append("0"+hexString);}else{sBuilder.append(hexString);}}} catch (NoSuchAlgorithmException e) {// TODO Auto-generated catch blocke.printStackTrace();}return sBuilder.toString();}/** * @Title: smsCode  * @Description: TODO( 產(chǎn)生驗(yàn)證碼)  * @param @return  設(shè)定文件  * @return String  返回類型  * @throws */public static String smsCode(){String random = new Random().nextInt(1000000)+"";if(random.length()!=6){return smsCode();}else{return random;}}

三、編寫servlet

protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {resp.setCharacterEncoding("utf-8");String phone = req.getParameter("phone");  String code = GetMessage.getCode(phone);//手機(jī)號(hào)登錄后HttpSession session = req.getSession();PrintWriter out = resp.getWriter();//檢查手機(jī)號(hào)是否注冊過checkPhoneDao checkPhoneDao = new checkPhoneImpl();boolean results = checkPhoneDao.checkPhone(phone);if(!results){out.print(code);session.setAttribute("name", "phone");}else {out.print("此手機(jī)號(hào)沒有被注冊");}out.close();}

四、 dao 層實(shí)現(xiàn) 的方法

@Overridepublic boolean checkPhone(String phone) {String sql = "select username from user where phone=?";List<Map<String, Object>> queryForList = DbUtil.queryForList(sql, phone);if(queryForList.isEmpty()){return true;}return false;}

到此,短信登錄的功能已經(jīng)實(shí)現(xiàn)了,大家可以按照我的思路嘗試去實(shí)現(xiàn)。注:由于有的小伙伴沒有框架基礎(chǔ),所以演示使用jsp+servlet+jdbc實(shí)現(xiàn)的。

上述就是小編為大家分享的javaweb項(xiàng)目中怎么手機(jī)短信登錄了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI