private AuthenticationProvider createProviderWhichThrows( final AuthenticationException e) { AuthenticationProvider provider = mock(AuthenticationProvider.class); when(provider.supports(any(Class.class))).thenReturn(true); when(provider.authenticate(any(Authentication.class))).thenThrow(e); return provider; }
/** * Does the actual authentication. * * <p>Subclasses should override this method, the default implementation simply delegages to the * underlying {@link AuthenticationProvider#authenticate(Authentication)}. * * <p>This method does not need to worry about handling any {@link AuthenticationException}, * they should be thrown back. */ protected Authentication doAuthenticate( Authentication authentication, HttpServletRequest request) throws AuthenticationException { return authProvider.authenticate(authentication); } }
Scanner scan = new Scanner(System.in); AuthenticationProvider authProvider = new AuthenticationProvider(); authProvider.addAuthentication("D3", "R4"); System.out.print("The co-ordinates that your after: "); String userInput = scan.nextLine(); System.out.print(authProvider.authenticate(userInput));
@Override public boolean supports(Class<?> authentication) { return authenticationProvider.supports(authentication); }
@Test public void providerWithShaPasswordEncoderWorks() throws Exception { appContext = new InMemoryXmlApplicationContext( " <authentication-manager>" + " <authentication-provider>" + " <password-encoder ref='passwordEncoder'/>" + " <user-service>" + " <user name='bob' password='{SSHA}PpuEwfdj7M1rs0C2W4ssSM2XEN/Y6S5U' authorities='ROLE_A' />" + " </user-service>" + " </authentication-provider>" + " </authentication-manager>" + " <b:bean id='passwordEncoder' class='" + LdapShaPasswordEncoder.class.getName() + "'/>"); getProvider().authenticate(bob); }
@Override public boolean supports(Class<? extends Object> authentication, HttpServletRequest request) { return authProvider.supports(authentication); }
private AuthenticationProvider createProviderWhichReturns(final Authentication a) { AuthenticationProvider provider = mock(AuthenticationProvider.class); when(provider.supports(any(Class.class))).thenReturn(true); when(provider.authenticate(any(Authentication.class))).thenReturn(a); return provider; }
@Test public void providerWithMd5PasswordEncoderWorks() throws Exception { appContext = new InMemoryXmlApplicationContext( " <authentication-manager>" + " <authentication-provider>" + " <password-encoder ref='passwordEncoder'/>" + " <user-service>" + " <user name='bob' password='12b141f35d58b8b3a46eea65e6ac179e' authorities='ROLE_A' />" + " </user-service>" + " </authentication-provider>" + " </authentication-manager>" + " <b:bean id='passwordEncoder' class='" + MessageDigestPasswordEncoder.class.getName() + "'>" + " <b:constructor-arg value='MD5'/>" + " </b:bean>"); getProvider().authenticate(bob); }
@Override public boolean apply(AuthenticationProvider input) { return input.supports(authentication); } });
@Test public void getAuthenticationWhenAuthenticationProviderBeanThenUsed() throws Exception { this.spring.register(AuthenticationProviderBeanConfig.class).autowire(); AuthenticationProvider ap = this.spring.getContext().getBean(AuthenticationProvider.class); AuthenticationManager am = this.spring.getContext().getBean(AuthenticationConfiguration.class).getAuthenticationManager(); when(ap.supports(any())).thenReturn(true); when(ap.authenticate(any())).thenReturn(TestAuthentication.authenticatedUser()); am.authenticate(new UsernamePasswordAuthenticationToken("user", "password")); }
} else { try { authed = authenticationProvider.authenticate(authentication);
@Test public void getAuthenticationWhenAuthenticationProviderAndUserDetailsBeanThenAuthenticationProviderUsed() throws Exception { this.spring.register(AuthenticationProviderBeanAndUserDetailsServiceConfig.class).autowire(); AuthenticationProvider ap = this.spring.getContext().getBean(AuthenticationProvider.class); AuthenticationManager am = this.spring.getContext().getBean(AuthenticationConfiguration.class).getAuthenticationManager(); when(ap.supports(any())).thenReturn(true); when(ap.authenticate(any())).thenReturn(TestAuthentication.authenticatedUser()); am.authenticate(new UsernamePasswordAuthenticationToken("user", "password")); }
@Test public void passwordIsBase64EncodedWhenBase64IsEnabled() throws Exception { appContext = new InMemoryXmlApplicationContext( " <authentication-manager>" + " <authentication-provider>" + " <password-encoder ref='passwordEncoder'/>" + " <user-service>" + " <user name='bob' password='ErFB811YuLOkbupl5qwXng==' authorities='ROLE_A' />" + " </user-service>" + " </authentication-provider>" + " </authentication-manager>" + " <b:bean id='passwordEncoder' class='" + MessageDigestPasswordEncoder.class.getName() + "'>" + " <b:constructor-arg value='MD5'/>" + " <b:property name='encodeHashAsBase64' value='true'/>" + " </b:bean>"); getProvider().authenticate(bob); }
if (!provider.supports(toTest)) { continue; result = provider.authenticate(authentication);
@Test public void externalUserServiceRefWorks() throws Exception { appContext = new InMemoryXmlApplicationContext( " <authentication-manager>" + " <authentication-provider user-service-ref='myUserService' />" + " </authentication-manager>" + " <user-service id='myUserService'>" + " <user name='bob' password='{noop}bobspassword' authorities='ROLE_A' />" + " </user-service>"); getProvider().authenticate(bob); }
if (!provider.supports(toTest)) { continue; result = provider.authenticate(authentication);
@Test public void providerWithBCryptPasswordEncoderWorks() throws Exception { setContext(" <authentication-provider>" + " <password-encoder hash='bcrypt'/>" + " <user-service>" + " <user name='bob' password='$2a$05$dRmjl1T05J7rvCPD2NgsHesCEJHww3pdmesUhjM3PD4m/gaEYyx/G' authorities='ROLE_A' />" + " </user-service>" + " </authentication-provider>"); getProvider().authenticate(bob); }
@Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { Iterator<AuthenticationProvider> serviceIterator = utilsService.getServiceIterator(providers, AuthenticationProvider.class); AuthenticationException lastException = null; LOGGER.debug("iterating {} authenticationProviderServices", providers.size()); while (serviceIterator.hasNext()) { AuthenticationProvider provider = serviceIterator.next(); if (provider.supports(authentication.getClass())) { LOGGER.info("attempting authentication using provider {}", provider.getClass()); try { return provider.authenticate(authentication); } catch (AuthenticationException e) { lastException = e; } } } if (lastException == null) { lastException = new ProviderNotFoundException("No AuthenticationProvider found, that supports " + authentication.getClass()); } throw lastException; }
@Test public void worksWithEmbeddedUserService() { setContext(" <authentication-provider>" + " <user-service>" + " <user name='bob' password='{noop}bobspassword' authorities='ROLE_A' />" + " </user-service>" + " </authentication-provider>"); getProvider().authenticate(bob); }
@Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (!delegate.supports(authentication.getClass())) { return null; } try { AuthenticationHolder.setAuthentication(authentication); Authentication delegatedAuth = delegate.authenticate(authentication); if (!groupsAreManagedInStudio) { return delegatedAuth; } if (delegatedAuth != null) { UserDetails userDetails = authenticationUserDetailsService.loadUserDetails(delegatedAuth); Collection<? extends GrantedAuthority> authorities = userDetails.getAuthorities(); UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken( delegatedAuth.getPrincipal(), delegatedAuth.getCredentials(), authorities); authenticationToken.setDetails(userDetails); return authenticationToken; } return null; } finally { AuthenticationHolder.clear(); } }