最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java動態(tài)口令登錄實現(xiàn)過程詳解

 更新時間:2019年07月04日 15:21:10   作者:Shen_sy  
這篇文章主要介紹了java動態(tài)口令登錄實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

1.實現(xiàn)一個ItsClient 客戶端用來實例化調(diào)用驗證功能

public class ItsClient {
	private static final String routing = "/v1.0/sectoken/otp_validation";
	// ! HTTPS消息驗證地址
	private String httpsVerifyUrl = "";
	// ! otp ipAddr
	private String ipAddr = "";
	// ! otp port
	private String port = "";
	// ! otp appID
	private String appID = "";
	// ! otp appKey
	private 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 St ring 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ù)的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.接下來就是LoginContorller 完成口令認(rèn)證

//username 用戶名
//code動態(tài)口令密碼
ItsClient itsClient = new ItsClient();
if(itsClient.AuthOtp(username, code)){
	//認(rèn)證成功,跳轉(zhuǎn)頁面
}

5.登陸頁面就省略了,自己完成吧

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • MyBatis與Spring中的SqlSession詳解

    MyBatis與Spring中的SqlSession詳解

    在MyBatis中,你可以使用SqlSessionFactory來創(chuàng)建SqlSession,使用MyBatis-Spring之后,你不再需要直接使用SqlSessionFactory了,接下來通過示例代碼講解MyBatis與Spring中的SqlSession,需要的朋友可以參考下
    2024-05-05
  • 淺談JVM系列之JIT中的Virtual Call

    淺談JVM系列之JIT中的Virtual Call

    什么是Virtual Call?Virtual Call在java中的實現(xiàn)是怎么樣的?Virtual Call在JIT中有沒有優(yōu)化?所有的答案看完這篇文章就明白了。
    2021-06-06
  • 給JavaBean賦默認(rèn)值并且轉(zhuǎn)Json字符串的實例

    給JavaBean賦默認(rèn)值并且轉(zhuǎn)Json字符串的實例

    這篇文章主要介紹了給JavaBean賦默認(rèn)值并且轉(zhuǎn)Json字符串的實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java中使用fastjson設(shè)置字段不序列化

    Java中使用fastjson設(shè)置字段不序列化

    這篇文章主要介紹了Java中使用fastjson設(shè)置字段不序列化,alibaba的fasetjson可以設(shè)置字段不序列化,使用@JSONField注解的serialize屬性,該屬性默認(rèn)是可以序列化的,設(shè)置成false就表示不可序列化,需要的朋友可以參考下
    2023-12-12
  • java中如何對arrayList按數(shù)字大小逆序排序

    java中如何對arrayList按數(shù)字大小逆序排序

    這篇文章主要介紹了java中如何對arrayList按數(shù)字大小逆序排序問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • 手把手教你如何在Idea中下載jar包

    手把手教你如何在Idea中下載jar包

    maven依賴的jar包,很多時候同一個jar包會存在多個版本,刪除其中一個后,重新編譯,會把舊jar由加載回來了,下面這篇文章主要給大家介紹了關(guān)于如何在Idea中下載jar包的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • SpringBoot實現(xiàn)在一個模塊中引入另一個模塊

    SpringBoot實現(xiàn)在一個模塊中引入另一個模塊

    這篇文章主要介紹了SpringBoot實現(xiàn)在一個模塊中引入另一個模塊的方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • 在Java SE上使用Headless模式的超級指南

    在Java SE上使用Headless模式的超級指南

    這篇文章主要介紹了在Java SE上使用Headless模式的超級指南,文中介紹了Headless模式實際使用的各種技巧,極力推薦!需要的朋友可以參考下
    2015-07-07
  • 微服務(wù)和分布式的區(qū)別詳解

    微服務(wù)和分布式的區(qū)別詳解

    在本篇文章里小編給各位整理了關(guān)于微服務(wù)和分布式的區(qū)別以及相關(guān)知識點總結(jié),有興趣的朋友們學(xué)習(xí)下。
    2019-07-07
  • Eclipse快捷鍵使用小結(jié)

    Eclipse快捷鍵使用小結(jié)

    Eclipse是用java的同行必不可少的工具,我總結(jié)了一下它的快捷鍵,太常用的ctrl+單擊、ctrl+shift+F、Ctrl+1等我就不細(xì)說了,主要是方便查看。下邊小編就詳細(xì)的為大家介紹一下
    2013-07-07

最新評論

华坪县| 山西省| 海南省| 华蓥市| 包头市| 山东省| 宜宾县| 延安市| 东丽区| 辛集市| 尉犁县| 塔城市| 临夏县| 夹江县| 邳州市| 通渭县| 白银市| 南昌县| 友谊县| 陆河县| 久治县| 博野县| 高安市| 通辽市| 三亚市| 姚安县| 罗定市| 托里县| 太仓市| 玉山县| 犍为县| 江永县| 商都县| 利津县| 滨州市| 息烽县| 鄂托克前旗| 武宣县| 米泉市| 施秉县| 宜都市|