@Test public void testHandle_when_account_is_locked() { AuthenticationFailureBadCredentialsEvent loginFailureEvent=mock(AuthenticationFailureBadCredentialsEvent.class); Authentication authentication=mock(Authentication.class); WebAuthenticationDetails webAuthenticationDetails=mock(WebAuthenticationDetails.class); Object name=new String(username); when(loginFailureEvent.getAuthentication()).thenReturn(authentication); when(authentication.getPrincipal()).thenReturn(name); when(authentication.getDetails()).thenReturn(webAuthenticationDetails); when(webAuthenticationDetails.getRemoteAddress()).thenReturn("127.0.0.1"); Users user=mock(Users.class); when(user.getFailedLoginAttempts()).thenReturn(3); when(usersRepository.loadUserByUsername(username)).thenReturn(user); loginFailureEventListener.handle(loginFailureEvent); verify(user).setLockoutTime(any(Calendar.class)); verify(usersRepository).updateUser(user); }
@Override public void handle(Object event) { AuthenticationFailureBadCredentialsEvent loginFailureEvent = (AuthenticationFailureBadCredentialsEvent) event; Object name = loginFailureEvent.getAuthentication().getPrincipal(); Users user = usersRepository.loadUserByUsername((String) name); eventService.raiseSecurityEvent(new AuthenticationFailedEvent( ((WebAuthenticationDetails) loginFailureEvent .getAuthentication().getDetails()).getRemoteAddress(), (String) name)); if (user != null) { user.increaseFailedLoginAttempts(); if (user.getFailedLoginAttempts() >= max_failed_attempts) { Calendar cal = Calendar.getInstance(); user.setLockoutTime(cal); } usersRepository.updateUser(user); } }
LoginFailureEventListener extends EventListener { @Override public void handle(Object event) { AuthenticationFailureBadCredentialsEvent loginFailureEvent = (AuthenticationFailureBadCredentialsEvent) event; Object name = loginFailureEvent.getAuthentication().getPrincipal(); Users user = usersRepository.loadUserByUsername((String) name); eventService.raiseSecurityEvent(new AuthenticationFailedEvent( ((WebAuthenticationDetails) loginFailureEvent .getAuthentication().getDetails()).getRemoteAddress(), (String) name)); if (user != null) { user.increaseFailedLoginAttempts(); if (user.getFailedLoginAttempts() >= max_failed_attempts) { Calendar cal = Calendar.getInstance(); user.setLockoutTime(cal); } usersRepository.updateUser(user); } } }
LoginFailureEventListener extends EventListener { @Override public void handle(Object event) { AuthenticationFailureBadCredentialsEvent loginFailureEvent = (AuthenticationFailureBadCredentialsEvent) event; Object name = loginFailureEvent.getAuthentication().getPrincipal(); Users user = usersRepository.loadUserByUsername((String) name); eventService.raiseSecurityEvent(new AuthenticationFailedEvent( ((WebAuthenticationDetails) loginFailureEvent .getAuthentication().getDetails()).getRemoteAddress(), (String) name)); if (user != null) { user.increaseFailedLoginAttempts(); if (user.getFailedLoginAttempts() >= max_failed_attempts) { Calendar cal = Calendar.getInstance(); user.setLockoutTime(cal); } usersRepository.updateUser(user); } } LoginFailureEventListener(short max_failed_attempts,
			EventService eventService, UsersRepository usersRepository); }
LoginFailureEventListener extends EventListener { @Override public void handle(Object event) { AuthenticationFailureBadCredentialsEvent loginFailureEvent = (AuthenticationFailureBadCredentialsEvent) event; Object name = loginFailureEvent.getAuthentication().getPrincipal(); Users user = usersRepository.loadUserByUsername((String) name); eventService.raiseSecurityEvent(new AuthenticationFailedEvent( ((WebAuthenticationDetails) loginFailureEvent .getAuthentication().getDetails()).getRemoteAddress(), (String) name)); if (user != null) { user.increaseFailedLoginAttempts(); if (user.getFailedLoginAttempts() >= max_failed_attempts) { Calendar cal = Calendar.getInstance(); user.setLockoutTime(cal); } usersRepository.updateUser(user); } } LoginFailureEventListener(short max_failed_attempts,
			EventService eventService, UsersRepository usersRepository); @Override boolean canHandle(Object event); @Override void handle(Object event); }
LoginFailureEventListener extends EventListener { @Override public void handle(Object event) { AuthenticationFailureBadCredentialsEvent loginFailureEvent = (AuthenticationFailureBadCredentialsEvent) event; Object name = loginFailureEvent.getAuthentication().getPrincipal(); Users user = usersRepository.loadUserByUsername((String) name); eventService.raiseSecurityEvent(new AuthenticationFailedEvent( ((WebAuthenticationDetails) loginFailureEvent .getAuthentication().getDetails()).getRemoteAddress(), (String) name)); if (user != null) { user.increaseFailedLoginAttempts(); if (user.getFailedLoginAttempts() >= max_failed_attempts) { Calendar cal = Calendar.getInstance(); user.setLockoutTime(cal); } usersRepository.updateUser(user); } } LoginFailureEventListener(short max_failed_attempts,
			EventService eventService, UsersRepository usersRepository); @Override boolean canHandle(Object event); @Override void handle(Object event);  }