java servlet 【Spring Cloud】feign调用携带token - RequestContextListener

📅 2026-11-30 👤 拼多多助力营销技巧 📂 拼多多助力平台 👁️ 365 次阅读

当我们再用feign调用的时候,如果对应服务需要token验证则需要我们传递token

网上提供的方法都是添加如下配置:

@Configuration

public class FeignConfig implements RequestInterceptor {

@Override

public void apply(RequestTemplate requestTemplate) {

ServletRequestAttributes attributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());

if (attributes != null) {

HttpServletRequest request = attributes.getRequest();

requestTemplate.header("token", request.getHeader("token"));

}

}

}

但是使用这个的时候由于fegin是使用子线程调用的,导致获取到的attributes不存在,所以我们要使用可继承的InheritableThreadLocal来保存

默认如下所示

public class RequestContextListener implements ServletRequestListener {

private static final String REQUEST_ATTRIBUTES_ATTRIBUTE =

RequestContextListener.class.getName() + ".REQUEST_ATTRIBUTES";

@Override

public void requestInitialized(ServletRequestEvent requestEvent) {

if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) {

throw new IllegalArgumentException(

"Request is not an HttpServletRequest: " + requestEvent.getServletRequest());

}

HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest();

ServletRequestAttributes attributes = new ServletRequestAttributes(request);

request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes);

LocaleContextHolder.setLocale(request.getLocale());

RequestContextHolder.setRequestAttributes(attributes); // 保存attributes

}

}

// RequestContextHolder

/**

* Bind the given RequestAttributes to the current thread,

*notexposing it as inheritable for child threads.

* @param attributes the RequestAttributes to expose

* @see #setRequestAttributes(RequestAttributes, boolean)

*/

public static void setRequestAttributes(@Nullable RequestAttributes attributes) {

setRequestAttributes(attributes, false); // inheritable 默认为false

}

所以要使用如下方法重新执行一下

public class MyRequestContextListener implements ServletRequestListener {

private static final String REQUEST_ATTRIBUTES_ATTRIBUTE =

MyRequestContextListener.class.getName() + ".REQUEST_ATTRIBUTES";

/**

* 初始化

*

* @param requestEvent Information about the request

*/

@Override

public void requestInitialized(ServletRequestEvent requestEvent) {

if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) {

throw new IllegalArgumentException(

"Request is not an HttpServletRequest: " + requestEvent.getServletRequest());

}

HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest();

ServletRequestAttributes attributes = new ServletRequestAttributes(request);

request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes);

LocaleContextHolder.setLocale(request.getLocale()); 拼多多帮助力入口

RequestContextHolder.setRequestAttributes(attributes, true);

}

...

}

InheritableThreadLocal也有局限性,即后续父线程数据更改的时候不会在子线程进行同步

tips: InheritableThreadLocal 由于InheritableThreadLocal 是只有在线程初始化的时候才会进行初始化复制,所以后续父线程数据更改的时候不会进行同步,如下所示

private void init(ThreadGroup g, Runnable target, String name,

long stackSize, AccessControlContext acc,

boolean inheritThreadLocals) {

if (inheritThreadLocals && parent.inheritableThreadLocals != null)

this.inheritableThreadLocals =

ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);

}

// inheritThreadLocals为True时 构造一个新映射,包括来自给定父映射的所有可继承 ThreadLocal。仅由 createInheritedMap 调用。

private ThreadLocalMap(ThreadLocalMap parentMap) {

Entry[] parentTable = parentMap.table;

int len = parentTable.length;

setThreshold(len);

table = new Entry[len];

for (int j = 0; j < len; j++) {

Entry e = parentTable[j];

if (e != null) {

@SuppressWarnings("unchecked")

ThreadLocal

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧