DirectAccessGrantAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { String username = resolveUsername(authentication.getPrincipal()); String password = (String) authentication.getCredentials(); RefreshableKeycloakSecurityContext context; KeycloakAuthenticationToken token; Collection<? extends GrantedAuthority> authorities; try { context = directAccessGrantService.login(username, password); authorities = KeycloakSpringAdapterUtils.createGrantedAuthorities(context, grantedAuthoritiesMapper); token = new KeycloakAuthenticationToken(KeycloakSpringAdapterUtils.createAccount(keycloakDeployment, context), authorities); } catch (VerificationException e) { throw new BadCredentialsException("Unable to validate token", e); } catch (Exception e) { throw new AuthenticationServiceException("Error authenticating with Keycloak server", e); } return token; }  @Override Authentication authenticate(Authentication authentication); @Override boolean supports(Class<?> authentication); @Required void setKeycloakDeployment(KeycloakDeployment keycloakDeployment); @Required void setDirectAccessGrantService(DirectAccessGrantService directAccessGrantService); void setGrantedAuthoritiesMapper(GrantedAuthoritiesMapper grantedAuthoritiesMapper);  }
@Test(expected = AuthenticationServiceException.class) public void testAuthenticateBadCredentials() throws Exception { directAccessGrantAuthenticationProvider.authenticate(new DirectAccessGrantToken("foo", "bar")); }
@Ignore @Test public void testAuthenticate() throws Exception { DirectAccessGrantToken token = new DirectAccessGrantToken(AppConfig.KNOWN_USERNAME, AppConfig.KNOWN_PASSWORD); Authentication authenication = directAccessGrantAuthenticationProvider.authenticate(token); assertNotNull(authenication); assertTrue(authenication.isAuthenticated()); }