From 818257c3535b4dff2c26aa0a4af200bec635b0fd Mon Sep 17 00:00:00 2001 From: Saurav Mishra Date: Mon, 22 Jun 2026 16:44:01 +0530 Subject: [PATCH 1/2] fix issue of feature multiple login attempt --- .../users/IEMRAdminUserServiceImpl.java | 28 ++++--------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/iemr/common/service/users/IEMRAdminUserServiceImpl.java b/src/main/java/com/iemr/common/service/users/IEMRAdminUserServiceImpl.java index 13adda14..3d20be1b 100644 --- a/src/main/java/com/iemr/common/service/users/IEMRAdminUserServiceImpl.java +++ b/src/main/java/com/iemr/common/service/users/IEMRAdminUserServiceImpl.java @@ -265,28 +265,12 @@ public List userAuthenticate(String userName, String password) throws Exce checkUserAccountStatus(user); iEMRUserRepositoryCustom.save(user); } else if (validatePassword == 0) { - int currentFailedAttempt = - user.getFailedAttempt() != null ? user.getFailedAttempt() : 0; - - int newFailedAttempt = currentFailedAttempt + 1; - int remainingAttempts = failedAttempt - newFailedAttempt; - if (newFailedAttempt < failedAttempt) { - - user.setFailedAttempt(newFailedAttempt); + if (user.getFailedAttempt() + 1 < failedAttempt) { + user.setFailedAttempt(user.getFailedAttempt() + 1); user = iEMRUserRepositoryCustom.save(user); - logger.warn("User Password Wrong"); - - if (remainingAttempts == 1) { - throw new IEMRException( - "Invalid username or password. Remaining attempts: 1. " - + "If you enter wrong username or password again, your account will be locked."); - } - - throw new IEMRException( - "Invalid username or password. Remaining attempts: " - + remainingAttempts); - }else if (user.getFailedAttempt() + 1 >= failedAttempt) { + throw new IEMRException("Invalid username or password"); + } else if (user.getFailedAttempt() + 1 >= failedAttempt) { user.setFailedAttempt(user.getFailedAttempt() + 1); user.setDeleted(true); user = iEMRUserRepositoryCustom.save(user); @@ -294,14 +278,14 @@ public List userAuthenticate(String userName, String password) throws Exce ConfigProperties.getInteger("failedLoginAttempt")); throw new IEMRException( - "Your account has been locked due to multiple failed login attempts. Please contact administrator."); + "Invalid username or password. Please contact administrator."); } else { user.setFailedAttempt(user.getFailedAttempt() + 1); user = iEMRUserRepositoryCustom.save(user); logger.warn("Failed login attempt {} of {} for a user account.", user.getFailedAttempt(), ConfigProperties.getInteger("failedLoginAttempt")); throw new IEMRException( - "Your account has been locked due to multiple failed login attempts. Please contact administrator."); + "Invalid username or password. Please contact administrator."); } } else { checkUserAccountStatus(user); From a27db85fa8bf9c5324245b756fff31cd5ea4ec61 Mon Sep 17 00:00:00 2001 From: Saurav Mishra Date: Mon, 22 Jun 2026 17:07:04 +0530 Subject: [PATCH 2/2] fix issue of feature multiple login attempt --- .../controller/users/IEMRAdminController.java | 71 ------------------- .../users/IEMRAdminUserServiceImpl.java | 28 ++++++-- 2 files changed, 22 insertions(+), 77 deletions(-) diff --git a/src/main/java/com/iemr/common/controller/users/IEMRAdminController.java b/src/main/java/com/iemr/common/controller/users/IEMRAdminController.java index b3644743..f3612fdb 100644 --- a/src/main/java/com/iemr/common/controller/users/IEMRAdminController.java +++ b/src/main/java/com/iemr/common/controller/users/IEMRAdminController.java @@ -171,83 +171,12 @@ public String userAuthenticate( } String decryptPassword = aesUtil.decrypt("Piramal12Piramal", m_User.getPassword()); - // Fetch user - List existingUser = iemrAdminUserServiceImpl.userExitsCheck(m_User.getUserName()); - - /* - * ========================================= - * ACCOUNT LOCK CHECK - * ========================================= - */ - if(!existingUser.isEmpty()){ - if (existingUser.get(0) != null - && existingUser.get(0).getFailedAttempt() != null - && existingUser.get(0).getFailedAttempt() >= 5) { - - throw new IEMRException( - "Your account has been locked due to multiple failed login attempts. Please contact administrator."); - } - } List mUser = iemrAdminUserServiceImpl .userAuthenticate(m_User.getUserName(), decryptPassword); - /* - * ========================================= - * FAILED LOGIN ATTEMPT LOGIC - * ========================================= - */ - if(!existingUser.isEmpty()){ - if (existingUser != null) { - - Integer failedAttempt = existingUser.get(0).getFailedAttempt() != null - ? existingUser.get(0).getFailedAttempt() - : 0; - - failedAttempt++; - - existingUser.get(0).setFailedAttempt(failedAttempt); - - iemrAdminUserServiceImpl.save(existingUser.get(0)); - - int remainingAttempts = 5 - failedAttempt; - - // Lock account on 5th attempt - if (failedAttempt >= 5) { - - - - response.setError(new IEMRException( - "Your account has been locked due to multiple failed login attempts.")); - return response.toString(); - } - - // Warning on 3rd attempt - if (failedAttempt == 4) { - - - response.setError(new IEMRException( - "Invalid username or password. Remaining attempts: " - + remainingAttempts - + ". If you enter wrong username or password again, your account will be locked.")); - return response.toString(); - } - - - response.setError(new IEMRException( - "Invalid username or password. Remaining attempts: " - + remainingAttempts)); - return response.toString(); - - } - } - /* - * ========================================= - * RESET FAILED ATTEMPTS ON SUCCESS LOGIN - * ========================================= - */ User loggedInUser = mUser.get(0); loggedInUser.setFailedAttempt(0); diff --git a/src/main/java/com/iemr/common/service/users/IEMRAdminUserServiceImpl.java b/src/main/java/com/iemr/common/service/users/IEMRAdminUserServiceImpl.java index 3d20be1b..13adda14 100644 --- a/src/main/java/com/iemr/common/service/users/IEMRAdminUserServiceImpl.java +++ b/src/main/java/com/iemr/common/service/users/IEMRAdminUserServiceImpl.java @@ -265,12 +265,28 @@ public List userAuthenticate(String userName, String password) throws Exce checkUserAccountStatus(user); iEMRUserRepositoryCustom.save(user); } else if (validatePassword == 0) { - if (user.getFailedAttempt() + 1 < failedAttempt) { - user.setFailedAttempt(user.getFailedAttempt() + 1); + int currentFailedAttempt = + user.getFailedAttempt() != null ? user.getFailedAttempt() : 0; + + int newFailedAttempt = currentFailedAttempt + 1; + int remainingAttempts = failedAttempt - newFailedAttempt; + if (newFailedAttempt < failedAttempt) { + + user.setFailedAttempt(newFailedAttempt); user = iEMRUserRepositoryCustom.save(user); + logger.warn("User Password Wrong"); - throw new IEMRException("Invalid username or password"); - } else if (user.getFailedAttempt() + 1 >= failedAttempt) { + + if (remainingAttempts == 1) { + throw new IEMRException( + "Invalid username or password. Remaining attempts: 1. " + + "If you enter wrong username or password again, your account will be locked."); + } + + throw new IEMRException( + "Invalid username or password. Remaining attempts: " + + remainingAttempts); + }else if (user.getFailedAttempt() + 1 >= failedAttempt) { user.setFailedAttempt(user.getFailedAttempt() + 1); user.setDeleted(true); user = iEMRUserRepositoryCustom.save(user); @@ -278,14 +294,14 @@ public List userAuthenticate(String userName, String password) throws Exce ConfigProperties.getInteger("failedLoginAttempt")); throw new IEMRException( - "Invalid username or password. Please contact administrator."); + "Your account has been locked due to multiple failed login attempts. Please contact administrator."); } else { user.setFailedAttempt(user.getFailedAttempt() + 1); user = iEMRUserRepositoryCustom.save(user); logger.warn("Failed login attempt {} of {} for a user account.", user.getFailedAttempt(), ConfigProperties.getInteger("failedLoginAttempt")); throw new IEMRException( - "Invalid username or password. Please contact administrator."); + "Your account has been locked due to multiple failed login attempts. Please contact administrator."); } } else { checkUserAccountStatus(user);