java动态口令登录实现过程详解
作者:Shen_sy 发布时间:2022-01-01 10:16:28
标签:java,动态,口令,登录
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<Integer, String> errorCodeTable = new TreeMap<Integer, String>() {
{
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 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 需要验证的动态口令
* @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); // 访问接口调取返回结果
} 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.实现一个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://www.cnblogs.com/wuaili/p/9810661.html
0
投稿
猜你喜欢
- 需求:Android调用webView加载网页的时候,拦截某一个链接不执行此链接,执行指定跳转到其他activity页面。webview的s
- 1. 启用AOPa. 在类上添加@Aspect注解b. 注入该类, 可以使用@Component进行注入到Spring容器中2. 通过Poi
- 1. ObsoleteAttributeObsoleteAttribute 适用于除组件、模块、参数和返回值以外的所有程序元素。将元素标记为
- 从概念上看,值类型直接存储其值,而引用类型存储对其值的引用。这两种类型存储在内存的不同地方。在C#中,我们必须在设计类型的时候就决定类型实例
- 今天看了一下数据结构,一个练习就是构建哈夫曼树,就顺手用C#写了一个。static void Main(string[] args){ &n
- Kotlin中的一个伟大创前举就是空指针的处理,在代码的编译阶段就能检测可能出现的空指针问题,示例代码如下:data class Perso
- synchronizedsynchronized可以用来同步块,同步方法。同步块可以用来更精确地控制对象锁,控制锁的作用域。(锁
- 本文实例为大家分享了C#字数统计(字母、数字、汉字、符号)的具体代码,供大家参考,具体内容如下namespace 测试1{ public p
- 问题是这样的在开发时,为了节约时间,我选择了mybatis框架来开发,然后又在网上找了一个许多人都推荐的mybatis-plus来作为持久层
- 背景Android开发中,加载图片过多、过大很容易引起OutOfMemoryError异常,即我们常见的内存溢出。因为Android对单个应
- MultipartResolver和ServletFileUpload冲突如果同时使用了MultipartResolver 和Servlet
- checkbox控件时导致Activity启动默认不显示输入法。网上很多资料说要放一个空的Linearlayout,完全是在误导大众,正确的
- 目录图像修复API操作效果源码图像修复实际应用中,图像常常容易受损,如存在污渍的镜头、旧照片的划痕、人为的涂画(比如马赛克),亦或是图像本身
- 摘要 想必大家对小榕时光等扫描器都非常熟悉了,有没有自己写一个的冲动。最近微软推实施了.NET战略方案,C#是主推语言,你们是否
- 本文实例讲述了Java Arrays工具类用法。分享给大家供大家参考,具体如下:Arrays类功能描述Arrays类是一个工具类,其中包含了
- 1、MyBatis调用存储过程MyBatis支持使用存储过程的配置。当使用存储过程时,需要设置一个参数“mode”,其值有IN(输入参数)、
- 一、开发环境:1、windows 7 企业版2、IDEA 143、JDK 1.84、Maven 3.5.25、MariaDB6、SQLYog
- 这是一个演示如何使用java执行定时任务的实例,本实例开始运行后不会自动结束,请在运行本实例后手动结束程序。package com.hong
- c#数据绑定之将datatabel的data添加listView中,简要的通过代码应用了DataTable,DataTableColumns
- 在activity级下使用this表示contextkotlin中取消了xxxActivity.this的用法,所以我们可以在activit