diff --git a/oauth2-ribbon/src/main/java/demo/RibbonClientApplication.java b/oauth2-ribbon/src/main/java/demo/RibbonClientApplication.java index 06e6c9d..be7c6a9 100644 --- a/oauth2-ribbon/src/main/java/demo/RibbonClientApplication.java +++ b/oauth2-ribbon/src/main/java/demo/RibbonClientApplication.java @@ -2,8 +2,13 @@ package demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.ribbon.RibbonClient; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.security.oauth2.client.OAuth2ClientContext; +import org.springframework.security.oauth2.client.OAuth2RestTemplate; +import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client; @Configuration @@ -12,6 +17,13 @@ import org.springframework.security.oauth2.config.annotation.web.configuration.E @EnableOAuth2Client public class RibbonClientApplication { + @LoadBalanced + @Bean + public OAuth2RestTemplate loadBalancedOauth2RestTemplate( + OAuth2ProtectedResourceDetails resource, OAuth2ClientContext context) { + return new OAuth2RestTemplate(resource, context); + } + public static void main(String[] args) { SpringApplication.run(RibbonClientApplication.class, args); } diff --git a/oauth2-ribbon/src/test/java/demo/RibbonClientApplicationTests.java b/oauth2-ribbon/src/test/java/demo/RibbonClientApplicationTests.java index 34e570d..6d3ee65 100644 --- a/oauth2-ribbon/src/test/java/demo/RibbonClientApplicationTests.java +++ b/oauth2-ribbon/src/test/java/demo/RibbonClientApplicationTests.java @@ -1,10 +1,6 @@ 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 java.net.URI; import org.junit.After; import org.junit.Rule; @@ -12,20 +8,16 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.cloud.client.loadbalancer.LoadBalanced; -import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor; -import org.springframework.cloud.netflix.ribbon.RibbonClientHttpRequestFactory; -import org.springframework.http.client.InterceptingClientHttpRequestFactory; +import org.springframework.http.HttpMethod; +import org.springframework.http.client.ClientHttpRequest; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.security.oauth2.client.OAuth2RestTemplate; import org.springframework.security.oauth2.client.resource.UserRedirectRequiredException; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; -import org.springframework.test.util.ReflectionTestUtils; -import org.springframework.web.client.RestTemplate; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; @@ -39,11 +31,6 @@ public class RibbonClientApplicationTests { @LoadBalanced private OAuth2RestTemplate oauth2RestTemplate; - @Autowired - @Qualifier("loadBalancedRestTemplate") - @LoadBalanced - private RestTemplate restTemplate; - private MockHttpServletRequest request = new MockHttpServletRequest(); @Rule @@ -55,27 +42,20 @@ public class RibbonClientApplicationTests { } @Test - public void restTemplateHasLoadBalancer() { - // Just to prove that the request factory is present... - assertThat(this.restTemplate.getRequestFactory(), - instanceOf(InterceptingClientHttpRequestFactory.class)); - assertThat(ReflectionTestUtils.getField(this.restTemplate.getRequestFactory(), "requestFactory"), - instanceOf(RibbonClientHttpRequestFactory.class)); - - } - - @Test - public void oauth2RestTemplateHasLoadBalancer() { + public void oauth2RestTemplateHasLoadBalancer() throws Exception { // Just to prove that the interceptor is present... - assertThat(new ArrayList(this.oauth2RestTemplate.getInterceptors()), - hasItem(instanceOf(LoadBalancerInterceptor.class))); + ClientHttpRequest request = oauth2RestTemplate.getRequestFactory() + .createRequest(new URI("http://nosuchservice"), HttpMethod.GET); + expected.expectMessage("No instances available for nosuchservice"); + request.execute(); } @Test public void useRestTemplate() throws Exception { // There's nowhere to get an access token so it should fail, but in a sensible way this.expected.expect(UserRedirectRequiredException.class); - RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(this.request)); + RequestContextHolder + .setRequestAttributes(new ServletRequestAttributes(this.request)); this.oauth2RestTemplate.getForEntity("http://foo/bar", String.class); }