@Test public void shouldLoadUserByUsername_authentication_badCredentials() { TestObserver<User> testObserver = authenticationProvider.loadUserByUsername(new Authentication() { @Override public Object getCredentials() { return "wrongpassword"; } @Override public Object getPrincipal() { return "bob"; } @Override public AuthenticationContext getContext() { return null; } }).test(); testObserver.awaitTerminalEvent(); testObserver.assertError(BadCredentialsException.class); }
public Maybe<User> loadUserByUsername(Authentication authentication) { String username = ((String) authentication.getPrincipal()).toLowerCase(); return findUserByUsername(username) .switchIfEmpty(Maybe.error(new UsernameNotFoundException(username))) .map(user -> { String password = user.getString(this.configuration.getPasswordField()); String presentedPassword = authentication.getCredentials().toString(); if (password == null) { LOGGER.debug("Authentication failed: password is null"); throw new BadCredentialsException("Invalid account"); } if (!passwordEncoder.matches(presentedPassword, password)) { LOGGER.debug("Authentication failed: password does not match stored value"); throw new BadCredentialsException("Bad credentials"); } return createUser(user); }); }
MongoAuthenticationProvider implements AuthenticationProvider { public Maybe<User> loadUserByUsername(Authentication authentication) { String username = ((String) authentication.getPrincipal()).toLowerCase(); return findUserByUsername(username) .switchIfEmpty(Maybe.error(new UsernameNotFoundException(username))) .map(user -> { String password = user.getString(this.configuration.getPasswordField()); String presentedPassword = authentication.getCredentials().toString(); if (password == null) { LOGGER.debug("Authentication failed: password is null"); throw new BadCredentialsException("Invalid account"); } if (!passwordEncoder.matches(presentedPassword, password)) { LOGGER.debug("Authentication failed: password does not match stored value"); throw new BadCredentialsException("Bad credentials"); } return createUser(user); }); } }
MongoAuthenticationProvider implements AuthenticationProvider { public Maybe<User> loadUserByUsername(Authentication authentication) { String username = ((String) authentication.getPrincipal()).toLowerCase(); return findUserByUsername(username) .switchIfEmpty(Maybe.error(new UsernameNotFoundException(username))) .map(user -> { String password = user.getString(this.configuration.getPasswordField()); String presentedPassword = authentication.getCredentials().toString(); if (password == null) { LOGGER.debug("Authentication failed: password is null"); throw new BadCredentialsException("Invalid account"); } if (!passwordEncoder.matches(presentedPassword, password)) { LOGGER.debug("Authentication failed: password does not match stored value"); throw new BadCredentialsException("Bad credentials"); } return createUser(user); }); }  }
MongoAuthenticationProvider implements AuthenticationProvider { public Maybe<User> loadUserByUsername(Authentication authentication) { String username = ((String) authentication.getPrincipal()).toLowerCase(); return findUserByUsername(username) .switchIfEmpty(Maybe.error(new UsernameNotFoundException(username))) .map(user -> { String password = user.getString(this.configuration.getPasswordField()); String presentedPassword = authentication.getCredentials().toString(); if (password == null) { LOGGER.debug("Authentication failed: password is null"); throw new BadCredentialsException("Invalid account"); } if (!passwordEncoder.matches(presentedPassword, password)) { LOGGER.debug("Authentication failed: password does not match stored value"); throw new BadCredentialsException("Bad credentials"); } return createUser(user); }); }  Maybe<User> loadUserByUsername(Authentication authentication); Maybe<User> loadUserByUsername(String username); }
MongoAuthenticationProvider implements AuthenticationProvider { public Maybe<User> loadUserByUsername(Authentication authentication) { String username = ((String) authentication.getPrincipal()).toLowerCase(); return findUserByUsername(username) .switchIfEmpty(Maybe.error(new UsernameNotFoundException(username))) .map(user -> { String password = user.getString(this.configuration.getPasswordField()); String presentedPassword = authentication.getCredentials().toString(); if (password == null) { LOGGER.debug("Authentication failed: password is null"); throw new BadCredentialsException("Invalid account"); } if (!passwordEncoder.matches(presentedPassword, password)) { LOGGER.debug("Authentication failed: password does not match stored value"); throw new BadCredentialsException("Bad credentials"); } return createUser(user); }); }  Maybe<User> loadUserByUsername(Authentication authentication); Maybe<User> loadUserByUsername(String username);  }