KeycloakUserDetailsAuthenticationProvider extends KeycloakAuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { KeycloakAuthenticationToken token = (KeycloakAuthenticationToken) super.authenticate(authentication); String username; UserDetails userDetails; if (token == null) { return null; } username = this.resolveUsername(token); userDetails = userDetailsService.loadUserByUsername(username); return new KeycloakUserDetailsAuthenticationToken(userDetails, token.getAccount(), token.getAuthorities()); }  @Override Authentication authenticate(Authentication authentication); @Required void setUserDetailsService(UserDetailsService userDetailsService);  }
@Test public void testAuthenticate() throws Exception { KeycloakUserDetailsAuthenticationToken authentication = (KeycloakUserDetailsAuthenticationToken) provider.authenticate(token); assertNotNull(authentication); assertEquals(user, authentication.getPrincipal()); }
@Test(expected = UsernameNotFoundException.class) public void testAuthenticateUserNotFound() throws Exception { when(principal.getName()).thenReturn(UNKNOWN_USERNAME); provider.authenticate(token); }