SpringBoot应用中通过使用FeignClient与第三方服务REST API交互。如果请求中需要进行安全认证,可以对FeignClient进行设置,下面以Basic Authentication 认证为例说明如何使用。
@FeignClient(name = "workflow", url = "${workflow.api.baseUrl}", configuration = WorkFlowFeignClientConfiguration.class)
public interface WorkFlowFeignClient {
@RequestMapping(
value = "/execution",
method = RequestMethod.GET)
WorkFLowResponseDto getListOfExecutions(
@RequestParam(name = "flowId") String flowId);
}
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import feign.auth.BasicAuthRequestInterceptor;
public class WorkFlowFeignClientConfiguration {
@Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("api_key", "api_secret");
}
}
注意
WorkFlowFeignClientConfiguration不可以增加@Configuration
注释,加注释后会将整个服务的FeignClient请求全部拦截