标签:component span web strong err https java oauth OAuth2
1.为什么要用feign拦截器?
作用:由于服务整合了oauth2,在被调用时需要传递令牌才能正常调用,feign拦截器的作用就是为了在服务之间传递令牌。
2.feign拦截器怎么用?
(1)创建拦截器(一般定义在全局中)
@Component public class FeignInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); if (requestAttributes!=null){ HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest(); if (request!=null){ Enumeration<String> headerNames = request.getHeaderNames(); if (headerNames!=null){ while (headerNames.hasMoreElements()){ String headerName = headerNames.nextElement(); if (headerName.equals("authorization")){ String headerValue = request.getHeader(headerName);//Bearer jwt requestTemplate.header(headerName,headerValue);//向下传递令牌 } } } } } } }
2) 更改changgou_order_web启动类,添加拦截器声明
@Bean public FeignInterceptor feignInterceptor(){ return new FeignInterceptor(); }
标签:component span web strong err https java oauth OAuth2
原文地址:https://www.cnblogs.com/hujunwei/p/11442463.html