Feign有提供一个接口,RequestInterceptor;只要实现这个接口,简单做一些处理,比如说我们验证请求头的token叫Access-Token,我们就先取出当前请求的token,然后放到feign请求头上;
新建配置类
@Configuration
public class FeignConfig implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
requestTemplate.header(HttpHeaders.AUTHORIZATION, request.getHeader(HttpHeaders.AUTHORIZATION));
}
}
在@FeignClient接口里添加configuration = {FeignConfig.class}
@FeignClient(value="被调用的服务名",configuration={FeignConfig.class})
即可对做权限处理的服务接口进行调用