@Test public void shouldLoadUserByUsername_authentication_badCredentials() throws Exception { embeddedLdapRule.ldapConnection(); TestObserver<User> testObserver = authenticationProvider.loadUserByUsername(new Authentication() { @Override public Object getCredentials() { return "wrongpassword"; } @Override public Object getPrincipal() { return "ben"; } @Override public AuthenticationContext getContext() { return null; } }).test(); testObserver.assertError(BadCredentialsException.class); }
@Override public Maybe<User> loadUserByUsername(Authentication authentication) { return Maybe.fromCallable(() -> { try { String username = (String) authentication.getPrincipal(); String password = (String) authentication.getCredentials(); AuthenticationResponse response = authenticator.authenticate(new AuthenticationRequest(username, new Credential(password), ReturnAttributes.ALL_USER.value())); if (response.getResult()) { LdapEntry userEntry = response.getLdapEntry(); return userEntry; } else { LOGGER.debug("Failed to authenticate user", response.getMessage()); throw new BadCredentialsException(response.getMessage()); } } catch (LdapException e) { LOGGER.error("An error occurs during LDAP authentication", e); throw new InternalAuthenticationServiceException(e.getMessage(), e); } }) .map(this::createUser); }
LdapAuthenticationProvider extends AbstractService<AuthenticationProvider> implements AuthenticationProvider, InitializingBean { @Override public Maybe<User> loadUserByUsername(Authentication authentication) { return Maybe.fromCallable(() -> { try { String username = (String) authentication.getPrincipal(); String password = (String) authentication.getCredentials(); AuthenticationResponse response = authenticator.authenticate(new AuthenticationRequest(username, new Credential(password), ReturnAttributes.ALL_USER.value())); if (response.getResult()) { LdapEntry userEntry = response.getLdapEntry(); return userEntry; } else { LOGGER.debug("Failed to authenticate user", response.getMessage()); throw new BadCredentialsException(response.getMessage()); } } catch (LdapException e) { LOGGER.error("An error occurs during LDAP authentication", e); throw new InternalAuthenticationServiceException(e.getMessage(), e); } }) .map(this::createUser); } }
LdapAuthenticationProvider extends AbstractService<AuthenticationProvider> implements AuthenticationProvider, InitializingBean { @Override public Maybe<User> loadUserByUsername(Authentication authentication) { return Maybe.fromCallable(() -> { try { String username = (String) authentication.getPrincipal(); String password = (String) authentication.getCredentials(); AuthenticationResponse response = authenticator.authenticate(new AuthenticationRequest(username, new Credential(password), ReturnAttributes.ALL_USER.value())); if (response.getResult()) { LdapEntry userEntry = response.getLdapEntry(); return userEntry; } else { LOGGER.debug("Failed to authenticate user", response.getMessage()); throw new BadCredentialsException(response.getMessage()); } } catch (LdapException e) { LOGGER.error("An error occurs during LDAP authentication", e); throw new InternalAuthenticationServiceException(e.getMessage(), e); } }) .map(this::createUser); }  }
LdapAuthenticationProvider extends AbstractService<AuthenticationProvider> implements AuthenticationProvider, InitializingBean { @Override public Maybe<User> loadUserByUsername(Authentication authentication) { return Maybe.fromCallable(() -> { try { String username = (String) authentication.getPrincipal(); String password = (String) authentication.getCredentials(); AuthenticationResponse response = authenticator.authenticate(new AuthenticationRequest(username, new Credential(password), ReturnAttributes.ALL_USER.value())); if (response.getResult()) { LdapEntry userEntry = response.getLdapEntry(); return userEntry; } else { LOGGER.debug("Failed to authenticate user", response.getMessage()); throw new BadCredentialsException(response.getMessage()); } } catch (LdapException e) { LOGGER.error("An error occurs during LDAP authentication", e); throw new InternalAuthenticationServiceException(e.getMessage(), e); } }) .map(this::createUser); }  @Override void afterPropertiesSet(); @Override Maybe<User> loadUserByUsername(Authentication authentication); @Override Maybe<User> loadUserByUsername(String username); }
LdapAuthenticationProvider extends AbstractService<AuthenticationProvider> implements AuthenticationProvider, InitializingBean { @Override public Maybe<User> loadUserByUsername(Authentication authentication) { return Maybe.fromCallable(() -> { try { String username = (String) authentication.getPrincipal(); String password = (String) authentication.getCredentials(); AuthenticationResponse response = authenticator.authenticate(new AuthenticationRequest(username, new Credential(password), ReturnAttributes.ALL_USER.value())); if (response.getResult()) { LdapEntry userEntry = response.getLdapEntry(); return userEntry; } else { LOGGER.debug("Failed to authenticate user", response.getMessage()); throw new BadCredentialsException(response.getMessage()); } } catch (LdapException e) { LOGGER.error("An error occurs during LDAP authentication", e); throw new InternalAuthenticationServiceException(e.getMessage(), e); } }) .map(this::createUser); }  @Override void afterPropertiesSet(); @Override Maybe<User> loadUserByUsername(Authentication authentication); @Override Maybe<User> loadUserByUsername(String username);  }