基于java的动态口令_java动态口令登录实现过程详解

基于java的动态口令_java动态口令登录实现过程详解1.实现一个ItsClient客户端用来实例化调用验证功能publicclassItsClient{privatestaticfinalStringrouting=”/v1.0/sectoken/otp_validation”;//!HTTPS消息验证地址privateStringhttpsVerifyUrl=””;//!otpipAddrprivateSt…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

1.实现一个ItsClient 客户端用来实例化调用验证功能

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 errorCodeTable = new TreeMap() {

{

put(200, “请求成功”);

put(400, “输入不合法,比如请求数据不是json”);

put(401, “AppID不合法”);

put(402, “指纹不合法”);

put(410, “非法用户,验证otp时,传入的uid有误,找不到用户”);

put(411, “错误的otp”);

put(412, “一个周期内动态口令只能使用一次”);

put(413, “已达一个周期内最大尝试次数”);

put(500, “ITS服务器内部错误”);

put(601, “参数错误”);

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 map) {

JSONObject jmap = new JSONObject(map);

return jmap.toString();

}

public TreeMap DecodeJson(String jsonStr) throws ParseException {

JSONObject jsonObject = new JSONObject(jsonStr);

TreeMap retMap = new TreeMap();

Iterator 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 params) {

String queryStr = “”;

Iterator 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 需要验证的动态口令

* @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 params = new TreeMap() {

{

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); // 访问接口调取返回结果

} catch (Exception ex) {

ex.printStackTrace();

SetError(604, ex.getMessage());

return false;

}

TreeMap 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.实现一个HttpsClient 请求工具

public class HttpsClient {

final static HostnameVerifier doNotVerifier = new HostnameVerifier() {

public boolean verify(String hostname, SSLSession session) {

return true;

}

};

/**

* @brief 发送请求

* @param httpsUrl 请求的地址

* @param postStr 请求的数据

* @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);

//设置为utf-8可以解决服务器接收时读取的数据中文乱码问题

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.实现Its一个配置用来配置Its服务器信息接口访问地址

public class ItsConf {

// ITS服务器地址 1.1.1.1 或 xxx.xxx.com的形式

private static String ipAddr = “”;

// ITS服务器端口

private static String port = “”;

// OTP服务的AppID

private static String otpAppID = “”;

// OTP服务的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 完成口令认证

//username 用户名

//code动态口令密码

ItsClient itsClient = new ItsClient();

if(itsClient.AuthOtp(username, code)){

//认证成功,跳转页面

}

5.登陆页面就省略了,自己完成吧

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持聚米学院。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/234943.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • C#中FileStream与StreamReader/StreamWriter区别

    C#中FileStream与StreamReader/StreamWriter区别FileStreamFileStream类不是静态类,表示在磁盘或网络路径上指向文件的流。这个类提供了在文件中读写字节的方法,但经常使用StreamReader或StreamWriter执行这些功能。这是因为FileStream类操作的是字节和字节数组,而Stream类操作的是字符数据。使用时需要创建对象,FileStream类既可以对文本文件进行读也可以对多媒体文件进行写,以字节数组的形式进…

    2022年7月21日
    20
  • 【PHP】PHP获得第一章

    【PHP】PHP获得第一章

    2022年1月12日
    46
  • springboot框架简介_spring框架的功能

    springboot框架简介_spring框架的功能SpringBoot框架简单介绍前言:我们大家都知道Spring,Boot是启动的意思,所以SpringBoot其实是一个启动Spring项目的一个工具,从根本上讲,SpringBoot就是一些库的集合,它能够被任意项目的构建系统所使用。 简介:SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配…

    2022年8月21日
    8
  • 小米bl未解锁变砖了如何刷机_如何安装MIUI 10[通俗易懂]

    小米bl未解锁变砖了如何刷机_如何安装MIUI 10[通俗易懂]MIUI,一个安卓系统,如何安装?先说一下,MIUI10目前支持这些小米手机/平板:手机:小米4、红米3S、红米3X、红米Note3、小米4c、红米Pro、红米Note4、小米4S、红米5A、小米5、红米5Plus、小米6、小米Note2、红米6Pro、红米Note5、小米Max2、小米MIX、小米Max、小米MIX2S、小米5X、红米4、小米6X、红米Note5A、红米4A、…

    2022年5月9日
    2.1K
  • 十二、Maven项目模板「建议收藏」

    十二、Maven项目模板「建议收藏」Maven提供用户,使用原型的概念,不同类型的项目模板(以数字614)是一个非常大的列表。Maven帮助用户快速开始使用以下命令创建新的Java项目mvnarchetype:generate什么是原型?原型是一个Maven插件,其任务是创建一个项目结构按照其模板。我们将使用快速启动原型插件在这里创建一个简单的Java应用程序。使用项目模板让我们打开命令控制

    2022年7月19日
    19

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号