FeignClient调用为请求添加请求头

feign调用的本质其实就是restTemplate调用,就是为restTemplate添加请求头

package com.mbcloud.trip.operation.core.core.feign;

import com.mbcloud.trip.operation.core.client.model.vo.resp.AccAccessTokenResp;
import com.mbcloud.trip.operation.core.core.service.AuthorizationService;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeignClientInterceptor implements RequestInterceptor {
          
   

    public static final String AUTHORIZATION="Authorization";

    public static final String BEARER="Bearer";

    @Autowired
    private AuthorizationService authorizationService;


    @Override
    public void apply(RequestTemplate requestTemplate) {
          
   

        AccAccessTokenResp accessToken = authorizationService.getAccessToken();
        String token = accessToken.getAccessToken();
        requestTemplate.header(AUTHORIZATION,BEARER+token);
    }
}
经验分享 程序员 微信小程序 职场和发展