1.实体
/**
*
* <p>Title: LoginLog</p>
* <p>Description: 登陆失败记录表</p>
* <p>Company: </p>
* @author chenlf
* @date 2020年4月22日 上午9:28:49
*/
public class LoginLog extends DataEntity<LoginLog> {
private static final long serialVersionUID = 1L;
private String loginName; // 登录账号
private String loginPhone; //手机号
private Date lastTime; // 最后一次登录失败时间
private Integer failures; //登陆失败次数
private String isLock; //是否锁定:0 解锁,1 锁定
public static final int FAILURES_DEFAULT = 1;//默认登陆失败次数
public static final int FAILURES_MAX = 5;//登录失败最大次数
public static final int LOCK_MIN_LONG = 30;//锁定时长(分钟)
public static final int INSIDE_MIN_OPERATE = 1;//频繁操作时间段(分钟)
public LoginLog() {
super();
}
public LoginLog(String loginName, String loginPhone){
this.loginName = loginName;
this.loginPhone = loginPhone;
this.failures = FAILURES_DEFAULT;
this.isLock = Global.NO;
this.lastTime = new Date();
}
//get
//set
}
2.实现
/**
*
* @title checkFailures
* @Description 检查用户登录失败次数
* @author chenlf
* @param username
* @return
*/
@Transactional(readOnly = false)
public String checkIsLock(String loginName, String phone) {
String msg = null;
LoginLog loginLog = new LoginLog();
if (StringUtils.isNotBlank(loginName)) {//根据账号登录
loginLog = this.getByLoginName(loginName);
} else {//根据手机号
loginLog = this.getByLoginPhone(phone);
}
if(loginLog != null){
String acount = StringUtils.isNotBlank(loginLog.getLoginName()) ? loginLog.getLoginName() : loginLog.getLoginPhone();
int min = (int) ((System.currentTimeMillis()-loginLog.getLastTime().getTime())/60000);//锁定时间分钟
switch (loginLog.getIsLock()) {
case Global.YES://锁定
if (min < LoginLog.LOCK_MIN_LONG){//时间小于锁定分钟
msg = "您的账号<font color='red'>"+acount+"</font>已被锁定,请"+(LoginLog.LOCK_MIN_LONG-min)+"分钟之后再次尝试登录!";
} else {//解锁并重置次数
loginLog.setFailures(LoginLog.FAILURES_DEFAULT);
loginLog.setIsLock(Global.NO);
}
break;
case Global.NO://未锁定
//n分钟内频繁操作则作记录
if (min < LoginLog.INSIDE_MIN_OPERATE) {
int num = loginLog.getFailures()+1;
if (num == LoginLog.FAILURES_MAX) {
msg = "您的账号<font color='red'>"+acount+"</font>已被锁定,请"+ LoginLog.LOCK_MIN_LONG +"分钟之后再次尝试登录!";
loginLog.setIsLock(Global.YES);
}
loginLog.setFailures(num);
} else {//不是n分钟内操作的,重置次数为1
loginLog.setFailures(LoginLog.FAILURES_DEFAULT);
}
loginLog.setLastTime(new Date());
break;
default:
break;
}
} else {
loginLog = new LoginLog(loginName, phone);
}
this.save(loginLog);
return msg;
}