java中怎么實現(xiàn)動態(tài)口令登錄,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
10余年的赤峰網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。成都營銷網(wǎng)站建設(shè)的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整赤峰建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)公司從事“赤峰網(wǎng)站設(shè)計”,“赤峰網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實執(zhí)行。
1.實現(xiàn)一個ItsClient 客戶端用來實例化調(diào)用驗證功能
public class ItsClient {private static final String routing = "/v1.0/sectoken/otp_validation";// ! HTTPS消息驗證地址private String httpsVerifyUrl = "";// ! otp ipAddrprivate String ipAddr = "";// ! otp portprivate String port = "";// ! otp appIDprivate String appID = "";// ! otp appKeyprivate String appKey = "";// ! 錯誤碼private int errorCode = 0;// ! 錯誤消息private String errorMessage = "";TreeMap<Integer, String> errorCodeTable = new TreeMap<Integer, String>() {{put(200, "請求成功");put(400, "輸入不合法,比如請求數(shù)據(jù)不是json");put(401, "AppID不合法");put(402, "指紋不合法");put(410, "非法用戶,驗證otp時,傳入的uid有誤,找不到用戶");put(411, "錯誤的otp");put(412, "一個周期內(nèi)動態(tài)口令只能使用一次");put(413, "已達(dá)一個周期內(nèi)最大嘗試次數(shù)");put(500, "ITS服務(wù)器內(nèi)部錯誤");put(601, "參數(shù)錯誤");put(602, "sha1簽名失敗");put(603, "操作json失敗");put(604, "url訪問錯誤:");put(605, "較驗返回指紋失敗");}};public ItsClient() {this.ipAddr = ItsConf.GetIpAddr();this.port = ItsConf.GetPort();this.appID = ItsConf.GetOtpAppID();this.appKey = ItsConf.GetOtpAppKey();httpsVerifyUrl = "https://" + this.ipAddr + ':' + this.port + routing;}//獲取錯誤信息public String GetErrorMessage() {return this.errorMessage;} //獲取錯誤碼public int GetErrorCode() {return this.errorCode;}public void SetError(int errorCode, String extMessage) {this.errorCode = errorCode;this.errorMessage = this.errorCodeTable.get(this.errorCode).toString() + extMessage;}public static String SHA1(String decript) throws NoSuchAlgorithmException {String ret = "";MessageDigest sha1 = MessageDigest.getInstance("SHA1");byte[] sha1bytes = sha1.digest(decript.getBytes());if (sha1bytes != null) {ret = new BASE64Encoder().encode(sha1bytes);}return ret;}public String EncodeJson(TreeMap<String, String> map) {JSONObject jmap = new JSONObject(map);return jmap.toString();}public TreeMap<String, Object> DecodeJson(String jsonStr) throws ParseException {JSONObject jsonObject = new JSONObject(jsonStr);TreeMap<String, Object> retMap = new TreeMap<String, Object>();Iterator<String> iter = jsonObject.keys();String key = null;Object value = null;while (iter.hasNext()) {key = iter.next();value = jsonObject.get(key);retMap.put(key, value);}return retMap;}public String BuildQueryStr(TreeMap<String, String> params) {String queryStr = "";Iterator<String> itr = params.keySet().iterator();while (itr.hasNext()) {String key = itr.next();queryStr += (key + "=" + params.get(key).toString() + "&");}return queryStr.substring(0, queryStr.length() - 1);}public boolean IsEmptyOrNull(String param) {return param == null || param.length() <= 0;}/** * @brief 驗證otp * @param uid ITS主賬號UID或已配置的從賬號 * @param otp 需要驗證的動態(tài)口令 * @return bool true: 成功, false: 失敗 */@SuppressWarnings("serial")public boolean AuthOtp(final String uid, final String otp) {if (IsEmptyOrNull(this.ipAddr) || IsEmptyOrNull(this.port) || IsEmptyOrNull(this.appID)|| IsEmptyOrNull(this.appKey) || IsEmptyOrNull(uid) || IsEmptyOrNull(otp)) {SetError(601, "");return false;}TreeMap<String, String> params = new TreeMap<String, String>() {{put("app_id", appID);put("app_key", appKey);put("uid", uid);put("otp", otp);}};String qureyStr = this.BuildQueryStr(params);String fingerprint = "";try {fingerprint = SHA1(qureyStr);} catch (Exception ex) {ex.printStackTrace();SetError(602, ex.getMessage());return false;}params.remove("app_key");params.put("fingerprint", fingerprint);String postStr = "";try {postStr = EncodeJson(params);} catch (Exception ex) {ex.printStackTrace();SetError(603, "json encode" + ex.getMessage());return false;}HttpsClient conn = null;String res = "";try {conn = new HttpsClient();res = conn.post(this.httpsVerifyUrl, postStr); // 訪問接口調(diào)取返回結(jié)果} catch (Exception ex) {ex.printStackTrace();SetError(604, ex.getMessage());return false;}TreeMap<String, Object> ret = null;try {ret = DecodeJson(res);} catch (Exception ex) {ex.printStackTrace();SetError(603, "json decode " + ex.getMessage());return false;}int retCode = (Integer) ret.get("status");if (200 != retCode) {SetError(retCode, "");return false;}return true;}}
2.實現(xiàn)一個HttpsClient 請求工具
public class HttpsClient { final static HostnameVerifier doNotVerifier = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }; /** * @brief 發(fā)送請求 * @param httpsUrl 請求的地址 * @param postStr 請求的數(shù)據(jù) * @throws Exception */ public String post(String httpsUrl, String postStr) throws Exception { HttpsURLConnection conn = null; StringBuffer recvBuff = new StringBuffer(); String resData = ""; try { conn = (HttpsURLConnection) (new URL(httpsUrl)).openConnection(); conn.setHostnameVerifier(doNotVerifier); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", " application/json"); conn.setRequestProperty("Content-Length", String.valueOf(postStr.getBytes("utf-8").length)); conn.setUseCaches(false); //設(shè)置為utf-8可以解決服務(wù)器接收時讀取的數(shù)據(jù)中文亂碼問題 conn.getOutputStream().write(postStr.getBytes("utf-8")); conn.getOutputStream().flush(); conn.getOutputStream().close(); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { recvBuff.append(line); } resData = recvBuff.toString(); return resData; } catch (MalformedURLException ex) { throw ex; } catch (IOException ex) { throw ex; } catch (Exception ex) { throw ex; } }}
3.實現(xiàn)Its一個配置用來配置Its服務(wù)器信息接口訪問地址
public class ItsConf {// ITS服務(wù)器地址 1.1.1.1 或 xxx.xxx.com的形式private static String ipAddr = "";// ITS服務(wù)器端口private static String port = "";// OTP服務(wù)的AppIDprivate static String otpAppID = "";// OTP服務(wù)的AppKeyprivate static String otpAppKey = "";public static String GetIpAddr() {return ipAddr;}public static String GetPort() {return port;}public static String GetOtpAppID() {return otpAppID;}public static String GetOtpAppKey() {return otpAppKey;}}
4.接下來就是LoginContorller 完成口令認(rèn)證
//username 用戶名//code動態(tài)口令密碼ItsClient itsClient = new ItsClient();if(itsClient.AuthOtp(username, code)){//認(rèn)證成功,跳轉(zhuǎn)頁面}
看完上述內(nèi)容,你們掌握java中怎么實現(xiàn)動態(tài)口令登錄的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
當(dāng)前標(biāo)題:java中怎么實現(xiàn)動態(tài)口令登錄
網(wǎng)頁鏈接:http://aaarwkj.com/article18/igijgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、域名注冊、網(wǎng)站改版、網(wǎng)站營銷、品牌網(wǎng)站建設(shè)、移動網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)