All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.c4_soft.springaddons.rest.BearerTokenAuthenticationInterceptor Maven / Gradle / Ivy

There is a newer version: 7.9.0-M4
Show newest version
package com.c4_soft.springaddons.rest;

import java.io.IOException;

import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.lang.NonNull;

import lombok.Data;

/**
 * A {@link ClientHttpRequestInterceptor} adding a Bearer Authorization header (if the {@link BearerProvider} provides one).
 *
 * @author Jerome Wacongne ch4mp@c4-soft.com
 */
@Data
public class BearerTokenAuthenticationInterceptor implements ClientHttpRequestInterceptor {
	private final BearerProvider bearerProvider;

	@Override
	public @NonNull ClientHttpResponse intercept(@NonNull HttpRequest request, @NonNull byte[] body, @NonNull ClientHttpRequestExecution execution)
			throws IOException {
		bearerProvider.getBearer().ifPresent(bearer -> {
			request.getHeaders().setBearerAuth(bearer);
		});
		return execution.execute(request, body);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy