use of com.webank.wedatasphere.qualitis.exception.LoginFailedException in project Qualitis by WeBankFinTech.
the class LoginServiceImpl method localLogin.
@Override
public GeneralResponse<?> localLogin(LocalLoginRequest request) throws LoginFailedException, UnExpectedRequestException {
// Check Arguments
checkRequest(request);
String username = request.getUsername();
String password = request.getPassword();
long currentLoginTime = System.currentTimeMillis();
User userInDb = userDao.findByUsername(username);
if (userInDb == null) {
throw new LoginFailedException("{&USER_NOT_EXIST}");
}
if (userInDb.getLockTime() != null && (currentLoginTime - userInDb.getLockTime()) / (1000 * 60) < 10) {
String lockTime = SDF.format(new Date(userInDb.getLockTime()));
LOGGER.info("Login locked. user: {}, lock time: {}", username, lockTime);
throw new LoginFailedException("{&LOGIN_LOCKED}" + lockTime);
}
if (localLogin(userInDb, password)) {
addToSession(username, httpRequest);
clearErrorLoginRecord(userInDb);
userDao.saveUser(userInDb);
LOGGER.info("Succeed to login. user: {}, current_user: {}", username, username);
return new GeneralResponse<>("200", "{&LOGIN_SUCCESS}", null);
} else {
// Login failed in first time.
if (userInDb.getLoginErrorTime() == null || userInDb.getLoginErrorCount() == null) {
userInDb.setLoginErrorTime(currentLoginTime);
userInDb.setLoginErrorCount(1);
} else {
// Check error count in 5 minutes decide to lock
boolean consecutiveError = (currentLoginTime - userInDb.getLoginErrorTime()) / (1000 * 60) < 5;
if (consecutiveError) {
userInDb.setLoginErrorCount(userInDb.getLoginErrorCount() + 1);
if (userInDb.getLoginErrorCount() >= 5) {
userInDb.setLockTime(currentLoginTime);
}
} else {
userInDb.setLoginErrorTime(currentLoginTime);
userInDb.setLoginErrorCount(1);
}
}
userDao.saveUser(userInDb);
throw new LoginFailedException("{&LOGIN_FAILED}" + (5 - userInDb.getLoginErrorCount()));
}
}
Aggregations