0
点赞
收藏
分享

微信扫一扫

FeignClient增加Basic Authentication认证信息

90哦吼 2022-01-15 阅读 47

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请求全部拦截

举报

相关推荐

0 条评论