後續更新地址: https://www.aiprose.com/blog/36
在springcloud 微服務中大部分是通過token來驗證用戶的,那麼有個問題,假設現在有A服務,B服務,外部使用RESTApi請求調用A服務,在請求頭上有token字段,A服務使用完後,B服務也要使用,如何才能把token也轉發到B服務呢,最差的解決辦法就是吧token放到請求參數中,但是這樣第一個是明文傳輸,第二個是比較麻煩,前端每次都要加個參數。
這裏可以使用Feign的RequestInterceptor,把request裏的請求參數包括請求頭全部複製到feign的request裏,但是直接使用一般情況下HttpServletRequest上下文對象是爲空的,其實加個配置就可以解決。
1.服務A中 application.yml 加入如下配置
hystrix:
command:
default:
execution:
isolation:
strategy: SEMAPHORE #加上這個就可以獲取到HttpServletRequest
thread:
timeoutInMilliseconds: 10000
2.服務A中加入 FeginInterceptor
@Configuration
public class FeginInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
try {
Map<String,String> headers = getHeaders();
for(String headerName : headers.keySet()){
requestTemplate.header(headerName, headers.get(headerName));
}
}catch (Exception e){
e.printStackTrace();
}
}
private Map<String, String> getHeaders(){
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
Map<String, String> map = new LinkedHashMap<>();
Enumeration<String> enumeration = request.getHeaderNames();
while (enumeration.hasMoreElements()) {
String key = enumeration.nextElement();
String value = request.getHeader(key);
map.put(key, value);
}
return map;
}
}
若服務B或C也想傳遞token,加上上述A配置即可