问题出现场景
1.前端请求fegin服务端api接口
2.fegin请求user Client端数据接口
3.!这里的Console.log("Token={}",token)输出为空
解决方案
fegin端在config包下新建FeginInterceptor类继承RequestInterceptor拦截器
package config; import java.util.Enumeration; import java.util.LinkedHashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import feign.RequestInterceptor; import feign.RequestTemplate; public class FeginInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate template) { // TODO Auto-generated method stub Map<String, String> headers = getHeaders(); for (String headerName : headers.keySet()) { template.header(headerName, getHeaders().get(headerName)); } } /** *获取所有的header数据 */ 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; } }
@Bean public RequestInterceptor getRequestInterceptor(){ return new FeginInterceptor(); }
成功解决