diff --git a/oauth2-ribbon/README.md b/oauth2-ribbon/README.md new file mode 100644 index 0000000..6142641 --- /dev/null +++ b/oauth2-ribbon/README.md @@ -0,0 +1 @@ +This project is a sample of a client application with OAuth2 and Ribbon \ No newline at end of file diff --git a/oauth2-ribbon/pom.xml b/oauth2-ribbon/pom.xml new file mode 100644 index 0000000..fd69d57 --- /dev/null +++ b/oauth2-ribbon/pom.xml @@ -0,0 +1,96 @@ + + + 4.0.0 + + org.springframework.cloud + spring-cloud-sample-oauth2-ribbon + 1.0.0.BUILD-SNAPSHOT + jar + + spring-cloud-sample-oauth2-ribbon + Demo project for Spring Cloud + + + org.springframework.cloud + spring-cloud-starter-parent + 1.0.0.BUILD-SNAPSHOT + + + + + UTF-8 + 1.7 + + + + + org.springframework.cloud + spring-cloud-starter-oauth2 + + + org.springframework.cloud + spring-cloud-starter-ribbon + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + maven-deploy-plugin + + true + + + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + diff --git a/oauth2-ribbon/src/main/java/demo/RibbonClientApplication.java b/oauth2-ribbon/src/main/java/demo/RibbonClientApplication.java new file mode 100644 index 0000000..abba450 --- /dev/null +++ b/oauth2-ribbon/src/main/java/demo/RibbonClientApplication.java @@ -0,0 +1,16 @@ +package demo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.cloud.netflix.ribbon.RibbonClient; +import org.springframework.context.annotation.Configuration; + +@Configuration +@EnableAutoConfiguration +@RibbonClient("foo") +public class RibbonClientApplication { + + public static void main(String[] args) { + SpringApplication.run(RibbonClientApplication.class, args); + } +} diff --git a/oauth2-ribbon/src/main/resources/application.properties b/oauth2-ribbon/src/main/resources/application.properties new file mode 100644 index 0000000..e1d17cc --- /dev/null +++ b/oauth2-ribbon/src/main/resources/application.properties @@ -0,0 +1,4 @@ +spring.oauth2.client.clientId: acme +spring.oauth2.client.accessTokenUri: https://example.com +foo.ribbon.listOfServers: example.com +# debug: \ No newline at end of file diff --git a/oauth2-ribbon/src/test/java/demo/RibbonClientApplicationTests.java b/oauth2-ribbon/src/test/java/demo/RibbonClientApplicationTests.java new file mode 100644 index 0000000..463815b --- /dev/null +++ b/oauth2-ribbon/src/test/java/demo/RibbonClientApplicationTests.java @@ -0,0 +1,55 @@ +package demo; + +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.junit.Assert.assertThat; + +import java.util.ArrayList; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor; +import org.springframework.security.oauth2.client.OAuth2RestTemplate; +import org.springframework.security.oauth2.client.resource.OAuth2AccessDeniedException; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.web.client.RestTemplate; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = RibbonClientApplication.class) +public class RibbonClientApplicationTests { + + @Autowired + private OAuth2RestTemplate oauth2RestTemplate; + + @Autowired + private RestTemplate restTemplate; + + @Rule + public ExpectedException expected = ExpectedException.none(); + + @Test + public void restTemplateHasLoadBalancer() { + // Just to prove that the interceptor is present... + assertThat(new ArrayList(restTemplate.getInterceptors()), + hasItem(instanceOf(LoadBalancerInterceptor.class))); + } + + @Test + public void oauth2RestTemplateHasLoadBalancer() { + // Just to prove that the interceptor is present... + assertThat(new ArrayList(oauth2RestTemplate.getInterceptors()), + hasItem(instanceOf(LoadBalancerInterceptor.class))); + } + + @Test + public void useRestTemplate() throws Exception { + // There's nowhere to get an access token so it should fail, but in a sensible way + expected.expect(OAuth2AccessDeniedException.class); + oauth2RestTemplate.getForEntity("http://foo/bar", String.class); + } + +} diff --git a/pom.xml b/pom.xml index 48ab8d0..913d055 100644 --- a/pom.xml +++ b/pom.xml @@ -23,6 +23,7 @@ eureka-noweb netflix-sidecar noweb + oauth2-ribbon hystrix turbine zuul-proxy