public class FeignConfig extends SpringDecoder implements RequestInterceptor, Decoder {
public FeignConfig(ObjectFactory<HttpMessageConverters> messageConverters, ObjectProvider<HttpMessageConverterCustomizer> customizers) {
super(messageConverters, customizers);
}
@Override
public void apply(RequestTemplate requestTemplate) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if(attributes != null) {
HttpServletRequest request = attributes.getRequest();
//添加token
requestTemplate.header("Cookie", request.getHeader("Cookie"));
}
}
@Override
public Object decode(Response response, Type type) throws FeignException, IOException {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if(attributes != null) {
HttpServletResponse myResponse = attributes.getResponse();
//添加Set-Cookie
Collection<String> cookies = response.headers().get("Set-Cookie");
if (!ObjectUtils.isEmpty(cookies)) {
cookies.stream().forEach(cookie -> {
myResponse.addHeader("Set-Cookie", cookie); // 需要再拦截器中异常当前应用的session,否则存在两个,后添加的会生效
});
}
}
Object object = super.decode(response, type);
return object;
}
}
feign 响应与请求拦截配置Cookie
最新推荐文章于 2024-07-08 22:10:27 发布