java动态口令登录实现过程详解

  

<强> 1。实现一个ItsClient客户端用来实例化调用验证功能

  

        公开课ItsClient {   私有静态最终字符串路由="/v1.0 sectoken/otp_validation”;//!HTTPS消息验证地址   私人字符串httpsVerifyUrl=" ";//!otp ipAddr   私人字符串ipAddr=" ";//!otp港口   私人字符串端口=" ";//!otp appID   私人字符串appID=" ";//!otp appKey   私人字符串appKey=" ";//!错误码   私人int errorCode=0;//!错误消息   私人字符串errorMessage=" ";   String> TreeMap<整数;errorCodeTable=new TreeMap<整数,String> () {   {   把(200年,“请求成功”);   把(400年,“输入不合法,比如请求数据不是json ");   把(401年,“AppID不合法”);   (402年,”指纹不合法”);   把(410年,“非法用户,验证otp时,传入的uid有误,找不到用户”);   把(411年,“错误的otp”);   (412年,”一个周期内动态口令只能使用一次”);   把(413年,“已达一个周期内最大尝试次数”);   把(500年,“服务器内部错误”);   把(601年,“参数错误”);   把(602年,“sha1签名失败”);   把(603年,“操作json失败”);   (604年,”url访问错误:”);   把(605年,“较验返回指纹失败”);   }   };   公共ItsClient () {   这一点。ipAddr=ItsConf.GetIpAddr ();   这一点。端口=ItsConf.GetPort ();   这一点。appID=ItsConf.GetOtpAppID ();   这一点。appKey=ItsConf.GetOtpAppKey ();   httpsVerifyUrl=" https://" +。ipAddr + ': ' +。港口+路由;   }   ,//获取错误信息   公共St 环GetErrorMessage () {   返回this.errorMessage;      }      ,,,,,//获取错误码      ,,,,,,,公共int GetErrorCode () {      返回this.errorCode;   }   公共空SetError (int errorCode字符串extMessage) {   这一点。errorCode=错误代码;   这一点。errorMessage=this.errorCodeTable.get (this.errorCode) .toString () + extMessage;   }      SHA1公共静态字符串(字符串decript)抛出NoSuchAlgorithmException {   字符串ret=" ";   MessageDigest sha1=MessageDigest.getInstance (“sha1”);   byte [] sha1bytes=sha1.digest (decript.getBytes ());   如果(sha1bytes !=null) {   ret=new BASE64Encoder () .encode (sha1bytes);   }   返回受潮湿腐烂;   }   公共字符串EncodeJson (TreeMap<字符串,String>地图){   JSONObject jmap=new JSONObject(地图);   返回jmap.toString ();   }   公共TreeMap<字符串,Object>DecodeJson(字符串jsonStr)抛出ParseException {   JSONObject JSONObject=new JSONObject (jsonStr);   Object> TreeMap<字符串;retMap=new TreeMap<字符串,Object> ();   Iteratoriter=jsonObject.keys ();   字符串键=零;   对象价值=https://www.yisu.com/zixun/null;   而(iter.hasNext ()) {   关键=iter.next ();   值=jsonObject.get(关键);   retMap。put(关键字,值);   }   返回retMap;   }   公共字符串BuildQueryStr (TreeMap <字符串,字符串> params) {   字符串queryStr=" ";   Iterator.iterator itr=params.keySet () ();   而(itr.hasNext ()) {   字符串键=itr.next ();   queryStr +=(键+ "=" + params.get(关键).toString() + "和");   }   queryStr返回。substring (0, queryStr.length () - 1);   }   公共布尔IsEmptyOrNull(字符串参数){   返回参数==null | | param.length () & lt;=0;   }/* *   * @brief验证otp   * @param uid其主账号uid或已配置的从账号   * @param otp需要验证的动态口令   * @return bool真的:成功,假:失败   */@SuppressWarnings(“串行”)   公共布尔AuthOtp(最终字符串uid,最终字符串otp) {   如果(IsEmptyOrNull (this.ipAddr) | | IsEmptyOrNull (this.port) | | IsEmptyOrNull (this.appID)   | | IsEmptyOrNull (this.appKey) | | IsEmptyOrNull (uid) | | IsEmptyOrNull (otp)) {   SetError (601“);   返回错误;   }   String> TreeMap<字符串;params=new TreeMap<字符串,String> () {   {   把(“app_id”, appID);   把(“app_key”, appKey);   把(“uid”, uid);   把(otp, otp);   }   };   字符串qureyStr=this.BuildQueryStr (params);   字符串指纹=" ";   尝试{   指纹=SHA1 (qureyStr);   }捕捉(例外的前女友){   ex.printStackTrace ();   SetError(602年,ex.getMessage ());   返回错误;   }   params.remove (“app_key”);   参数个数。把(“指纹”,指纹);   字符串postStr=" ";   尝试{   postStr=EncodeJson (params);   }捕捉(例外的前女友){   ex.printStackTrace ();   SetError(603年,“json编码”+ ex.getMessage ());   返回错误;   }   HttpsClient康涅狄格州=零;   字符串res=" ";   尝试{   康涅狄格州=new HttpsClient ();   res=conn.post(这一点。httpsVerifyUrl postStr);//访问接口调取返回结果   }捕捉(例外的前女友){   ex.printStackTrace ();   SetError(604年,ex.getMessage ());   返回错误;   }   Object> TreeMap

java动态口令登录实现过程详解