身份认证列表的业务实现

身份认证列表的业务实现


一般用户修改/重置支付密码、登录密码等业务实现时,我们需要用户进行几步身份认证,才可让其他通过。
先贴一个类似的效果图:展示了几个认证流程(流程中的几个认证步骤暂没图了)



根据业务列举几个可提供的认证列表:
手机otp、交易密码验证、身份证、邮箱验证、当前登录密码、联系客服等。除联系客服验证之外,其它验证可以自由排列组合,即:手机otp+交易密码验证或手机otp+交易密码验证+当前登录密码等等,只要业务合理,自由排列。

那如何实现此类列表验证功能,而且每个验证可以自由组合,而不能改变现有代码,即可扩展、可增加。

业务分析:身份验证的列表属于某一类认证流程,此认证流程包含几步认证方式,即上面的手机otp、交易密码验证、身份证、邮箱验证、当前登录密码等步骤的自由排列组合。
抽象成数据结构,可以利用二维数组来表示:
行index i表示认证认证流程的一类流程,
列index j表示某认证流程中的第N步的步骤值(步骤我们也用常量数字标识)。
其值a[i][j]表示这种认证流程编号i,第j步骤的后续步骤的列值即步骤值.

我们可以定义枚举表示认证流程:
public enum AuthenticationCategory {
OtpAndIdCard(1, "手机验证", "手机otp+身份证"), //手机otp+身份证
Otp(2, "手机验证", "手机otp"), //手机otp
TradingPasswdAndIdCard(3, "交易密码验证", "交易密码+身份证"), //交易密码+身份证
MailBoxAuthenticationCodeAndIdCard(4, "邮箱验证", "邮箱otp+身份证"), //邮箱otp+身份证
MailBoxAuthenticationCode(5, "邮箱验证", "邮箱otp"), //邮箱otp
CustomerService(6, "联系客服", "联系客服"),
LoginPasswd(7, "当前登录密码", "当前登录密码"),
.................
TradingPasswd(8, "交易密码验证", "交易密码验证");//交易密码
private int priority; //身份认证流程编号
private String description;//流程描述:流程里面的步骤及顺序,可以供显示

private String detailDescription;//流程具体描述:流程里面的步骤及顺序

private AuthenticationCategory(int priority, String description, String detailDescription) {
this.priority = priority;
this.description = description;
this.detailDescription = detailDescription;
}

.......

}

流程编号的定义不一定非得用枚举,适合业务就行。

流程编号定义了,我们还需要定义每个流程中的步骤编号:

/**
* 类AuthenticationCategory.java的实现描述: 验证类别步骤
*
*/
public static final class AuthenticationCategoryStep {
//利用这个值可以获取每个流程的第一步step值.
public static final int begin_index = 0;
/**
* 手机opt验证
*/
public static final int phone_opt_step_index = 1;
/**
* 身份证验证
*/
public static final int id_card_step_index = 2;
/**
* 交易密码验证
*/
public static final int trading_passwd_step_index = 3;
/**
* 邮箱验证码验证
*/
public static final int mail_opt_step_index = 4;

/**
* 当前登录密码验证
*/
public static final int loginPasswd_step_index = 5;

/**
* 表示无后续步骤,当前流程认证通过
*/
public static final int end_index = 88;

}

上面定义的认证步骤编号自由组合就成了一套认证流程,按照业务逻辑和数学语言来说,是上述认证步骤的 排列方式 组成了用户认证的流程。
现在利用二维数组表示认证流程及流程中各步骤顺序之间的关系:
认证流程数决定了数组的行值,认证步骤数决定了数组的列值。
数组的第一行索引0,我们先不用表示任何流程(但你可以表示流程编号的)。

/**
* 二维数组表示用户所处流程:行index表示AuthenticationCategory的一种认证流程;
* 列index表示AuthenticationCategoryStep流程中的第N步的步骤值。
* a[i][j]表示这种认证i,第j步骤的后续步骤AuthenticationCategoryStep值. [可以实例化在xml中]
*/
private static final Integer[][] stepIndex = new Integer[][] {
{ null, null, null, null, null, null },
{ phone_opt_step_index, id_card_step_index, end_index, null, null, null },
//OtpAndIdCard验证步骤;1,0 = 1;1,1=2;1,2=88
{ phone_opt_step_index, end_index, null, null, null, null },
//Otp认证;2,0=1;2,1=88
{ trading_passwd_step_index, null, end_index, id_card_step_index, null, null },
//TradingPasswdAndIdCard认证;3,0=3;3,3=2;3,2=88
{ mail_opt_step_index, null, end_index, null, id_card_step_index, null },
//MailBoxAuthenticationCodeAndIdCard认证: 4,0=4;4,4=2;,4,2=88
{ mail_opt_step_index, null, null, null, end_index, null },
//MailBoxAuthenticationCode认证: 5,0=4;5,4=88
{ end_index, null, null, null, null, null },
{ loginPasswd_step_index, null, null, null, null, end_index },
{ trading_passwd_step_index, null, null, end_index, null, null },
//TradingPasswd认证;8,0=3; 8,3=88
};

public static Integer getNextStep(int authenticationPriority, int step) {
if (authenticationPriority < 0 || authenticationPriority > stepIndex.length - 1) {
return null;
}

Integer[] array = stepIndex[authenticationPriority];
int n = array.length;
if (step < 0 || step > n - 1) {
return null;
}

return stepIndex[authenticationPriority][step];
}

public static Integer getPreviousStep(int authenticationPriority, int step) {
if (authenticationPriority < 0 || authenticationPriority > stepIndex.length - 1) {
return null;
}

Integer[] array = stepIndex[authenticationPriority];
int n = array.length;
if (step != end_index && (step < 0 || step > n - 1)) {
return null;
}

for (int i = 0; i < n; i++) {
Integer value = array[i];
if (value != null && value.intValue() == step) {
return i;
}
}
return null;
}

/**
* 当前流程中的最后 验证步骤
*
* @param authenticationPriority
* @return
*/
public static Integer getEndStep(int authenticationPriority) {
if (authenticationPriority < 0 || authenticationPriority > stepIndex.length - 1) {
return null;
}

Integer[] array = stepIndex[authenticationPriority];
int n = array.length;
for (int i = 0; i < n; i++) {
if (array[i] != null && array[i] == AuthenticationCategoryStep.end_index) {
return i;
}
}

return null;
}

public static boolean validedAuthenticationCategoryStep(int step) {
return authenticationCategorySteps.contains(step);
}

当获取用户的认证流程,我们可以根据业务返回认证流程编号(按照业务排序)及对应的此流程的第一个认证步骤值,后面的业务我们就可以动态的获取此流程此步骤的下一步骤值,而且我们可以获取当前流程当前步骤的前一步骤的值(可以用来判断是不是之前步骤校验通过--缓存来存取状态).

当认证步骤增加时,数组的行和列要随着变化,当认证流程中的步骤增加、减少或顺序调动,我们可以增加流程编号,来表示新的认证流程即可,可动态扩展,不必改动,唯一变动的是获取认证流程的业务实现(比如顺序、流程的选择与丢弃)。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dreamer who

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值