Skip to content

Commit fab2df5

Browse files
committed
[BAEL-13600] - Updated HttpClient with SSL Header article
1 parent ad3a6c4 commit fab2df5

25 files changed

+712
-20
lines changed

httpclient-simple/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
77

88
### Relevant Articles:
99

10-
- [HttpClient 4 – Get the Status Code](http://www.baeldung.com/httpclient-status-code)
10+
- [HttpClient 4 – Get the Status Code](http://www.baeldung.com/httpclient-status-code)
11+
- [HttpClient with SSL](http://www.baeldung.com/httpclient-ssl)

httpclient-simple/pom.xml

Lines changed: 191 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,111 @@
55
<artifactId>httpclient-simple</artifactId>
66
<version>0.1-SNAPSHOT</version>
77
<name>httpclient-simple</name>
8+
<packaging>war</packaging>
89

910
<parent>
1011
<groupId>com.baeldung</groupId>
11-
<artifactId>parent-java</artifactId>
12+
<artifactId>parent-spring-5</artifactId>
1213
<version>0.0.1-SNAPSHOT</version>
13-
<relativePath>../parent-java</relativePath>
14+
<relativePath>../parent-spring-5</relativePath>
1415
</parent>
1516

1617
<dependencies>
18+
19+
<!-- Spring Security -->
20+
21+
<dependency>
22+
<groupId>org.springframework.security</groupId>
23+
<artifactId>spring-security-web</artifactId>
24+
<version>${spring.version}</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.security</groupId>
28+
<artifactId>spring-security-config</artifactId>
29+
<version>${spring.version}</version>
30+
</dependency>
31+
32+
<!-- Spring -->
33+
34+
<dependency>
35+
<groupId>org.springframework</groupId>
36+
<artifactId>spring-core</artifactId>
37+
<version>${spring.version}</version>
38+
<exclusions>
39+
<exclusion>
40+
<artifactId>commons-logging</artifactId>
41+
<groupId>commons-logging</groupId>
42+
</exclusion>
43+
</exclusions>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.springframework</groupId>
47+
<artifactId>spring-context</artifactId>
48+
<version>${spring.version}</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.springframework</groupId>
52+
<artifactId>spring-jdbc</artifactId>
53+
<version>${spring.version}</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.springframework</groupId>
57+
<artifactId>spring-beans</artifactId>
58+
<version>${spring.version}</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.springframework</groupId>
62+
<artifactId>spring-aop</artifactId>
63+
<version>${spring.version}</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.springframework</groupId>
67+
<artifactId>spring-tx</artifactId>
68+
<version>${spring.version}</version>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.springframework</groupId>
72+
<artifactId>spring-expression</artifactId>
73+
<version>${spring.version}</version>
74+
</dependency>
75+
76+
<dependency>
77+
<groupId>org.springframework</groupId>
78+
<artifactId>spring-web</artifactId>
79+
<version>${spring.version}</version>
80+
</dependency>
81+
<dependency>
82+
<groupId>org.springframework</groupId>
83+
<artifactId>spring-webmvc</artifactId>
84+
<version>${spring.version}</version>
85+
</dependency>
86+
87+
<dependency>
88+
<groupId>org.springframework</groupId>
89+
<artifactId>spring-oxm</artifactId>
90+
<version>${spring.version}</version>
91+
</dependency>
92+
93+
<!-- marshalling -->
94+
95+
<dependency>
96+
<groupId>com.fasterxml.jackson.core</groupId>
97+
<artifactId>jackson-databind</artifactId>
98+
<version>${jackson.version}</version>
99+
</dependency>
100+
101+
<dependency>
102+
<groupId>org.apache.httpcomponents</groupId>
103+
<artifactId>httpcore</artifactId>
104+
<version>${httpcore.version}</version>
105+
<exclusions>
106+
<exclusion>
107+
<artifactId>commons-logging</artifactId>
108+
<groupId>commons-logging</groupId>
109+
</exclusion>
110+
</exclusions>
111+
</dependency>
112+
17113
<!-- utils -->
18114
<dependency>
19115
<groupId>org.apache.commons</groupId>
@@ -70,23 +166,105 @@
70166
<version>${wiremock.version}</version>
71167
<scope>test</scope>
72168
</dependency>
169+
170+
<!-- web -->
171+
172+
<dependency>
173+
<groupId>javax.servlet</groupId>
174+
<artifactId>javax.servlet-api</artifactId>
175+
<version>${javax.servlet.version}</version>
176+
<scope>provided</scope>
177+
</dependency>
178+
179+
<dependency>
180+
<groupId>javax.servlet</groupId>
181+
<artifactId>jstl</artifactId>
182+
<version>${jstl.version}</version>
183+
<scope>runtime</scope>
184+
</dependency>
185+
186+
<!-- util -->
187+
188+
<dependency>
189+
<groupId>com.google.guava</groupId>
190+
<artifactId>guava</artifactId>
191+
<version>${guava.version}</version>
192+
</dependency>
193+
194+
<!-- test scoped -->
195+
196+
<dependency>
197+
<groupId>org.springframework</groupId>
198+
<artifactId>spring-test</artifactId>
199+
<version>${spring.version}</version>
200+
<scope>test</scope>
201+
</dependency>
73202
</dependencies>
74203

75204
<build>
76-
<finalName>httpclient</finalName>
205+
<finalName>httpclient-simple</finalName>
77206
<resources>
78207
<resource>
79208
<directory>src/main/resources</directory>
80209
<filtering>true</filtering>
81210
</resource>
82211
</resources>
212+
213+
<plugins>
214+
<plugin>
215+
<groupId>org.apache.maven.plugins</groupId>
216+
<artifactId>maven-war-plugin</artifactId>
217+
<version>${maven-war-plugin.version}</version>
218+
</plugin>
219+
<plugin>
220+
<groupId>org.codehaus.cargo</groupId>
221+
<artifactId>cargo-maven2-plugin</artifactId>
222+
<version>${cargo-maven2-plugin.version}</version>
223+
<configuration>
224+
<wait>true</wait>
225+
<container>
226+
<containerId>jetty8x</containerId>
227+
<type>embedded</type>
228+
<systemProperties>
229+
<!-- <provPersistenceTarget>cargo</provPersistenceTarget> -->
230+
</systemProperties>
231+
</container>
232+
<configuration>
233+
<properties>
234+
<cargo.servlet.port>8082</cargo.servlet.port>
235+
</properties>
236+
</configuration>
237+
</configuration>
238+
</plugin>
239+
</plugins>
83240
</build>
84241

85242
<profiles>
86243
<profile>
87244
<id>live</id>
88245
<build>
89246
<plugins>
247+
<plugin>
248+
<groupId>org.codehaus.cargo</groupId>
249+
<artifactId>cargo-maven2-plugin</artifactId>
250+
<executions>
251+
<execution>
252+
<id>start-server</id>
253+
<phase>pre-integration-test</phase>
254+
<goals>
255+
<goal>start</goal>
256+
</goals>
257+
</execution>
258+
<execution>
259+
<id>stop-server</id>
260+
<phase>post-integration-test</phase>
261+
<goals>
262+
<goal>stop</goal>
263+
</goals>
264+
</execution>
265+
</executions>
266+
</plugin>
267+
90268
<plugin>
91269
<groupId>org.apache.maven.plugins</groupId>
92270
<artifactId>maven-surefire-plugin</artifactId>
@@ -98,35 +276,39 @@
98276
</goals>
99277
<configuration>
100278
<excludes>
101-
<exclude>**/*ManualTest.java</exclude>
279+
<exclude>none</exclude>
102280
</excludes>
103281
<includes>
104282
<include>**/*LiveTest.java</include>
105283
</includes>
284+
<systemPropertyVariables>
285+
<webTarget>cargo</webTarget>
286+
</systemPropertyVariables>
106287
</configuration>
107288
</execution>
108289
</executions>
109-
<configuration>
110-
<systemPropertyVariables>
111-
<test.mime>json</test.mime>
112-
</systemPropertyVariables>
113-
</configuration>
114290
</plugin>
291+
115292
</plugins>
116293
</build>
117294
</profile>
118295
</profiles>
119296

120297
<properties>
298+
<!-- various -->
299+
<jstl.version>1.2</jstl.version>
300+
<javax.servlet.version>3.1.0</javax.servlet.version>
121301
<!-- util -->
122302
<guava.version>19.0</guava.version>
123303
<commons-lang3.version>3.5</commons-lang3.version>
124304
<commons-codec.version>1.10</commons-codec.version>
125305
<httpasyncclient.version>4.1.4</httpasyncclient.version>
126306
<!-- testing -->
127307
<wiremock.version>2.5.1</wiremock.version>
308+
<httpcore.version>4.4.11</httpcore.version>
128309
<httpclient.version>4.5.8</httpclient.version> <!-- 4.3.6 --> <!-- 4.4-beta1 -->
129310
<!-- maven plugins -->
311+
<maven-war-plugin.version>2.6</maven-war-plugin.version>
130312
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
131313
</properties>
132314

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.baeldung.basic;
2+
3+
import org.springframework.security.core.AuthenticationException;
4+
import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint;
5+
import org.springframework.stereotype.Component;
6+
7+
import javax.servlet.ServletException;
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
import java.io.IOException;
11+
import java.io.PrintWriter;
12+
13+
@Component
14+
public class MyBasicAuthenticationEntryPoint extends BasicAuthenticationEntryPoint {
15+
16+
@Override
17+
public void commence(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException authException) throws IOException, ServletException {
18+
response.addHeader("WWW-Authenticate", "Basic realm=\"" + getRealmName() + "\"");
19+
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
20+
final PrintWriter writer = response.getWriter();
21+
writer.println("HTTP Status " + HttpServletResponse.SC_UNAUTHORIZED + " - " + authException.getMessage());
22+
}
23+
24+
@Override
25+
public void afterPropertiesSet() throws Exception {
26+
setRealmName("Baeldung");
27+
super.afterPropertiesSet();
28+
}
29+
30+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.baeldung.client;
2+
3+
import java.net.URI;
4+
5+
import org.apache.http.HttpHost;
6+
import org.apache.http.client.AuthCache;
7+
import org.apache.http.client.protocol.HttpClientContext;
8+
import org.apache.http.impl.auth.BasicScheme;
9+
import org.apache.http.impl.client.BasicAuthCache;
10+
import org.apache.http.protocol.BasicHttpContext;
11+
import org.apache.http.protocol.HttpContext;
12+
import org.springframework.http.HttpMethod;
13+
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
14+
15+
public class HttpComponentsClientHttpRequestFactoryBasicAuth extends HttpComponentsClientHttpRequestFactory {
16+
17+
HttpHost host;
18+
19+
public HttpComponentsClientHttpRequestFactoryBasicAuth(HttpHost host) {
20+
super();
21+
this.host = host;
22+
}
23+
24+
protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
25+
return createHttpContext();
26+
}
27+
28+
private HttpContext createHttpContext() {
29+
30+
AuthCache authCache = new BasicAuthCache();
31+
32+
BasicScheme basicAuth = new BasicScheme();
33+
authCache.put(host, basicAuth);
34+
35+
BasicHttpContext localcontext = new BasicHttpContext();
36+
localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
37+
return localcontext;
38+
}
39+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.baeldung.client;
2+
3+
import org.apache.http.HttpHost;
4+
import org.springframework.beans.factory.FactoryBean;
5+
import org.springframework.beans.factory.InitializingBean;
6+
import org.springframework.http.client.ClientHttpRequestFactory;
7+
import org.springframework.http.client.support.BasicAuthenticationInterceptor;
8+
import org.springframework.stereotype.Component;
9+
import org.springframework.web.client.RestTemplate;
10+
11+
@Component
12+
public class RestTemplateFactory implements FactoryBean<RestTemplate>, InitializingBean {
13+
private RestTemplate restTemplate;
14+
15+
public RestTemplateFactory() {
16+
super();
17+
}
18+
19+
// API
20+
21+
@Override
22+
public RestTemplate getObject() {
23+
return restTemplate;
24+
}
25+
26+
@Override
27+
public Class<RestTemplate> getObjectType() {
28+
return RestTemplate.class;
29+
}
30+
31+
@Override
32+
public boolean isSingleton() {
33+
return true;
34+
}
35+
36+
@Override
37+
public void afterPropertiesSet() {
38+
HttpHost host = new HttpHost("localhost", 8082, "http");
39+
final ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryBasicAuth(host);
40+
restTemplate = new RestTemplate(requestFactory);
41+
restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor("user1", "user1Pass"));
42+
}
43+
44+
}

0 commit comments

Comments
 (0)