一、在Feign模块或者核心模块添加一个工具类ThreadLocalUtils,代码如下
package com.zhiyu.common.security.utils;
import org.springframework.util.CollectionUtils;
import java.util.HashMap;
import java.util.Map;
public class ThreadLocalUtils {
private static final InheritableThreadLocal<Map<String, String>> headerMap =
new InheritableThreadLocal<Map<String,String>>(){
@Override
protected Map<String, String> initialValue() {
return new HashMap<>();
}
};
public static Map<String,String> get(){
return headerMap.get();
}
public static String get(String key){
return headerMap.get().get(key);
}
public static void setHeaderMap(Map<String, String> header) {
if (!CollectionUtils.isEmpty(header)) {
for (Map.Entry<String, String> entry : header.entrySet()) {
headerMap.get().put(entry.getKey(), entry.getValue());
}
}
}
public static void set(String key,String value){
headerMap.get().put(key,value);
}
}
二、在需要使用异步线程的地方加入如下代码
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
Map<String, String> headers = ServletUtils.getHeaders(ServletUtils.getRequest());
ThreadLocalUtils.setHeaderMap(headers);
RequestContextHolder.setRequestAttributes(requestAttributes);
三、在Feign的拦截器上添加如下代码
package com.zhiyu.common.security.feign;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zhiyu.common.security.utils.ThreadLocalUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;
import com.zhiyu.common.core.constant.SecurityConstants;
import com.zhiyu.common.core.utils.ServletUtils;
import com.zhiyu.common.core.utils.StringUtils;
import com.zhiyu.common.core.utils.ip.IpUtils;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.web.context.request.RequestAttributes;
@Component
public class FeignRequestInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
HttpServletRequest httpServletRequest = ServletUtils.getRequest();
Map<String, String> headerMaps = ThreadLocalUtils.get();
if (StringUtils.isNotNull(httpServletRequest)) {
Map<String, String> headers = ServletUtils.getHeaders(httpServletRequest);
if (CollectionUtils.isEmpty(headers) && CollectionUtils.isNotEmpty(headerMaps)) {
headers = headerMaps;
}
String userId = headers.get(SecurityConstants.DETAILS_USER_ID);
if (StringUtils.isNotEmpty(userId)) {
requestTemplate.header(SecurityConstants.DETAILS_USER_ID, userId);
}
String userKey = headers.get(SecurityConstants.USER_KEY);
if (StringUtils.isNotEmpty(userKey)) {
requestTemplate.header(SecurityConstants.USER_KEY, userKey);
}
String userName = headers.get(SecurityConstants.DETAILS_USERNAME);
if (StringUtils.isNotEmpty(userName)) {
requestTemplate.header(SecurityConstants.DETAILS_USERNAME, userName);
}
String authentication = headers.get(SecurityConstants.AUTHORIZATION_HEADER);
if (StringUtils.isNotEmpty(authentication)) {
requestTemplate.header(SecurityConstants.AUTHORIZATION_HEADER, authentication);
}
requestTemplate.header("X-Forwarded-For", IpUtils.getIpAddr());
}
}
}