@Test public void testAuthenticates() { when(mockUserDetailService.login(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, null, null, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME)).thenReturn(user); VistaAuthenticationToken token = new VistaAuthenticationToken(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME); Authentication result = provider.authenticate(token); if (!(result instanceof VistaAuthenticationToken)) { fail("Should have returned instance of VistaAuthenticationToken"); } assertNotSame(token, result); VistaAuthenticationToken castResult = (VistaAuthenticationToken) result; assertTrue(VistaUserDetails.class.isAssignableFrom(castResult.getPrincipal().getClass())); assertEquals(TEST_VISTA_ID, castResult.getVistaId()); assertEquals(TEST_ACCESS, castResult.getAccessCode()); assertEquals(TEST_VERIFY, castResult.getVerifyCode()); assertEquals(TEST_REMOTE_ADDRESS, castResult.getRemoteAddress()); assertEquals(TEST_REMOTE_HOSTNAME, castResult.getRemoteHostName()); assertEquals("ROLE_ONE", new ArrayList<GrantedAuthority>(castResult.getAuthorities()).get(0).getAuthority()); assertEquals("ROLE_TWO", new ArrayList<GrantedAuthority>(castResult.getAuthorities()).get(1).getAuthority()); assertEquals(TEST_REMOTE_HOSTNAME + "(" + TEST_REMOTE_ADDRESS + ")", castResult.getDetails()); verify(mockUserDetailService).login(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, null, null, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME); }
public Authentication authenticate(Authentication authentication) throws AuthenticationException { Assert.isInstanceOf(VistaAuthenticationToken.class, authentication, messages.getMessage("VistaAuthenticationProvider.onlySupports", "Only VistaAuthenticationToken is supported")); VistaAuthenticationToken auth = (VistaAuthenticationToken) authentication; String username = auth.getName(); boolean cacheWasUsed = true; VistaUserDetails user = (VistaUserDetails) this.userCache.getUserFromCache(username); if (user == null) { cacheWasUsed = false; user = retrieveUser(auth); Assert.notNull(user, "retrieveUser returned null - a violation of the interface contract"); } if (!user.isAccountNonLocked()) { throw new LockedException(messages.getMessage("VistaAuthenticationProvider.locked", "User account is locked")); } if (!user.isEnabled()) { throw new DisabledException(messages.getMessage("VistaAuthenticationProvider.disabled", "User is disabled")); } if (!user.isAccountNonExpired()) { throw new AccountExpiredException(messages.getMessage("VistaAuthenticationProvider.expired", "User account has expired")); } try { additionalAuthenticationChecks(user, auth); } catch (AuthenticationException exception) { if (cacheWasUsed) { cacheWasUsed = false; user = retrieveUser(auth); additionalAuthenticationChecks(user, auth); } else { throw exception; } } if (!user.isCredentialsNonExpired()) { throw new CredentialsExpiredException(messages.getMessage( "VistaAuthenticationProvider.credentialsExpired", "User credentials have expired")); } if (!cacheWasUsed) { this.userCache.putUserInCache(user); } return createSuccessAuthentication(user, auth); }
VistaAuthenticationProvider implements AuthenticationProvider, MessageSourceAware, InitializingBean { public Authentication authenticate(Authentication authentication) throws AuthenticationException { Assert.isInstanceOf(VistaAuthenticationToken.class, authentication, messages.getMessage("VistaAuthenticationProvider.onlySupports", "Only VistaAuthenticationToken is supported")); VistaAuthenticationToken auth = (VistaAuthenticationToken) authentication; String username = auth.getName(); boolean cacheWasUsed = true; VistaUserDetails user = (VistaUserDetails) this.userCache.getUserFromCache(username); if (user == null) { cacheWasUsed = false; user = retrieveUser(auth); Assert.notNull(user, "retrieveUser returned null - a violation of the interface contract"); } if (!user.isAccountNonLocked()) { throw new LockedException(messages.getMessage("VistaAuthenticationProvider.locked", "User account is locked")); } if (!user.isEnabled()) { throw new DisabledException(messages.getMessage("VistaAuthenticationProvider.disabled", "User is disabled")); } if (!user.isAccountNonExpired()) { throw new AccountExpiredException(messages.getMessage("VistaAuthenticationProvider.expired", "User account has expired")); } try { additionalAuthenticationChecks(user, auth); } catch (AuthenticationException exception) { if (cacheWasUsed) { cacheWasUsed = false; user = retrieveUser(auth); additionalAuthenticationChecks(user, auth); } else { throw exception; } } if (!user.isCredentialsNonExpired()) { throw new CredentialsExpiredException(messages.getMessage( "VistaAuthenticationProvider.credentialsExpired", "User credentials have expired")); } if (!cacheWasUsed) { this.userCache.putUserInCache(user); } return createSuccessAuthentication(user, auth); } }
VistaAuthenticationProvider implements AuthenticationProvider, MessageSourceAware, InitializingBean { public Authentication authenticate(Authentication authentication) throws AuthenticationException { Assert.isInstanceOf(VistaAuthenticationToken.class, authentication, messages.getMessage("VistaAuthenticationProvider.onlySupports", "Only VistaAuthenticationToken is supported")); VistaAuthenticationToken auth = (VistaAuthenticationToken) authentication; String username = auth.getName(); boolean cacheWasUsed = true; VistaUserDetails user = (VistaUserDetails) this.userCache.getUserFromCache(username); if (user == null) { cacheWasUsed = false; user = retrieveUser(auth); Assert.notNull(user, "retrieveUser returned null - a violation of the interface contract"); } if (!user.isAccountNonLocked()) { throw new LockedException(messages.getMessage("VistaAuthenticationProvider.locked", "User account is locked")); } if (!user.isEnabled()) { throw new DisabledException(messages.getMessage("VistaAuthenticationProvider.disabled", "User is disabled")); } if (!user.isAccountNonExpired()) { throw new AccountExpiredException(messages.getMessage("VistaAuthenticationProvider.expired", "User account has expired")); } try { additionalAuthenticationChecks(user, auth); } catch (AuthenticationException exception) { if (cacheWasUsed) { cacheWasUsed = false; user = retrieveUser(auth); additionalAuthenticationChecks(user, auth); } else { throw exception; } } if (!user.isCredentialsNonExpired()) { throw new CredentialsExpiredException(messages.getMessage( "VistaAuthenticationProvider.credentialsExpired", "User credentials have expired")); } if (!cacheWasUsed) { this.userCache.putUserInCache(user); } return createSuccessAuthentication(user, auth); }  }
VistaAuthenticationProvider implements AuthenticationProvider, MessageSourceAware, InitializingBean { public Authentication authenticate(Authentication authentication) throws AuthenticationException { Assert.isInstanceOf(VistaAuthenticationToken.class, authentication, messages.getMessage("VistaAuthenticationProvider.onlySupports", "Only VistaAuthenticationToken is supported")); VistaAuthenticationToken auth = (VistaAuthenticationToken) authentication; String username = auth.getName(); boolean cacheWasUsed = true; VistaUserDetails user = (VistaUserDetails) this.userCache.getUserFromCache(username); if (user == null) { cacheWasUsed = false; user = retrieveUser(auth); Assert.notNull(user, "retrieveUser returned null - a violation of the interface contract"); } if (!user.isAccountNonLocked()) { throw new LockedException(messages.getMessage("VistaAuthenticationProvider.locked", "User account is locked")); } if (!user.isEnabled()) { throw new DisabledException(messages.getMessage("VistaAuthenticationProvider.disabled", "User is disabled")); } if (!user.isAccountNonExpired()) { throw new AccountExpiredException(messages.getMessage("VistaAuthenticationProvider.expired", "User account has expired")); } try { additionalAuthenticationChecks(user, auth); } catch (AuthenticationException exception) { if (cacheWasUsed) { cacheWasUsed = false; user = retrieveUser(auth); additionalAuthenticationChecks(user, auth); } else { throw exception; } } if (!user.isCredentialsNonExpired()) { throw new CredentialsExpiredException(messages.getMessage( "VistaAuthenticationProvider.credentialsExpired", "User credentials have expired")); } if (!cacheWasUsed) { this.userCache.putUserInCache(user); } return createSuccessAuthentication(user, auth); }  void afterPropertiesSet(); Authentication authenticate(Authentication authentication); void setMessageSource(MessageSource messageSource); void setUserCache(UserCache userCache); boolean supports(Class authentication); VistaUserDetailsService getUserDetailsService(); @Required void setUserDetailsService(VistaUserDetailsService userDetailsService); UserCache getUserCache(); }
VistaAuthenticationProvider implements AuthenticationProvider, MessageSourceAware, InitializingBean { public Authentication authenticate(Authentication authentication) throws AuthenticationException { Assert.isInstanceOf(VistaAuthenticationToken.class, authentication, messages.getMessage("VistaAuthenticationProvider.onlySupports", "Only VistaAuthenticationToken is supported")); VistaAuthenticationToken auth = (VistaAuthenticationToken) authentication; String username = auth.getName(); boolean cacheWasUsed = true; VistaUserDetails user = (VistaUserDetails) this.userCache.getUserFromCache(username); if (user == null) { cacheWasUsed = false; user = retrieveUser(auth); Assert.notNull(user, "retrieveUser returned null - a violation of the interface contract"); } if (!user.isAccountNonLocked()) { throw new LockedException(messages.getMessage("VistaAuthenticationProvider.locked", "User account is locked")); } if (!user.isEnabled()) { throw new DisabledException(messages.getMessage("VistaAuthenticationProvider.disabled", "User is disabled")); } if (!user.isAccountNonExpired()) { throw new AccountExpiredException(messages.getMessage("VistaAuthenticationProvider.expired", "User account has expired")); } try { additionalAuthenticationChecks(user, auth); } catch (AuthenticationException exception) { if (cacheWasUsed) { cacheWasUsed = false; user = retrieveUser(auth); additionalAuthenticationChecks(user, auth); } else { throw exception; } } if (!user.isCredentialsNonExpired()) { throw new CredentialsExpiredException(messages.getMessage( "VistaAuthenticationProvider.credentialsExpired", "User credentials have expired")); } if (!cacheWasUsed) { this.userCache.putUserInCache(user); } return createSuccessAuthentication(user, auth); }  void afterPropertiesSet(); Authentication authenticate(Authentication authentication); void setMessageSource(MessageSource messageSource); void setUserCache(UserCache userCache); boolean supports(Class authentication); VistaUserDetailsService getUserDetailsService(); @Required void setUserDetailsService(VistaUserDetailsService userDetailsService); UserCache getUserCache();  }