feign
遠程調用的請求頭中沒有含有JSESSIONID
的cookie
,所以也就不能得到服務端的session
數據,cart認為沒登錄,獲取不了用戶信息
我們追蹤遠程調用的源碼,可以在SynchronousMethodHandler.targetRequest()方法中看到他會遍歷容器中的RequestInterceptor
進行封裝
Request targetRequest(RequestTemplate template) {
for (RequestInterceptor interceptor : requestInterceptors) {
interceptor.apply(template);
}
return target.apply(template);
}
根據追蹤源碼,我們可以知道我們可以通過給容器中注入RequestInterceptor,從而給遠程調用轉發時帶上cookie
但是在feign的調用過程中,會使用容器中的RequestInterceptor對RequestTemplate進行處理,因此我們可以通過向容器中導入定制的RequestInterceptor為請求加上cookie。
public class GuliFeignConfig {
@Bean
public RequestInterceptor requestInterceptor() {
return new RequestInterceptor() {
@Override
public void apply(RequestTemplate template) {
//1. 使用RequestContextHolder拿到老請求的請求數據
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (requestAttributes != null) {
HttpServletRequest request = requestAttributes.getRequest();
if (request != null) {
//2. 將老請求得到cookie信息放到feign請求上
String cookie = request.getHeader("Cookie");
template.header("Cookie", cookie);
}
}
}
};
}
}
注意:上面在封裝cookie的時候要拿到原來請求的cookie,設置到新的請求中
RequestContextHolder為SpingMVC中共享request數據的上下文,底層由ThreadLocal實現,也就是說該請求只對當前訪問線程有效,如果new了新線程就找不到原來request了