package net.snrapps.sdsm; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.json.JSONObject; import org.keycloak.OAuth2Constants; import org.keycloak.adapters.HttpClientBuilder; import org.keycloak.constants.ServiceUrlConstants; import org.keycloak.representations.AccessTokenResponse; import org.keycloak.util.JsonSerialization; import org.keycloak.util.KeycloakUriBuilder; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; public class KeyCloakAdminAdapter { // @Value("${local.server.port}") private int port = 8080; public static void main(String[] args) { // TODO Auto-generated method stub KeyCloakAdminAdapter keyCloakAdminAdapter = new KeyCloakAdminAdapter(); AccessTokenResponse accessTokenResponse = keyCloakAdminAdapter .getToken(); System.out.println("acc-----" + accessTokenResponse.getToken()); System.out.println("getTokenType-----" + accessTokenResponse.getTokenType()); keyCloakAdminAdapter.createUserAsRest1(accessTokenResponse); } public AccessTokenResponse getToken() { HttpClient client = new HttpClientBuilder().disableTrustManager() .build(); AccessTokenResponse tokenResponse = null; try { HttpPost post = new HttpPost(KeycloakUriBuilder .fromUri("http://localhost:8080/auth") .path(ServiceUrlConstants.TOKEN_PATH).build("test-realm")); System.out.println("<<<<<<<>>>>>>>" + post.getURI()); List formparams = new ArrayList(); formparams.add(new BasicNameValuePair("username", "test-realm-user01")); formparams.add(new BasicNameValuePair("password", "password")); formparams.add(new BasicNameValuePair(OAuth2Constants.GRANT_TYPE, "password")); // if (isPublic()) { // if client is public access type formparams.add(new BasicNameValuePair(OAuth2Constants.CLIENT_ID, "test-realm-client-id")); UrlEncodedFormEntity form = new UrlEncodedFormEntity(formparams, "UTF-8"); post.setEntity(form); HttpResponse response = client.execute(post); int status = response.getStatusLine().getStatusCode(); HttpEntity entity = response.getEntity(); if (status != 200) { throw new IOException("Bad status: " + status); } if (entity == null) { throw new IOException("No Entity"); } InputStream is = entity.getContent(); try { tokenResponse = JsonSerialization.readValue(is, AccessTokenResponse.class); } finally { try { is.close(); } catch (IOException ignored) { } } } catch (Exception exp) { exp.printStackTrace(); } finally { client.getConnectionManager().shutdown(); } return tokenResponse; } public void createUserAsRest(AccessTokenResponse accessTokenResponse) { // org.springframework.http.HttpEntity requestEntity = new // org.springframework.http.HttpEntity(getHeaders(accessTokenResponse.getToken())); MultiValueMap formData = new LinkedMultiValueMap(); formData.add("email", "hjarsh@sd.com"); formData.add("firstName", "ss"); formData.add("lastName", "ewsdsd"); formData.add("username", "username02"); formData.add("enabled", "true"); org.springframework.http.HttpEntity request = new org.springframework.http.HttpEntity( formData, getHeaders(accessTokenResponse.getToken())); RestTemplate restTemplate = new RestTemplate(); String urlForCreateUser = "http://localhost:8080/auth/admin/realms/test-realm/users"; try { restTemplate.postForObject(urlForCreateUser, request, String.class); } catch (Exception exp) { exp.printStackTrace(); } } /* * public List getRealmRoles(HttpServletRequest request, * AccessTokenResponse res) throws Failure { * * HttpClient client = new DefaultHttpClient(); try { HttpGet get = new * HttpGet(getBaseUrl(request) + "/auth/admin/realms/demo/roles"); * get.addHeader("Authorization", "Bearer " + res.getToken()); try { * HttpResponse response = client.execute(get); if * (response.getStatusLine().getStatusCode() != 200) { throw new * Failure(response.getStatusLine().getStatusCode()); } HttpEntity entity = * response.getEntity(); InputStream is = entity.getContent(); try { return * JsonSerialization.readValue(is, TypedList.class); } finally { is.close(); * } } catch (IOException e) { throw new RuntimeException(e); } } finally { * client.getConnectionManager().shutdown(); } } */ public void createUserAsRest1(AccessTokenResponse accessTokenResponse) { try { List formData = new ArrayList(); BasicNameValuePair baP = new BasicNameValuePair("email", "hjawwwrsh@sd.com"); formData.add(baP); baP = new BasicNameValuePair("firstName", "swwws"); formData.add(baP); baP = new BasicNameValuePair("lastName", "ewsdsd"); formData.add(baP); baP = new BasicNameValuePair("username", "username03"); formData.add(baP); baP = new BasicNameValuePair("enabled", "true"); formData.add(baP); String urlForCreateUser = "http://localhost:8080/auth/admin/realms/test-realm/users"; HttpPost httpPost = new HttpPost(urlForCreateUser); httpPost.addHeader("Authentication", "Bearer " + accessTokenResponse.getToken()); httpPost.addHeader(OAuth2Constants.CLIENT_ID, "test-realm-client-id"); httpPost.setEntity(new UrlEncodedFormEntity(formData, HTTP.UTF_8)); HttpClient httpClient = new HttpClientBuilder().disableTrustManager() .build(); HttpResponse response = httpClient.execute(httpPost); int status = response.getStatusLine().getStatusCode(); System.out.println("sdsdsds"+status); } catch(Exception e) { e.printStackTrace(); } //System.out.println("88888888888" + loginResponse.getBody()); } /** * Obtain headers for Keycloack authentication. * * @param token * @return */ public HttpHeaders getHeaders(String token) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.add("Authorization", "Bearer " + new String(token)); return headers; } }