diff --git a/spring-security-rest-template/.classpath b/spring-security-rest-template/.classpath index 5dea193677..ca257cf1f9 100644 --- a/spring-security-rest-template/.classpath +++ b/spring-security-rest-template/.classpath @@ -16,6 +16,12 @@ + + + + + + diff --git a/spring-security-rest-template/.settings/org.eclipse.wst.common.component b/spring-security-rest-template/.settings/org.eclipse.wst.common.component index d81a71a7eb..7092dce0b6 100644 --- a/spring-security-rest-template/.settings/org.eclipse.wst.common.component +++ b/spring-security-rest-template/.settings/org.eclipse.wst.common.component @@ -4,6 +4,7 @@ + diff --git a/spring-security-rest-template/pom.xml b/spring-security-rest-template/pom.xml index 51fb1db96d..768b791119 100644 --- a/spring-security-rest-template/pom.xml +++ b/spring-security-rest-template/pom.xml @@ -1,242 +1,278 @@ - - 4.0.0 - org.baeldung - spring-security-rest-template - 0.1-SNAPSHOT + + 4.0.0 + org.baeldung + spring-security-rest-template + 0.1-SNAPSHOT - spring-security-rest-template - war + spring-security-rest-template + war - + - + - - org.springframework.security - spring-security-web - ${org.springframework.security.version} - - - org.springframework.security - spring-security-config - ${org.springframework.security.version} - + + org.springframework.security + spring-security-web + ${org.springframework.security.version} + + + org.springframework.security + spring-security-config + ${org.springframework.security.version} + - + - - org.springframework - spring-core - ${org.springframework.version} - - - org.springframework - spring-context - ${org.springframework.version} - - - org.springframework - spring-jdbc - ${org.springframework.version} - - - org.springframework - spring-beans - ${org.springframework.version} - - - org.springframework - spring-aop - ${org.springframework.version} - - - org.springframework - spring-tx - ${org.springframework.version} - - - org.springframework - spring-expression - ${org.springframework.version} - + + org.springframework + spring-core + ${org.springframework.version} + + + org.springframework + spring-context + ${org.springframework.version} + + + org.springframework + spring-jdbc + ${org.springframework.version} + + + org.springframework + spring-beans + ${org.springframework.version} + + + org.springframework + spring-aop + ${org.springframework.version} + + + org.springframework + spring-tx + ${org.springframework.version} + + + org.springframework + spring-expression + ${org.springframework.version} + - - org.springframework - spring-web - ${org.springframework.version} - - - org.springframework - spring-webmvc - ${org.springframework.version} - - - - org.springframework - spring-oxm - ${org.springframework.version} - + + org.springframework + spring-web + ${org.springframework.version} + + + org.springframework + spring-webmvc + ${org.springframework.version} + - + + org.springframework + spring-oxm + ${org.springframework.version} + - - com.fasterxml.jackson.core - jackson-databind - 2.2.2 - + - + + com.fasterxml.jackson.core + jackson-databind + 2.2.2 + - - javax.servlet - javax.servlet-api - 3.0.1 - provided - + - - javax.servlet - jstl - 1.2 - runtime - + + org.apache.httpcomponents + httpcore + ${httpcore.version} + + + commons-logging + commons-logging + + + - + + org.apache.httpcomponents + httpclient + ${httpclient.version} + + + commons-logging + commons-logging + + + - - com.google.guava - guava - ${guava.version} - + - + + javax.servlet + javax.servlet-api + 3.0.1 + provided + - - junit - junit-dep - ${junit.version} - test - + + javax.servlet + jstl + 1.2 + runtime + - - org.hamcrest - hamcrest-core - ${org.hamcrest.version} - test - - - org.hamcrest - hamcrest-library - ${org.hamcrest.version} - test - + - - org.mockito - mockito-core - ${mockito.version} - test - + + com.google.guava + guava + ${guava.version} + - + - - spring-security-rest-template - - - src/main/resources - true - - + + org.springframework + spring-test + ${org.springframework.version} + test + - + + junit + junit-dep + ${junit.version} + test + - - org.apache.maven.plugins - maven-compiler-plugin - 3.1 - - 1.7 - 1.7 - true - source - - + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - - - - - - - - - + + org.mockito + mockito-core + ${mockito.version} + test + - - org.codehaus.cargo - cargo-maven2-plugin - ${cargo-maven2-plugin.version} - - true - - jetty8x - embedded - - - - - - - 8082 - - - - + - + + spring-security-rest-template + + + src/main/resources + true + + - + - - - 3.2.3.RELEASE - 3.1.4.RELEASE + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.7 + 1.7 + true + source + + - - 4.2.2.Final - 5.1.25 + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + + + + + + + - - 1.7.5 - 1.0.11 + + org.codehaus.cargo + cargo-maven2-plugin + ${cargo-maven2-plugin.version} + + true + + jetty8x + embedded + + + + + + + 8082 + + + + - - 5.0.1.Final + - - 14.0.1 - 3.1 + - - 1.3 - 4.11 - 1.9.5 + + + 3.2.3.RELEASE + 3.1.4.RELEASE - 4.2.4 - 4.2.5 + + 4.2.3.Final + 5.1.25 - 1.8.1 - 1.8.9 + + 4.2.4 + 4.2.5 - - 1.4.2 - 2.15 - + + 1.7.5 + 1.0.11 + + + 5.0.1.Final + + + 14.0.1 + 3.1 + + + 1.3 + 4.11 + 1.9.5 + + 4.2.4 + 4.2.5 + + 1.8.1 + 1.8.9 + + + 1.4.3 + 2.15 + \ No newline at end of file diff --git a/spring-security-rest-template/src/main/java/org/baeldung/client/HttpComponentsClientHttpRequestFactoryBasicAuth.java b/spring-security-rest-template/src/main/java/org/baeldung/client/HttpComponentsClientHttpRequestFactoryBasicAuth.java new file mode 100644 index 0000000000..5f808413ce --- /dev/null +++ b/spring-security-rest-template/src/main/java/org/baeldung/client/HttpComponentsClientHttpRequestFactoryBasicAuth.java @@ -0,0 +1,42 @@ +package org.baeldung.client; + +import java.net.URI; + +import org.apache.http.HttpHost; +import org.apache.http.client.AuthCache; +import org.apache.http.client.protocol.ClientContext; +import org.apache.http.impl.auth.BasicScheme; +import org.apache.http.impl.client.BasicAuthCache; +import org.apache.http.protocol.BasicHttpContext; +import org.apache.http.protocol.HttpContext; +import org.springframework.http.HttpMethod; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; + +public class HttpComponentsClientHttpRequestFactoryBasicAuth extends HttpComponentsClientHttpRequestFactory { + HttpHost host; + + public HttpComponentsClientHttpRequestFactoryBasicAuth(final HttpHost host) { + super(); + this.host = host; + } + + // + + @Override + protected HttpContext createHttpContext(final HttpMethod httpMethod, final URI uri) { + return createHttpContext(); + } + + private HttpContext createHttpContext() { + // Create AuthCache instance + final AuthCache authCache = new BasicAuthCache(); + // Generate BASIC scheme object and add it to the local auth cache + final BasicScheme basicAuth = new BasicScheme(); + authCache.put(host, basicAuth); + // Add AuthCache to the execution context + final BasicHttpContext localcontext = new BasicHttpContext(); + localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache); + return localcontext; + } + +} \ No newline at end of file diff --git a/spring-security-rest-template/src/main/java/org/baeldung/client/RestTemplateFactory.java b/spring-security-rest-template/src/main/java/org/baeldung/client/RestTemplateFactory.java new file mode 100644 index 0000000000..3401d14d85 --- /dev/null +++ b/spring-security-rest-template/src/main/java/org/baeldung/client/RestTemplateFactory.java @@ -0,0 +1,40 @@ +package org.baeldung.client; + +import org.apache.http.HttpHost; +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; + +@Component +public class RestTemplateFactory implements FactoryBean, InitializingBean { + private RestTemplate restTemplate; + + public RestTemplateFactory() { + super(); + } + + // API + + @Override + public RestTemplate getObject() { + return restTemplate; + } + + @Override + public Class getObjectType() { + return RestTemplate.class; + } + + @Override + public boolean isSingleton() { + return true; + } + + @Override + public void afterPropertiesSet() { + final HttpHost host = new HttpHost("localhost", 8080, "http"); + restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactoryBasicAuth(host)); + } + +} \ No newline at end of file diff --git a/spring-security-rest-template/src/main/java/org/baeldung/spring/ClientConfig.java b/spring-security-rest-template/src/main/java/org/baeldung/spring/ClientConfig.java new file mode 100644 index 0000000000..a0c90b51ae --- /dev/null +++ b/spring-security-rest-template/src/main/java/org/baeldung/spring/ClientConfig.java @@ -0,0 +1,16 @@ +package org.baeldung.spring; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan("org.baeldung.client") +public class ClientConfig { + + public ClientConfig() { + super(); + } + + // beans + +} \ No newline at end of file diff --git a/spring-security-rest-template/src/test/java/org/baeldung/client/ClientLiveTest.java b/spring-security-rest-template/src/test/java/org/baeldung/client/ClientLiveTest.java new file mode 100644 index 0000000000..be215bf959 --- /dev/null +++ b/spring-security-rest-template/src/test/java/org/baeldung/client/ClientLiveTest.java @@ -0,0 +1,26 @@ +package org.baeldung.client; + +import org.baeldung.spring.ClientConfig; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; +import org.springframework.web.client.RestTemplate; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { ClientConfig.class }, loader = AnnotationConfigContextLoader.class) +public class ClientLiveTest { + + @Autowired + private RestTemplate restTemplate; + + // tests + + @Test + public final void whenSecuredRestApiIsConsumed_then200OK() { + System.out.println(); + } + +}