Skip to content

Commit db963bc

Browse files
committed
Add BasicAuthorizationInterceptor
This commit adds a `ClientHttpRequestInterceptor` that applies a BASIC authorization header for each request. It can be used as follows: ``` BasicAuthorizationInterceptor basicAuthorization = new BasicAuthorizationInterceptor("user", "secret"); restTemplate.getInterceptors().add(basicAuthorization); ``` Issue: SPR-14412
1 parent 64628dc commit db963bc

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.http.client;
18+
19+
import java.io.IOException;
20+
import java.nio.charset.Charset;
21+
22+
import org.springframework.http.HttpRequest;
23+
import org.springframework.util.Assert;
24+
import org.springframework.util.Base64Utils;
25+
26+
/**
27+
* {@link ClientHttpRequestInterceptor} to apply a BASIC authorization header.
28+
*
29+
* @author Phillip Webb
30+
* @since 4.3.1
31+
*/
32+
public class BasicAuthorizationInterceptor implements ClientHttpRequestInterceptor {
33+
34+
private static final Charset UTF_8 = Charset.forName("UTF-8");
35+
36+
private final String username;
37+
38+
private final String password;
39+
40+
public BasicAuthorizationInterceptor(String username, String password) {
41+
Assert.hasLength(username, "Username must not be empty");
42+
this.username = username;
43+
this.password = (password == null ? "" : password);
44+
}
45+
46+
@Override
47+
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
48+
ClientHttpRequestExecution execution) throws IOException {
49+
String token = Base64Utils
50+
.encodeToString((this.username + ":" + this.password).getBytes(UTF_8));
51+
request.getHeaders().add("Authorization", "Basic " + token);
52+
return execution.execute(request, body);
53+
}
54+
55+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.http.client;
18+
19+
import java.net.URI;
20+
21+
import org.junit.Rule;
22+
import org.junit.Test;
23+
import org.junit.rules.ExpectedException;
24+
25+
import org.springframework.beans.DirectFieldAccessor;
26+
import org.springframework.http.HttpMethod;
27+
28+
import static org.junit.Assert.*;
29+
import static org.mockito.Mockito.*;
30+
31+
/**
32+
* Tests for {@link BasicAuthorizationInterceptor}.
33+
*
34+
* @author Phillip Webb
35+
* @author Stephane Nicoll
36+
*/
37+
public class BasicAuthorizationInterceptorTests {
38+
39+
@Rule
40+
public ExpectedException thrown = ExpectedException.none();
41+
42+
@Test
43+
public void createWhenUsernameIsNullShouldThrowException() {
44+
this.thrown.expect(IllegalArgumentException.class);
45+
this.thrown.expectMessage("Username must not be empty");
46+
new BasicAuthorizationInterceptor(null, "password");
47+
}
48+
49+
@Test
50+
public void createWhenUsernameIsEmptyShouldThrowException() throws Exception {
51+
this.thrown.expect(IllegalArgumentException.class);
52+
this.thrown.expectMessage("Username must not be empty");
53+
new BasicAuthorizationInterceptor("", "password");
54+
}
55+
56+
@Test
57+
public void createWhenPasswordIsNullShouldUseEmptyPassword() throws Exception {
58+
BasicAuthorizationInterceptor interceptor = new BasicAuthorizationInterceptor(
59+
"username", null);
60+
assertEquals("", new DirectFieldAccessor(interceptor).getPropertyValue("password"));
61+
}
62+
63+
@Test
64+
public void interceptShouldAddHeader() throws Exception {
65+
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
66+
ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET);
67+
ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);
68+
byte[] body = new byte[] {};
69+
new BasicAuthorizationInterceptor("spring", "boot").intercept(request, body,
70+
execution);
71+
verify(execution).execute(request, body);
72+
assertEquals("Basic c3ByaW5nOmJvb3Q=", request.getHeaders().getFirst("Authorization"));
73+
}
74+
75+
}

0 commit comments

Comments
 (0)