@Test public void shouldReturnAuthenticationIfUserValidAndAuthenticated() throws Exception { String userName = "userName"; String password = "valid token"; when(authentication.getPrincipal()).thenReturn(userName); when(authentication.getCredentials()).thenReturn(password); when(userService.selectUserByUserNameAndPassword(userName, Encoder.hash(password))).thenReturn(new User(55L, "userName")); Authentication authenticated = restApiAuthenticationProvider.authenticate(authentication); assertThat((Long) authenticated.getPrincipal(), is(55L)); verify(userService).selectUserByUserNameAndPassword(userName, Encoder.hash(password)); }
@Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { User user = new User(); user.setUserName(authentication.getPrincipal().toString()); user.setPassword(authentication.getCredentials().toString()); User authenticatedUser = userService.selectUserByUserNameAndPassword(user.getUserName(), user.getPassword()); if (authenticatedUser == null) throw new BadCredentialsException(messageService.message("error.authentication.failed")); Collection<? extends GrantedAuthority> authorities = null; return new UsernamePasswordAuthenticationToken(authenticatedUser.getId(), user.getPassword(), authorities); }
RestApiAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { User user = new User(); user.setUserName(authentication.getPrincipal().toString()); user.setPassword(authentication.getCredentials().toString()); User authenticatedUser = userService.selectUserByUserNameAndPassword(user.getUserName(), user.getPassword()); if (authenticatedUser == null) throw new BadCredentialsException(messageService.message("error.authentication.failed")); Collection<? extends GrantedAuthority> authorities = null; return new UsernamePasswordAuthenticationToken(authenticatedUser.getId(), user.getPassword(), authorities); } }
RestApiAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { User user = new User(); user.setUserName(authentication.getPrincipal().toString()); user.setPassword(authentication.getCredentials().toString()); User authenticatedUser = userService.selectUserByUserNameAndPassword(user.getUserName(), user.getPassword()); if (authenticatedUser == null) throw new BadCredentialsException(messageService.message("error.authentication.failed")); Collection<? extends GrantedAuthority> authorities = null; return new UsernamePasswordAuthenticationToken(authenticatedUser.getId(), user.getPassword(), authorities); }  }
RestApiAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { User user = new User(); user.setUserName(authentication.getPrincipal().toString()); user.setPassword(authentication.getCredentials().toString()); User authenticatedUser = userService.selectUserByUserNameAndPassword(user.getUserName(), user.getPassword()); if (authenticatedUser == null) throw new BadCredentialsException(messageService.message("error.authentication.failed")); Collection<? extends GrantedAuthority> authorities = null; return new UsernamePasswordAuthenticationToken(authenticatedUser.getId(), user.getPassword(), authorities); }  @Override Authentication authenticate(Authentication authentication); @Override boolean supports(Class<?> authentication); }
RestApiAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { User user = new User(); user.setUserName(authentication.getPrincipal().toString()); user.setPassword(authentication.getCredentials().toString()); User authenticatedUser = userService.selectUserByUserNameAndPassword(user.getUserName(), user.getPassword()); if (authenticatedUser == null) throw new BadCredentialsException(messageService.message("error.authentication.failed")); Collection<? extends GrantedAuthority> authorities = null; return new UsernamePasswordAuthenticationToken(authenticatedUser.getId(), user.getPassword(), authorities); }  @Override Authentication authenticate(Authentication authentication); @Override boolean supports(Class<?> authentication);  }