feignclient在header中传jwt

Springboog集成oracle数据源(原先已经配置druid项目可以跑起来)

1:pom中添加feign所需依赖

```java

    org.springframework.cloud

    spring-cloud-starter-feign

    com.netflix.feign

    feign-httpclient

    RELEASE

```

2. 启动类添加@EnableFeignClients注解表示启用feign功能

3. ##### 创建自定义请求拦截器

template.header("access_token", "token")直接在header中添加参数,这种一般的比较少

常用的是在Servlet中获取参数再进行feign传递

1.

  ```java

  package com.bosssoft.gp.portal.center.config;

  import feign.RequestInterceptor;

  import feign.RequestTemplate;

  import org.springframework.stereotype.Component;

  import org.springframework.web.context.request.RequestContextHolder;

  import org.springframework.web.context.request.ServletRequestAttributes;

  import javax.servlet.http.HttpServletRequest;

  import java.util.Enumeration;

  /**

    * @author pei

    * @description

    * @createDate 2019/3/18

    */

  @Component

  public class RequestInterceptors implements RequestInterceptor {

      /* 直接添加参数,这种不常用

      @Override

      public void apply(RequestTemplate template) {

          template.header("access_token", "token");

      }*/

      @Override

      public void apply(RequestTemplate requestTemplate) {

          ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder

                  .getRequestAttributes();

            //在servlet中获取参数     

          HttpServletRequest request = attributes.getRequest();

          Enumeration headerNames = request.getHeaderNames();

          if (headerNames != null) {

              while (headerNames.hasMoreElements()) {

                  String name = headerNames.nextElement();

                  String values = request.getHeader(name);

                  //传递参数

                  requestTemplate.header(name, values);

              }

          }

      }

  }


  ```

  创建feign接口

  ```java

  package com.bosssoft.gp.portal.center.service.api;

  import com.bosssoft.gp.portal.center.config.RequestInterceptors;

  import org.springframework.cloud.netflix.feign.FeignClient;

  import org.springframework.web.bind.annotation.RequestMapping;

  import org.springframework.web.bind.annotation.RequestParam;

  //value为服务名,配置拦截器

  @FeignClient(value="gp-pei", configuration = RequestInterceptors.class)

  public interface EurekaClientFeign {

      @RequestMapping(value= "client")

      //参数为name

      String sayHiFromClientEureka(@RequestParam(value = "name") String name);

  }

  ```

  创建controller调用

  ```java

      @Resource

      EurekaClientFeign eurekaClientFeign;

      @RequestMapping(value = "/feignClient")

      @ResponseBody

      public void feignClient() {

          String string = eurekaClientFeign.sayHiFromClientEureka("pei");

          System.out.println(string);


      }

  }

  ```

  创建被调用的客户端controller

```java

@RequestMapping("client")

@ResponseBody

public void client(HttpServletRequest request,String name) {

    System.out.println("name:"+name);

    String token = request.getHeader("access_token");

    System.out.println(token+"token");

    Map jwtMap = GpJwtUtils.parseTokenByJKSFile(token, "public.txt");

    String userId = (String) jwtMap.get("userId");

    System.out.println("id:"+userId);

}

```

##### 7 .测试

ip:port/eignClient

参数 :access_token

```

name:pei

eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsidW5pdHktcmVzb3VyY2UiXSwidXNlcl9uYW1lIjoi5YiY5L2c6b6ZIiwidXNlclR5cGVJbmZvIjpbeyJ1c2VyVHlwZUluZm9JZCI6IjQiLCJ1c2VySWQiOiJmNGUyZTE3MS1iZTM5LTQ1OTktOTY0Mi1hZDQ4ZjIyZWE3NTIiLCJ1c2VyVHlwZUlkIjoiMyIsInN0YXR1cyI6IjEifSx7InVzZXJUeXBlSW5mb0lkIjoiNSIsInVzZXJJZCI6ImY0ZTJlMTcxLWJlMzktNDU5OS05NjQyLWFkNDhmMjJlYTc1MiIsInVzZXJUeXBlSWQiOiIzIiwic3RhdHVzIjoiMSJ9LHsidXNlclR5cGVJbmZvSWQiOiI2IiwidXNlcklkIjoiZjRlMmUxNzEtYmUzOS00NTk5LTk2NDItYWQ0OGYyMmVhNzUyIiwidXNlclR5cGVJZCI6IjMiLCJzdGF0dXMiOiIxIn0seyJ1c2VyVHlwZUluZm9JZCI6Ijk0NDE1NTUyLWQ5MzMtNDI0My05MTViLWVlZWYxMDhiYmVmZSIsInVzZXJJZCI6ImY0ZTJlMTcxLWJlMzktNDU5OS05NjQyLWFkNDhmMjJlYTc1MiIsInVzZXJUeXBlSWQiOiIzIiwic3RhdHVzIjoiMSJ9XSwic2NvcGUiOlsicmVhZCIsIndyaXRlIl0sInVz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