仅贴出关键代码
Jedis jedis = new Jedis("192.168.1.105", 6379);
@Override
public Map loginByUserName(String userName, String password) {
//根据用户名获取用户信息
List<Map> list = this.userMapper.selectByUserName( userName );
if (list.size() == 0) {
checkUser(userName,1);
}
Long userId = Long.valueOf( list.get( 0 ).get( "userId" ).toString() );
//更新登录次数及最后登录时间
this.userMapper.updateUserLoginCount( userId, new Date() );
return login( list, password, userName );
}
public Map login(List<Map> list, String password, String loginId ) {
Map resultMap = new HashMap();
if (list.size() == 0) {
checkUser(loginId,1);
} else {
Map<String, Object> userMap = list.get( 0 );
if (null != password) {//仅loginbyThird没有密码
if (!password.equals( userMap.get( "password" ) )) {
checkUser(loginId,1);
}
checkUser(loginId,0);
}
...
}
}
private void checkUser(String loginId,Integer type){
jedis.auth("123456");
Boolean exit = jedis.exists(loginId);
if (exit == true && ("true".equals(jedis.get(loginId)))){
throw new BusinessException( "该账号已被锁,请确认" );
}else if (exit == true && "5".equals(jedis.get(loginId))){
//锁帐号
jedis.set(loginId, "true");
//设置半个小时的过期时间
jedis.expire(loginId, 30 * 60);
throw new BusinessException( "该账号已被锁,请确认" );
}else {
checkNameAndPassword(loginId, type);
}
}
private void checkNameAndPassword(String loginId, Integer type) {
if (type == 0) {
jedis.del(loginId);
System.out.println("登录成功");
}else {
if (jedis.exists(loginId)) {
jedis.incr(loginId);
}else {
jedis.setex(loginId, 300, "1");
}
System.out.println("第" + jedis.get(loginId) + "次登录失败");
throw new BusinessException( "账号密码错误,你还有" + (5-Integer.parseInt(jedis.get(loginId))) + "次机会" );
}
}