日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

注册功能之手机验证与邮箱验证

發(fā)布時(shí)間:2024/5/14 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 注册功能之手机验证与邮箱验证 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

  這兩天嘗試寫了一個(gè)注冊(cè)功能,一種是手機(jī)驗(yàn)證碼注冊(cè),一種是郵箱注冊(cè)激活。開始寫的時(shí)候,不知道從哪著手,看了好幾篇文章,開始慢慢了解思路,現(xiàn)在就記錄下來。

  思路

    注冊(cè)之手機(jī)驗(yàn)證:一個(gè)注冊(cè)頁面【手機(jī)號(hào)框、驗(yàn)證碼框、點(diǎn)擊獲取驗(yàn)證碼按鈕、點(diǎn)擊注冊(cè)按鈕】。點(diǎn)擊獲取驗(yàn)證碼后,后臺(tái)controller層隨機(jī)生成一個(gè)6位隨機(jī)數(shù)(即驗(yàn)證碼),把驗(yàn)證碼通過某個(gè)短信API接口傳到該手機(jī)上。手機(jī)信息看到驗(yàn)證碼,就把它輸入前臺(tái)的驗(yàn)證碼框,點(diǎn)注冊(cè)提交,只要輸入的驗(yàn)證碼與后臺(tái)生成的驗(yàn)證碼是相同的,就可以提示注冊(cè)成功了。

    注冊(cè)之郵箱激活:一個(gè)注冊(cè)頁面【用戶名框、密碼框、郵箱框、點(diǎn)擊注冊(cè)按鈕】。一個(gè)數(shù)據(jù)庫【用戶表:id、username、password、state(激活碼狀態(tài))、code(激活碼)】。一個(gè)后臺(tái)默認(rèn)生成一個(gè)激活碼(可以用UUID)和默認(rèn)一個(gè)激活碼狀態(tài)(比如:“0”)。用戶在點(diǎn)擊注冊(cè)后,將數(shù)據(jù)保存在數(shù)據(jù)庫,保存成功后,新建一個(gè)線程去執(zhí)行發(fā)送郵件的任務(wù),通過郵箱發(fā)送一個(gè)帶code(激活碼)的鏈接給用戶,用戶點(diǎn)擊鏈接后,后臺(tái)接收到鏈接傳過來的激活碼,查詢數(shù)據(jù)庫是否存在該用戶信息,有則執(zhí)行修改激活碼狀態(tài)(比如“1”),然后提示注冊(cè)成功。

  代碼之手機(jī)驗(yàn)證(測(cè)試):我是申請(qǐng)聚合數(shù)據(jù)的短信API接口來用的(參考其開發(fā)文檔),其他也行吧,這里只測(cè)試發(fā)驗(yàn)證碼給手機(jī)

package com.fh.controller.system.SMS;import net.sf.json.JSONObject;import java.util.HashMap; import java.util.Map; import java.util.Random;import static com.fh.util.url.UrlUtil.net;public class SmsVerification {public static void main(String[] args) {
     //這個(gè)這樣寫主要是開發(fā)文檔里這樣規(guī)定String code ="#code#="+getCode();
String result =null;String url ="http://v.juhe.cn/sms/send";//請(qǐng)求接口地址Map params = new HashMap();//請(qǐng)求參數(shù)params.put("mobile","*******");//接受短信的用戶手機(jī)號(hào)碼params.put("tpl_id","*******");//您申請(qǐng)的短信模板ID,根據(jù)實(shí)際情況修改params.put("tpl_value",code);//您設(shè)置的模板變量,根據(jù)實(shí)際情況修改params.put("key","*****************");//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁查詢)try {result = net(url, params, "GET");JSONObject object = JSONObject.fromObject(result);if(object.getInt("error_code")==0){System.out.println(object.get("result"));}else{System.out.println(object.get("error_code")+":"+object.get("reason"));}} catch (Exception e) {e.printStackTrace();}}//獲取隨機(jī)驗(yàn)證碼public static String getCode(){//開始生成隨機(jī)數(shù)字 -- 驗(yàn)證碼StringBuffer buffer = new StringBuffer();Random random = new Random(); //隨機(jī)數(shù)字for(int i =0;i<6 ;i++) {//生成一個(gè)6位數(shù)的隨機(jī)數(shù)buffer.append(random.nextInt(10));//范圍0到10,不包括10 ;0-9}return buffer.toString();} }

  工具類(這里只用到net方法)

package com.fh.util.url;import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.util.Map;public class UrlUtil {public static final String DEF_CHATSET = "UTF-8";public static final int DEF_CONN_TIMEOUT = 30000;public static final int DEF_READ_TIMEOUT = 30000;public static String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";// 配置您申請(qǐng)的KEYpublic static final String APPKEY = "*************************";/*** 向指定的地址發(fā)送一個(gè)post請(qǐng)求,帶著data數(shù)據(jù)** @param url* @param data* @return by 羅召勇 Q群193557337*/public static String post(String url, String data) {try {URL urlObj = new URL(url);URLConnection connection = urlObj.openConnection();// 要發(fā)送數(shù)據(jù)出去,必須要設(shè)置為可發(fā)送數(shù)據(jù)狀態(tài)connection.setDoOutput(true);// 獲取輸出流OutputStream os = connection.getOutputStream();// 寫出數(shù)據(jù)os.write(data.getBytes());os.close();// 獲取輸入流InputStream is = connection.getInputStream();byte[] b = new byte[1024];int len;StringBuilder sb = new StringBuilder();while ((len = is.read(b)) != -1) {sb.append(new String(b, 0, len));}return sb.toString();} catch (Exception e) {e.printStackTrace();}return null;}/*** 向指定的地址發(fā)送get請(qǐng)求** @param url* @return by 羅召勇 Q群193557337*/public static String get(String url) {try {URL urlObj = new URL(url);// 開連接URLConnection connection = urlObj.openConnection();InputStream is = connection.getInputStream();byte[] b = new byte[1024];int len;StringBuilder sb = new StringBuilder();while ((len = is.read(b)) != -1) {sb.append(new String(b, 0, len));}return sb.toString();} catch (Exception e) {e.printStackTrace();}return null;}/**** @param strUrl* 請(qǐng)求地址* @param params* 請(qǐng)求參數(shù)* @param method* 請(qǐng)求方法* @return 網(wǎng)絡(luò)請(qǐng)求字符串* @throws Exception*/public static String net(String strUrl, Map params, String method) throws Exception {HttpURLConnection conn = null;BufferedReader reader = null;String rs = null;try {StringBuffer sb = new StringBuffer();if (method == null || method.equals("GET")) {strUrl = strUrl + "?" + urlencode(params);}URL url = new URL(strUrl);conn = (HttpURLConnection) url.openConnection();if (method == null || method.equals("GET")) {conn.setRequestMethod("GET");} else {conn.setRequestMethod("POST");conn.setDoOutput(true);}conn.setRequestProperty("User-agent", userAgent);conn.setUseCaches(false);conn.setConnectTimeout(DEF_CONN_TIMEOUT);conn.setReadTimeout(DEF_READ_TIMEOUT);conn.setInstanceFollowRedirects(false);conn.connect();if (params != null && method.equals("POST")) {try {DataOutputStream out = new DataOutputStream(conn.getOutputStream());out.writeBytes(urlencode(params));} catch (Exception e) {// TODO: handle exception}}InputStream is = conn.getInputStream();reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET));String strRead = null;while ((strRead = reader.readLine()) != null) {sb.append(strRead);}rs = sb.toString();} catch (IOException e) {e.printStackTrace();} finally {if (reader != null) {reader.close();}if (conn != null) {conn.disconnect();}}return rs;}// 將map型轉(zhuǎn)為請(qǐng)求參數(shù)型public static String urlencode(Map<String, Object> data) {StringBuilder sb = new StringBuilder();for (Map.Entry i : data.entrySet()) {try {sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue() + "", "UTF-8")).append("&");} catch (UnsupportedEncodingException e) {e.printStackTrace();}}return sb.toString();}}

  代碼之郵箱注冊(cè):需要一個(gè)javax.mail.jar包:https://files.cnblogs.com/files/yuanmaolin/javax.mail.zip,紅色鏈接【http://p5up23.natappfree.cc】我是直接用內(nèi)網(wǎng)穿透來弄的,這樣外部就可以訪問到我的本地后臺(tái)。至于郵箱,是需要去打開smtp協(xié)議。

package com.fh.util.mailboxactivation;import com.sun.mail.util.MailSSLSocketFactory;import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties;public class MailUtil implements Runnable {private String email;// 收件人郵箱private String code;// 激活碼public MailUtil(String email, String code) {this.email = email;this.code = code;}public void run() {// 1.創(chuàng)建連接對(duì)象javax.mail.Session// 2.創(chuàng)建郵件對(duì)象 javax.mail.Message// 3.發(fā)送一封激活郵件String from = "********@163.com";// 發(fā)件人電子郵箱String host = "smtp.163.com"; // 指定發(fā)送郵件的主機(jī)smtp.qq.com(QQ)|smtp.163.com(網(wǎng)易)Properties properties = System.getProperties();// 獲取系統(tǒng)屬性properties.setProperty("mail.smtp.host", host);// 設(shè)置郵件服務(wù)器properties.setProperty("mail.smtp.auth", "true");// 打開認(rèn)證try {//QQ郵箱需要下面這段代碼,163郵箱不需要/*MailSSLSocketFactory sf = new MailSSLSocketFactory();sf.setTrustAllHosts(true);properties.put("mail.smtp.ssl.enable", "true");properties.put("mail.smtp.ssl.socketFactory", sf);*/// 1.獲取默認(rèn)session對(duì)象Session session = Session.getDefaultInstance(properties, new Authenticator() {public PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication("*********@163.com", "*******"); // 發(fā)件人郵箱賬號(hào)、授權(quán)碼}});// 2.創(chuàng)建郵件對(duì)象Message message = new MimeMessage(session);// 2.1設(shè)置發(fā)件人message.setFrom(new InternetAddress(from));// 2.2設(shè)置接收人message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));// 2.3設(shè)置郵件主題message.setSubject("賬號(hào)激活");// 2.4設(shè)置郵件內(nèi)容String content ="<html>" +"<head></head>" +"<body>" +"<h1>這是一封激活郵件,激活請(qǐng)點(diǎn)擊以下鏈接</h1>" +"<h3><a href='http://p5up23.natappfree.cc/mailbox/active?code=" + code + "'>" +"http://p5up23.natappfree.cc/mailbox/active?code=" + code + "</href>" +"</h3>" +"</body>" +"</html>";message.setContent(content, "text/html;charset=UTF-8");// 3.發(fā)送郵件Transport.send(message);System.out.println("郵件成功發(fā)送!");} catch (Exception e) {e.printStackTrace();}} }

  測(cè)試郵箱發(fā)送

import com.fh.util.mailboxactivation.MailUtil;public class test {public static void main(String[] args) {String mail = "*******@qq.com";String code = "dhfjgkridujfhng";//這里只是測(cè)試,我就沒新建一個(gè)線程了,實(shí)際可以這樣【new Thread(new MailUtil(mail, code)).start();】MailUtil(mail, code);} }

  

后面就是查看郵箱,點(diǎn)擊鏈接,后臺(tái)接收code。。。。按上面思路來。  

  

?

轉(zhuǎn)載于:https://www.cnblogs.com/yuanmaolin/p/11065060.html

總結(jié)

以上是生活随笔為你收集整理的注册功能之手机验证与邮箱验证的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。