@Test public void testAuthenticateSuccess() { User user = new User("username", "pass", "salt", "role"); Authentication authentication = mock(UsernamePasswordAuthenticationToken.class); when(authentication.getName()).thenReturn("username"); when(authentication.getCredentials()).thenReturn("pass"); when(userService.isLoginValid("username", "pass")).thenReturn(user); when(usernamePasswordAuthenticationTokenFactory.create(user)).thenReturn((UsernamePasswordAuthenticationToken) authentication); try { Authentication a = authProviderService.authenticate(authentication); assertEquals("username", a.getName()); assertEquals("pass", a.getCredentials().toString()); } catch (UsernameNotFoundException e) { fail("This code should not be executed"); } }
@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);  }