Skip to content

Instantly share code, notes, and snippets.

@ringoluo
Last active August 29, 2015 14:13
Show Gist options
  • Save ringoluo/b6fa8e18c7c9c659b0a1 to your computer and use it in GitHub Desktop.
Save ringoluo/b6fa8e18c7c9c659b0a1 to your computer and use it in GitHub Desktop.
SpringTemplate OAuth2 Client example
public class OAuthClientTest {
@Test
public void test() {
ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
resource.setClientAuthenticationScheme(AuthenticationScheme.header);
resource.setId("restservice");
resource.setClientId("clientapp");
resource.setClientSecret("123456");
resource.setAccessTokenUri("http://localhost:8080/oauth/token");
resource.setScope(Lists.newArrayList("read", "write"));
resource.setUsername("luochun");
resource.setPassword("secret1");
OAuth2RestTemplate template = new OAuth2RestTemplate(resource);
String reply = template.getForObject("http://localhost:8080/api/check", String.class);
assertThat(reply, is("ok"));
OAuth2AccessToken token = template.getAccessToken();
String tokenValue = token.getValue();
OAuth2ProtectedResourceDetails resource2 = new BaseOAuth2ProtectedResourceDetails();
OAuth2ClientContext context = new DefaultOAuth2ClientContext();
OAuth2AccessToken token2 = new DefaultOAuth2AccessToken(tokenValue);
context.setAccessToken(token2);
OAuth2RestTemplate template2 = new OAuth2RestTemplate(resource2, context);
String reply2 = template2.getForObject("http://localhost:8080/api/check", String.class);
assertThat(reply2, is("ok"));
}
@Test
public void test2() throws ClientProtocolException, IOException {
HttpClient client = HttpClientBuilder.create().build();
// unauthorized
HttpGet get = new HttpGet("http://localhost:8080/api/check");
HttpResponse getRes = client.execute(get);
String getReply = EntityUtils.toString(getRes.getEntity());
System.out.println(getReply);
// acquire token
String address = "http://localhost:8080/oauth/token";
HttpPost post = new HttpPost(address);
String encoding = Base64.encodeBase64String("clientapp:".getBytes());
post.setHeader("Authorization", "Basic " + encoding);
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("username", "luochun"));
pairs.add(new BasicNameValuePair("password", "secret1"));
pairs.add(new BasicNameValuePair("grant_type", "password"));
pairs.add(new BasicNameValuePair("scope", "read write"));
pairs.add(new BasicNameValuePair("client_id", "clientapp"));
pairs.add(new BasicNameValuePair("client_secret", "123456"));
post.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = client.execute(post);
String reply = EntityUtils.toString(response.getEntity());
ObjectMapper mapper = new ObjectMapper();
HashMap<String, String> map = mapper.readValue(reply, HashMap.class);
String token = map.get("access_token");
System.out.println(token);
HttpGet get2 = new HttpGet("http://localhost:8080/api/check");
get2.setHeader("Authorization", "Bearer " + token);
HttpResponse getRes2 = client.execute(get2);
String getReply2 = EntityUtils.toString(getRes2.getEntity());
System.out.println(getReply2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment