您好,登錄后才能下訂單哦!
1.實(shí)現(xiàn)一個(gè)ItsClient 客戶端用來(lái)實(shí)例化調(diào)用驗(yàn)證功能
public class ItsClient { private static final String routing = "/v1.0/sectoken/otp_validation"; // ! HTTPS消息驗(yàn)證地址 private String httpsVerifyUrl = ""; // ! otp ipAddr private String ipAddr = ""; // ! otp port private String port = ""; // ! otp appID private String appID = ""; // ! otp appKey private String appKey = ""; // ! 錯(cuò)誤碼 private int errorCode = 0; // ! 錯(cuò)誤消息 private String errorMessage = ""; TreeMap<Integer, String> errorCodeTable = new TreeMap<Integer, String>() { { put(200, "請(qǐng)求成功"); put(400, "輸入不合法,比如請(qǐng)求數(shù)據(jù)不是json"); put(401, "AppID不合法"); put(402, "指紋不合法"); put(410, "非法用戶,驗(yàn)證otp時(shí),傳入的uid有誤,找不到用戶"); put(411, "錯(cuò)誤的otp"); put(412, "一個(gè)周期內(nèi)動(dòng)態(tài)口令只能使用一次"); put(413, "已達(dá)一個(gè)周期內(nèi)最大嘗試次數(shù)"); put(500, "ITS服務(wù)器內(nèi)部錯(cuò)誤"); put(601, "參數(shù)錯(cuò)誤"); put(602, "sha1簽名失敗"); put(603, "操作json失敗"); put(604, "url訪問(wèn)錯(cuò)誤:"); put(605, "較驗(yàn)返回指紋失敗"); } }; 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; } //獲取錯(cuò)誤信息 public St ring GetErrorMessage() { return this.errorMessage; } //獲取錯(cuò)誤碼 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 驗(yàn)證otp * @param uid ITS主賬號(hào)UID或已配置的從賬號(hào) * @param otp 需要驗(yàn)證的動(dòng)態(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); // 訪問(wèn)接口調(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.實(shí)現(xiàn)一個(gè)HttpsClient 請(qǐng)求工具
public class HttpsClient { final static HostnameVerifier doNotVerifier = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }; /** * @brief 發(fā)送請(qǐng)求 * @param httpsUrl 請(qǐng)求的地址 * @param postStr 請(qǐng)求的數(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í)讀取的數(shù)據(jù)中文亂碼問(wèn)題 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.實(shí)現(xiàn)Its一個(gè)配置用來(lái)配置Its服務(wù)器信息接口訪問(wèn)地址
public class ItsConf { // ITS服務(wù)器地址 1.1.1.1 或 xxx.xxx.com的形式 private static String ipAddr = ""; // ITS服務(wù)器端口 private static String port = ""; // OTP服務(wù)的AppID private static String otpAppID = ""; // OTP服務(wù)的AppKey private 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.接下來(lái)就是LoginContorller 完成口令認(rèn)證
//username 用戶名 //code動(dòng)態(tài)口令密碼 ItsClient itsClient = new ItsClient(); if(itsClient.AuthOtp(username, code)){ //認(rèn)證成功,跳轉(zhuǎn)頁(yè)面 }
5.登陸頁(yè)面就省略了,自己完成吧
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。