Skip to content

Commit 281cb6a

Browse files
author
eugenp
committed
httpclient auth work
1 parent c382051 commit 281cb6a

9 files changed

Lines changed: 211 additions & 137 deletions

File tree

httpclient/src/main/webapp/WEB-INF/api-servlet.xml

Lines changed: 0 additions & 6 deletions
This file was deleted.

httpclient/src/main/webapp/WEB-INF/web.xml

Lines changed: 0 additions & 42 deletions
This file was deleted.

httpclient/src/test/java/org/baeldung/httpclient/HttpClientAuthLiveTest.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

2424
public class HttpClientAuthLiveTest {
2525

26+
private static final String URL_SECURED_BY_BASIC_AUTHENTICATION = "http://localhost:8080/spring-security-rest-basic-auth/api/foos/1";
27+
private static final String DEFAULT_USER = "user1";
28+
private static final String DEFAULT_PASS = "user1Pass";
29+
2630
private CloseableHttpClient instance;
2731

2832
private CloseableHttpResponse response;
@@ -51,22 +55,23 @@ public final void after() throws IllegalStateException, IOException {
5155

5256
// tests
5357

54-
// simple request - response
55-
5658
@Test
57-
public final void whenExecutingBasicGetRequest_thenNoExceptions() throws ClientProtocolException, IOException {
58-
final CredentialsProvider provider = new BasicCredentialsProvider();
59-
final AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
60-
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user1", "user1Pass");
61-
62-
provider.setCredentials(scope, credentials);
63-
64-
instance = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
59+
public final void whenExecutingBasicGetRequestWithBasicAuthenticationEnabled_thenSuccess() throws ClientProtocolException, IOException {
60+
instance = HttpClientBuilder.create().setDefaultCredentialsProvider(provider()).build();
6561

66-
response = instance.execute(new HttpGet("http://localhost:8080/spring-security-mvc-basic-auth/homepage.html"));
62+
response = instance.execute(new HttpGet(URL_SECURED_BY_BASIC_AUTHENTICATION));
6763

6864
final int statusCode = response.getStatusLine().getStatusCode();
6965
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
7066
}
7167

68+
// UTILS
69+
70+
private final CredentialsProvider provider() {
71+
final CredentialsProvider provider = new BasicCredentialsProvider();
72+
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS);
73+
provider.setCredentials(AuthScope.ANY, credentials);
74+
return provider;
75+
}
76+
7277
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.baeldung.httpclient;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
6+
import org.apache.http.HttpEntity;
7+
import org.apache.http.auth.AuthScope;
8+
import org.apache.http.auth.UsernamePasswordCredentials;
9+
import org.apache.http.client.ClientProtocolException;
10+
import org.apache.http.client.CredentialsProvider;
11+
import org.apache.http.client.methods.CloseableHttpResponse;
12+
import org.apache.http.client.methods.HttpGet;
13+
import org.apache.http.client.protocol.HttpClientContext;
14+
import org.apache.http.impl.client.BasicCredentialsProvider;
15+
import org.apache.http.impl.client.CloseableHttpClient;
16+
import org.apache.http.impl.client.HttpClientBuilder;
17+
import org.junit.After;
18+
import org.junit.Before;
19+
import org.junit.Test;
20+
21+
public class HttpClientSandboxLiveTest {
22+
23+
private CloseableHttpClient client;
24+
25+
private CloseableHttpResponse response;
26+
27+
@Before
28+
public final void before() {
29+
client = HttpClientBuilder.create().build();
30+
}
31+
32+
@After
33+
public final void after() throws IllegalStateException, IOException {
34+
if (response == null) {
35+
return;
36+
}
37+
38+
try {
39+
final HttpEntity entity = response.getEntity();
40+
if (entity != null) {
41+
// EntityUtils.consume(entity);
42+
final InputStream instream = entity.getContent();
43+
instream.close();
44+
}
45+
} finally {
46+
response.close();
47+
}
48+
}
49+
50+
// tests
51+
52+
// simple request - response
53+
54+
@Test
55+
public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws ClientProtocolException, IOException {
56+
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
57+
final AuthScope authscp = new AuthScope("api.calltrackingmetrics.com", 443);
58+
credentialsProvider.setCredentials(authscp, new UsernamePasswordCredentials("mykey", "mysecret"));
59+
final HttpClientContext localContext = HttpClientContext.create();
60+
localContext.setCredentialsProvider(credentialsProvider);
61+
final HttpGet httpGet = new HttpGet("https://api.calltrackingmetrics.com/api/v1/accounts/myaccout/calls.json");
62+
response = client.execute(httpGet);
63+
64+
System.out.println(response.getStatusLine());
65+
}
66+
67+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
web - 2014-01-30 20:48:07,161 [main] DEBUG o.a.h.c.protocol.RequestAddCookies - CookieSpec selected: best-match
2+
web - 2014-01-30 20:48:07,171 [main] DEBUG o.a.h.c.protocol.RequestAuthCache - Auth cache not set in the context
3+
web - 2014-01-30 20:48:07,172 [main] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Connection request: [route: {}->http://localhost:8080][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
4+
web - 2014-01-30 20:48:07,185 [main] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: {}->http://localhost:8080][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
5+
web - 2014-01-30 20:48:07,190 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Opening connection {}->http://localhost:8080
6+
web - 2014-01-30 20:48:07,192 [main] DEBUG o.a.h.c.HttpClientConnectionManager - Connecting to localhost/127.0.0.1:8080
7+
web - 2014-01-30 20:48:07,193 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Executing request GET /spring-security-rest-basic-auth/api/foos/1 HTTP/1.1
8+
web - 2014-01-30 20:48:07,193 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Target auth state: UNCHALLENGED
9+
web - 2014-01-30 20:48:07,193 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Proxy auth state: UNCHALLENGED
10+
web - 2014-01-30 20:48:07,194 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> GET /spring-security-rest-basic-auth/api/foos/1 HTTP/1.1
11+
web - 2014-01-30 20:48:07,194 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Host: localhost:8080
12+
web - 2014-01-30 20:48:07,194 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Connection: Keep-Alive
13+
web - 2014-01-30 20:48:07,194 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.3.1 (java 1.5)
14+
web - 2014-01-30 20:48:07,194 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Accept-Encoding: gzip,deflate
15+
web - 2014-01-30 20:48:07,203 [main] DEBUG org.apache.http.headers - http-outgoing-0 << HTTP/1.1 401 Unauthorized
16+
web - 2014-01-30 20:48:07,203 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Server: Apache-Coyote/1.1
17+
web - 2014-01-30 20:48:07,203 [main] DEBUG org.apache.http.headers - http-outgoing-0 << WWW-Authenticate: Basic realm="Spring Security Application"
18+
web - 2014-01-30 20:48:07,203 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Type: text/html;charset=utf-8
19+
web - 2014-01-30 20:48:07,203 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Language: en
20+
web - 2014-01-30 20:48:07,203 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Length: 1061
21+
web - 2014-01-30 20:48:07,203 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Date: Thu, 30 Jan 2014 18:48:07 GMT
22+
web - 2014-01-30 20:48:07,206 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Connection can be kept alive indefinitely
23+
web - 2014-01-30 20:48:07,206 [main] DEBUG o.a.http.impl.auth.HttpAuthenticator - Authentication required
24+
web - 2014-01-30 20:48:07,206 [main] DEBUG o.a.http.impl.auth.HttpAuthenticator - localhost:8080 requested authentication
25+
web - 2014-01-30 20:48:07,206 [main] DEBUG o.a.h.i.c.TargetAuthenticationStrategy - Authentication schemes in the order of preference: [negotiate, Kerberos, NTLM, Digest, Basic]
26+
web - 2014-01-30 20:48:07,206 [main] DEBUG o.a.h.i.c.TargetAuthenticationStrategy - Challenge for negotiate authentication scheme not available
27+
web - 2014-01-30 20:48:07,206 [main] DEBUG o.a.h.i.c.TargetAuthenticationStrategy - Challenge for Kerberos authentication scheme not available
28+
web - 2014-01-30 20:48:07,206 [main] DEBUG o.a.h.i.c.TargetAuthenticationStrategy - Challenge for NTLM authentication scheme not available
29+
web - 2014-01-30 20:48:07,206 [main] DEBUG o.a.h.i.c.TargetAuthenticationStrategy - Challenge for Digest authentication scheme not available
30+
web - 2014-01-30 20:48:07,213 [main] DEBUG o.a.http.impl.auth.HttpAuthenticator - Selected authentication options: [BASIC]
31+
web - 2014-01-30 20:48:07,214 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Executing request GET /spring-security-rest-basic-auth/api/foos/1 HTTP/1.1
32+
web - 2014-01-30 20:48:07,214 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Target auth state: CHALLENGED
33+
web - 2014-01-30 20:48:07,214 [main] DEBUG o.a.http.impl.auth.HttpAuthenticator - Generating response to an authentication challenge using basic scheme
34+
web - 2014-01-30 20:48:07,215 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Proxy auth state: UNCHALLENGED
35+
web - 2014-01-30 20:48:07,215 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> GET /spring-security-rest-basic-auth/api/foos/1 HTTP/1.1
36+
web - 2014-01-30 20:48:07,215 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Host: localhost:8080
37+
web - 2014-01-30 20:48:07,215 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Connection: Keep-Alive
38+
web - 2014-01-30 20:48:07,215 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.3.1 (java 1.5)
39+
web - 2014-01-30 20:48:07,215 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Accept-Encoding: gzip,deflate
40+
web - 2014-01-30 20:48:07,215 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Authorization: Basic dXNlcjE6dXNlcjFQYXNz
41+
web - 2014-01-30 20:48:07,217 [main] DEBUG org.apache.http.headers - http-outgoing-0 << HTTP/1.1 200 OK
42+
web - 2014-01-30 20:48:07,217 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Server: Apache-Coyote/1.1
43+
web - 2014-01-30 20:48:07,217 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Type: application/json;charset=UTF-8
44+
web - 2014-01-30 20:48:07,217 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Transfer-Encoding: chunked
45+
web - 2014-01-30 20:48:07,217 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Date: Thu, 30 Jan 2014 18:48:07 GMT
46+
web - 2014-01-30 20:48:07,218 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Connection can be kept alive indefinitely
47+
web - 2014-01-30 20:48:07,218 [main] DEBUG o.a.http.impl.auth.HttpAuthenticator - Authentication succeeded
48+
web - 2014-01-30 20:48:07,219 [main] DEBUG o.a.h.i.c.TargetAuthenticationStrategy - Caching 'basic' auth scheme for http://localhost:8080
49+
web - 2014-01-30 20:48:07,227 [main] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Connection [id: 0][route: {}->http://localhost:8080] can be kept alive indefinitely
50+
web - 2014-01-30 20:48:07,227 [main] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Connection released: [id: 0][route: {}->http://localhost:8080][total kept alive: 1; route allocated: 1 of 2; total allocated: 1 of 20]
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<beans xmlns="http://www.springframework.org/schema/beans"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd" >
2+
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
54

65
</beans>
Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
3-
<display-name>Spring Security Basic Auth Application</display-name>
4-
<context-param>
5-
<param-name>contextClass</param-name>
6-
<param-value>
7-
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
8-
</param-value>
9-
</context-param>
10-
<context-param>
11-
<param-name>contextConfigLocation</param-name>
12-
<param-value>org.baeldung.spring</param-value>
13-
</context-param>
14-
<listener>
15-
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
16-
</listener>
17-
<servlet>
18-
<servlet-name>mvc</servlet-name>
19-
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
20-
<load-on-startup>1</load-on-startup>
21-
</servlet>
22-
<servlet-mapping>
23-
<servlet-name>mvc</servlet-name>
24-
<url-pattern>/</url-pattern>
25-
</servlet-mapping>
26-
<filter>
27-
<filter-name>springSecurityFilterChain</filter-name>
28-
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
29-
</filter>
30-
<filter-mapping>
31-
<filter-name>springSecurityFilterChain</filter-name>
32-
<url-pattern>/*</url-pattern>
33-
</filter-mapping>
34-
<welcome-file-list>
35-
<welcome-file>index.html</welcome-file>
36-
</welcome-file-list>
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
3+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
4+
<display-name>Spring Security Basic Auth Application</display-name>
5+
6+
<context-param>
7+
<param-name>contextClass</param-name>
8+
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
9+
</context-param>
10+
<context-param>
11+
<param-name>contextConfigLocation</param-name>
12+
<param-value>org.baeldung.spring</param-value>
13+
</context-param>
14+
15+
<listener>
16+
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
17+
</listener>
18+
19+
<servlet>
20+
<servlet-name>mvc</servlet-name>
21+
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
22+
<load-on-startup>1</load-on-startup>
23+
</servlet>
24+
<servlet-mapping>
25+
<servlet-name>mvc</servlet-name>
26+
<url-pattern>/</url-pattern>
27+
</servlet-mapping>
28+
29+
<filter>
30+
<filter-name>springSecurityFilterChain</filter-name>
31+
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
32+
</filter>
33+
<filter-mapping>
34+
<filter-name>springSecurityFilterChain</filter-name>
35+
<url-pattern>/*</url-pattern>
36+
</filter-mapping>
37+
38+
<welcome-file-list>
39+
<welcome-file>index.html</welcome-file>
40+
</welcome-file-list>
41+
3742
</web-app>

spring-security-rest-basic-auth/src/main/resources/webSecurityConfig.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<authentication-manager>
1616
<authentication-provider>
1717
<user-service>
18-
<user name="user" password="userPass" authorities="ROLE_USER" />
18+
<user name="user1" password="user1Pass" authorities="ROLE_USER" />
1919
</user-service>
2020
</authentication-provider>
2121
</authentication-manager>
Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,42 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
4-
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
5-
id="WebApp_ID" version="3.0">
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
3+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
64

7-
<display-name>Spring Security Custom Application</display-name>
5+
<display-name>Spring Security Custom Application</display-name>
86

9-
<!-- Spring root -->
10-
<context-param>
11-
<param-name>contextClass</param-name>
12-
<param-value>
13-
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
14-
</param-value>
15-
</context-param>
16-
<context-param>
17-
<param-name>contextConfigLocation</param-name>
18-
<param-value>org.baeldung.spring</param-value>
19-
</context-param>
7+
<!-- Spring root -->
8+
<context-param>
9+
<param-name>contextClass</param-name>
10+
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
11+
</context-param>
12+
<context-param>
13+
<param-name>contextConfigLocation</param-name>
14+
<param-value>org.baeldung.spring</param-value>
15+
</context-param>
2016

21-
<listener>
22-
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
23-
</listener>
17+
<listener>
18+
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
19+
</listener>
2420

25-
<!-- Spring child -->
26-
<servlet>
27-
<servlet-name>api</servlet-name>
28-
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
29-
<load-on-startup>1</load-on-startup>
30-
</servlet>
31-
<servlet-mapping>
32-
<servlet-name>api</servlet-name>
33-
<url-pattern>/api/*</url-pattern>
34-
</servlet-mapping>
35-
36-
<!-- Spring Security -->
37-
<filter>
38-
<filter-name>springSecurityFilterChain</filter-name>
39-
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
40-
</filter>
41-
<filter-mapping>
42-
<filter-name>springSecurityFilterChain</filter-name>
43-
<url-pattern>/*</url-pattern>
44-
</filter-mapping>
21+
<!-- Spring child -->
22+
<servlet>
23+
<servlet-name>api</servlet-name>
24+
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
25+
<load-on-startup>1</load-on-startup>
26+
</servlet>
27+
<servlet-mapping>
28+
<servlet-name>api</servlet-name>
29+
<url-pattern>/api/*</url-pattern>
30+
</servlet-mapping>
31+
32+
<!-- Spring Security -->
33+
<filter>
34+
<filter-name>springSecurityFilterChain</filter-name>
35+
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
36+
</filter>
37+
<filter-mapping>
38+
<filter-name>springSecurityFilterChain</filter-name>
39+
<url-pattern>/*</url-pattern>
40+
</filter-mapping>
4541

4642
</web-app>

0 commit comments

Comments
 (0)