• 我爱java系列之---【微服务间的认证—Feign拦截器】


    1.feign的使用场景

    注意:谁发起feign的请求,feign的拦截器就定义在谁身上,拦截器的目的是为了传递令牌。

    2.使用步骤:

    (1)创建拦截器

    在changgou_common服务中创建一个com.changgou.interceptor.FeignInterceptor拦截器,并将所有头文件数据再次加入到Feign请求的微服务头文件中,代码如下:

    @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);
                                requestTemplate.header(headerName,headerValue);//核心代码
                            }
                        }
                    }
                }
            }
            }
    }

    2) 更改changgou_order_web启动类,添加拦截器声明(谁发起feign的请求,就把feign拦截器放在谁上面)

    @Bean
    public FeignInterceptor feignInterceptor(){
        return new FeignInterceptor();
    }
    愿你走出半生,归来仍是少年!
  • 相关阅读:
    Densely Connected Convolutional Networks 论文阅读
    仙剑美丽的背景图片
    欢迎follow github:https://github.com/wuxiangli91
    L1和L2特征的适用场景
    决策树和adaboost
    Dropout caffe源码
    destoon 短信发送函数及短信接口修改
    destoon后台权限-不给客户创始人权限并屏蔽部分功能
    destoon 数据库操作
    destoon修改笔记
  • 原文地址:https://www.cnblogs.com/hujunwei/p/11426005.html
Copyright © 2020-2023  润新知