Skip to content

Instantly share code, notes, and snippets.

@justindav1s
Last active October 15, 2018 09:17
Show Gist options
  • Save justindav1s/5e7ab114c3d96eb8e6b179723dca2bb0 to your computer and use it in GitHub Desktop.
Save justindav1s/5e7ab114c3d96eb8e6b179723dca2bb0 to your computer and use it in GitHub Desktop.
Log log = LogFactory.getLog(MyTokenController.class);
CloseableHttpClient httpClient
= HttpClients.custom()
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.build();
HttpComponentsClientHttpRequestFactory requestFactory
= new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
log.info("new token");
String uri = baseUrl+"/auth/realms/"+realm+"/protocol/openid-connect/token";
log.info("Token URL : "+uri);
String post_body = "grant_type=client_credentials&client_id="+clientId+"&client_secret="+clientSecret;
log.info("Post body : "+post_body);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/x-www-form-urlencoded");
HttpEntity<String> request = new HttpEntity<>(post_body, headers);
ResponseEntity<String> exchange =
restTemplate.exchange(
uri,
HttpMethod.POST,
request,
String.class);
String response = exchange.getBody();
log.info("Token Response : "+response);
## Parse this to get Access and Request Tokens
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment