001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.http.client.support;
018
019import java.io.IOException;
020import java.nio.charset.Charset;
021
022import org.springframework.http.HttpHeaders;
023import org.springframework.http.HttpRequest;
024import org.springframework.http.client.ClientHttpRequestExecution;
025import org.springframework.http.client.ClientHttpRequestInterceptor;
026import org.springframework.http.client.ClientHttpResponse;
027import org.springframework.lang.Nullable;
028
029/**
030 * {@link ClientHttpRequestInterceptor} to apply a given HTTP Basic Authentication
031 * username/password pair, unless a custom {@code Authorization} header has
032 * already been set.
033 *
034 * @author Juergen Hoeller
035 * @author Sam Brannen
036 * @since 5.1.1
037 * @see HttpHeaders#setBasicAuth
038 * @see HttpHeaders#AUTHORIZATION
039 */
040public class BasicAuthenticationInterceptor implements ClientHttpRequestInterceptor {
041
042        private final String encodedCredentials;
043
044
045        /**
046         * Create a new interceptor which adds Basic Authentication for the
047         * given username and password.
048         * @param username the username to use
049         * @param password the password to use
050         * @see HttpHeaders#setBasicAuth(String, String)
051         * @see HttpHeaders#encodeBasicAuth(String, String, Charset)
052         */
053        public BasicAuthenticationInterceptor(String username, String password) {
054                this(username, password, null);
055        }
056
057        /**
058         * Create a new interceptor which adds Basic Authentication for the
059         * given username and password, encoded using the specified charset.
060         * @param username the username to use
061         * @param password the password to use
062         * @param charset the charset to use
063         * @see HttpHeaders#setBasicAuth(String, String, Charset)
064         * @see HttpHeaders#encodeBasicAuth(String, String, Charset)
065         */
066        public BasicAuthenticationInterceptor(String username, String password, @Nullable Charset charset) {
067                this.encodedCredentials = HttpHeaders.encodeBasicAuth(username, password, charset);
068        }
069
070
071        @Override
072        public ClientHttpResponse intercept(
073                        HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
074
075                HttpHeaders headers = request.getHeaders();
076                if (!headers.containsKey(HttpHeaders.AUTHORIZATION)) {
077                        headers.setBasicAuth(this.encodedCredentials);
078                }
079                return execution.execute(request, body);
080        }
081
082}