MotechAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider { @Override protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) { String password = (String) authentication.getCredentials(); if (StringUtils.isEmpty(password)) { throw new BadCredentialsException(PLEASE_ENTER_PASSWORD); } if (!passwordEncoder.isPasswordValid(userDetails.getPassword(), password)) { throw new BadCredentialsException(USER_NOT_FOUND); } } @Autowired  MotechAuthenticationProvider(MotechUsersDao motechUsersDao, MotechPasswordEncoder motechPasswordEncoder,
                                        AuthoritiesService authoritiesService, SettingService settingService);  static final String PLEASE_ENTER_PASSWORD; static final String USER_NOT_FOUND; static final String USER_BLOCKED; }
@Test public void shouldAuthenticateUser() { when(passwordEncoder.isPasswordValid("encodedPassword", "password")).thenReturn(true); UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("bob", "password"); UserDetails user = mock(UserDetails.class); when(user.getPassword()).thenReturn("encodedPassword"); authenticationProvider.additionalAuthenticationChecks(user, authentication); }
@Test(expected = AuthenticationException.class) public void shouldNotAuthenticateEmptyPassword() { UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("bob", ""); UserDetails user = mock(UserDetails.class); when(user.getPassword()).thenReturn("encodedPassword"); authenticationProvider.additionalAuthenticationChecks(user, authentication); }
@Test(expected = AuthenticationException.class) public void shouldNotAuthenticateWrongPassword() { when(passwordEncoder.isPasswordValid("encodedPassword", "password")).thenReturn(false); UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("bob", ""); UserDetails user = mock(UserDetails.class); when(user.getPassword()).thenReturn("encodedPassword"); authenticationProvider.additionalAuthenticationChecks(user, authentication); }