微服务间feign调用token传递失败
在配置拦截器的地方添加拦截:
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
/**
* @apiNote feign传递token拦截统一处理
*/
@Component
public class TokenTelayRequestIntecepor implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
// 从header获取X-token
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes srat = (ServletRequestAttributes) requestAttributes;
HttpServletRequest request = srat.getRequest();
String token = request.getHeader("authorization");
if (StringUtils.isNotBlank(token)) {
//将token传递出去
requestTemplate.header("authorization", token);
}
}
}