@Test public void shouldNotBlockUser() throws ServletException, IOException { AuthenticationException exception = new BadCredentialsException("Wrong Password"); exception.setAuthentication(authentication); MotechUser user = createUser(UserStatus.ACTIVE, 2); when(authentication.getName()).thenReturn("testUser"); when(motechUsersDao.findByUserName("testUser")).thenReturn(user); when(settingService.getFailureLoginLimit()).thenReturn(3); motechLoginErrorHandler.onAuthenticationFailure(request, response, exception); verify(response).sendRedirect(LOGIN_ERROR); verify(motechUsersDao).update(userCaptor.capture()); MotechUser capturedUser = userCaptor.getValue(); assertEquals((Integer)3, capturedUser.getFailureLoginCounter()); assertEquals(UserStatus.ACTIVE, capturedUser.getUserStatus()); }
@Override @Transactional public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { if (exception instanceof BadCredentialsException) { MotechUser motechUser = motechUsersDao.findByUserName(exception.getAuthentication().getName()); int failureLoginLimit = settingService.getFailureLoginLimit(); if (motechUser != null && failureLoginLimit > 0) { int failureLoginCounter = motechUser.getFailureLoginCounter(); failureLoginCounter++; if (failureLoginCounter > failureLoginLimit && motechUser.isActive()) { motechUser.setUserStatus(UserStatus.BLOCKED); failureLoginCounter = 0; LOGGER.debug("User {} has been blocked", motechUser.getUserName()); } motechUser.setFailureLoginCounter(failureLoginCounter); motechUsersDao.update(motechUser); } if (motechUser != null && !motechUser.isActive()) { LOGGER.debug("Redirecting to " + userBlockedUrl); redirectStrategy.sendRedirect(request, response, userBlockedUrl); return; } } super.onAuthenticationFailure(request, response, exception); }
MotechLoginErrorHandler extends MotechLoginErrorJSONHandler { @Override @Transactional public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { if (exception instanceof BadCredentialsException) { MotechUser motechUser = motechUsersDao.findByUserName(exception.getAuthentication().getName()); int failureLoginLimit = settingService.getFailureLoginLimit(); if (motechUser != null && failureLoginLimit > 0) { int failureLoginCounter = motechUser.getFailureLoginCounter(); failureLoginCounter++; if (failureLoginCounter > failureLoginLimit && motechUser.isActive()) { motechUser.setUserStatus(UserStatus.BLOCKED); failureLoginCounter = 0; LOGGER.debug("User {} has been blocked", motechUser.getUserName()); } motechUser.setFailureLoginCounter(failureLoginCounter); motechUsersDao.update(motechUser); } if (motechUser != null && !motechUser.isActive()) { LOGGER.debug("Redirecting to " + userBlockedUrl); redirectStrategy.sendRedirect(request, response, userBlockedUrl); return; } } super.onAuthenticationFailure(request, response, exception); } }
MotechLoginErrorHandler extends MotechLoginErrorJSONHandler { @Override @Transactional public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { if (exception instanceof BadCredentialsException) { MotechUser motechUser = motechUsersDao.findByUserName(exception.getAuthentication().getName()); int failureLoginLimit = settingService.getFailureLoginLimit(); if (motechUser != null && failureLoginLimit > 0) { int failureLoginCounter = motechUser.getFailureLoginCounter(); failureLoginCounter++; if (failureLoginCounter > failureLoginLimit && motechUser.isActive()) { motechUser.setUserStatus(UserStatus.BLOCKED); failureLoginCounter = 0; LOGGER.debug("User {} has been blocked", motechUser.getUserName()); } motechUser.setFailureLoginCounter(failureLoginCounter); motechUsersDao.update(motechUser); } if (motechUser != null && !motechUser.isActive()) { LOGGER.debug("Redirecting to " + userBlockedUrl); redirectStrategy.sendRedirect(request, response, userBlockedUrl); return; } } super.onAuthenticationFailure(request, response, exception); } MotechLoginErrorHandler(String defaultFailureUrl, String userBlockedUrl, String changePasswordBaseUrl); }
MotechLoginErrorHandler extends MotechLoginErrorJSONHandler { @Override @Transactional public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { if (exception instanceof BadCredentialsException) { MotechUser motechUser = motechUsersDao.findByUserName(exception.getAuthentication().getName()); int failureLoginLimit = settingService.getFailureLoginLimit(); if (motechUser != null && failureLoginLimit > 0) { int failureLoginCounter = motechUser.getFailureLoginCounter(); failureLoginCounter++; if (failureLoginCounter > failureLoginLimit && motechUser.isActive()) { motechUser.setUserStatus(UserStatus.BLOCKED); failureLoginCounter = 0; LOGGER.debug("User {} has been blocked", motechUser.getUserName()); } motechUser.setFailureLoginCounter(failureLoginCounter); motechUsersDao.update(motechUser); } if (motechUser != null && !motechUser.isActive()) { LOGGER.debug("Redirecting to " + userBlockedUrl); redirectStrategy.sendRedirect(request, response, userBlockedUrl); return; } } super.onAuthenticationFailure(request, response, exception); } MotechLoginErrorHandler(String defaultFailureUrl, String userBlockedUrl, String changePasswordBaseUrl); @Override @Transactional void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception); }
MotechLoginErrorHandler extends MotechLoginErrorJSONHandler { @Override @Transactional public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { if (exception instanceof BadCredentialsException) { MotechUser motechUser = motechUsersDao.findByUserName(exception.getAuthentication().getName()); int failureLoginLimit = settingService.getFailureLoginLimit(); if (motechUser != null && failureLoginLimit > 0) { int failureLoginCounter = motechUser.getFailureLoginCounter(); failureLoginCounter++; if (failureLoginCounter > failureLoginLimit && motechUser.isActive()) { motechUser.setUserStatus(UserStatus.BLOCKED); failureLoginCounter = 0; LOGGER.debug("User {} has been blocked", motechUser.getUserName()); } motechUser.setFailureLoginCounter(failureLoginCounter); motechUsersDao.update(motechUser); } if (motechUser != null && !motechUser.isActive()) { LOGGER.debug("Redirecting to " + userBlockedUrl); redirectStrategy.sendRedirect(request, response, userBlockedUrl); return; } } super.onAuthenticationFailure(request, response, exception); } MotechLoginErrorHandler(String defaultFailureUrl, String userBlockedUrl, String changePasswordBaseUrl); @Override @Transactional void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception);  }