io.github.slyang.cloud.resttemplate.ApplyAuthHeaderRestTemplateInterceptor Maven / Gradle / Ivy
The newest version!
package io.github.slyang.cloud.resttemplate;
import io.github.slyang.web.security.token.TokenFetchHolder;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import java.io.IOException;
/**
* Description: 透传RestTemplate客户端调用的认证头信息
* Created by slyang
* Copyright (c) 2019, All Rights Reserved.
*/
public class ApplyAuthHeaderRestTemplateInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
HttpHeaders headers = request.getHeaders();
if (!headers.containsKey(TokenFetchHolder.TOKEN_FETCH.getAuthHeaderName())) {
headers.set(TokenFetchHolder.TOKEN_FETCH.getAuthHeaderName(), TokenFetchHolder.TOKEN_FETCH.getToken());
}
return execution.execute(request, body);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy