@Test public void testAuthenticateInvalid() { when(userService.isLoginValid(anyString(), anyString())).thenReturn(null); Authentication authentication = mock(Authentication.class); when(authentication.getName()).thenReturn("username"); when(authentication.getCredentials()).thenReturn("pass"); try { authProviderService.authenticate(authentication); fail("This code should not be executed"); } catch (UsernameNotFoundException e) { assertNotNull(e); } }
@Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { String login = authentication.getName(); String password = authentication.getCredentials().toString(); LOGGER.info("Doing login " + login); User u = userService.isLoginValid(login, password); if(u != null) { LOGGER.info("Login successful. User: " + login); return usernamePasswordAuthenticationTokenFactory.create(u); } throw new UsernameNotFoundException("Not valid login/password"); }
AuthProviderService implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { String login = authentication.getName(); String password = authentication.getCredentials().toString(); LOGGER.info("Doing login " + login); User u = userService.isLoginValid(login, password); if(u != null) { LOGGER.info("Login successful. User: " + login); return usernamePasswordAuthenticationTokenFactory.create(u); } throw new UsernameNotFoundException("Not valid login/password"); } }
AuthProviderService implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { String login = authentication.getName(); String password = authentication.getCredentials().toString(); LOGGER.info("Doing login " + login); User u = userService.isLoginValid(login, password); if(u != null) { LOGGER.info("Login successful. User: " + login); return usernamePasswordAuthenticationTokenFactory.create(u); } throw new UsernameNotFoundException("Not valid login/password"); }  }
AuthProviderService implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { String login = authentication.getName(); String password = authentication.getCredentials().toString(); LOGGER.info("Doing login " + login); User u = userService.isLoginValid(login, password); if(u != null) { LOGGER.info("Login successful. User: " + login); return usernamePasswordAuthenticationTokenFactory.create(u); } throw new UsernameNotFoundException("Not valid login/password"); }  @Override Authentication authenticate(Authentication authentication); @Override boolean supports(Class<?> aClass); }
AuthProviderService implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { String login = authentication.getName(); String password = authentication.getCredentials().toString(); LOGGER.info("Doing login " + login); User u = userService.isLoginValid(login, password); if(u != null) { LOGGER.info("Login successful. User: " + login); return usernamePasswordAuthenticationTokenFactory.create(u); } throw new UsernameNotFoundException("Not valid login/password"); }  @Override Authentication authenticate(Authentication authentication); @Override boolean supports(Class<?> aClass);  }