多一份贡献,多一份环保

https://github.com/dounine

springcloud fegin获取request header解决方案

假设现在有A服务,B服务,外部使用RESTApi请求调用A服务,在请求头上有token字段,A服务使用完后,B服务也要使用,如何才能把token也转发到B服务呢?这里可以使用Feign的RequestInterceptor,但是直接使用一般情况下HttpServletRequest上下文对象是为空的,这里要怎么处理,请看下文。

springcloud

演示

A服务FeginInterceptor

@Configuration
public class FeginInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        Map<String,String> headers = getHeaders(getHttpServletRequest());
        for(String headerName : headers.keySet()){
            requestTemplate.header(headerName, getHeaders(getHttpServletRequest()).get(headerName));
        }
    }

    private HttpServletRequest getHttpServletRequest() {
        try {
            return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private Map<String, String> getHeaders(HttpServletRequest request) {
        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;
    }

}

重点配置
A服务配置bootstrap.yml

hystrix:
  command:
    default:
      execution:
        isolation:
          strategy: SEMAPHORE

A服务build.gradle

buildscript {
    ext{
        springBootVersion = '1.5.9.RELEASE'
    }

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath "io.spring.gradle:dependency-management-plugin:0.5.6.RELEASE"
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: "io.spring.dependency-management"
version = '0.0.1-SNAPSHOT'
group 'com.dounine.test'

sourceCompatibility = 1.8

repositories {
    mavenLocal()
    mavenCentral()
}

ext {
    springCloudVersion = 'Dalston.SR2'
}

dependencies {
    compile('org.springframework.cloud:spring-cloud-starter-config')
    compile('org.springframework.cloud:spring-cloud-starter-eureka')
    compile('org.springframework.cloud:spring-cloud-starter-feign')
    compile group: 'org.aspectj', name: 'aspectjweaver', version: '1.8.13'
    compile('org.springframework.boot:spring-boot-starter-data-redis')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

B服务Action

    @Autowired
    private HttpServletRequest servletRequest;

    public String test() {
        return servletRequest.getHeader("token");
    }

B服务问题

如若B服务也要将header请求转发到其它服务,将A服务的配置也应用到B服务上即可

阅读更多
版权声明:多一份转载,多一份环保 https://blog.csdn.net/dounine/article/details/79976747
上一篇Redis 可视化界面工具
下一篇mongo 新增删除字段操作
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭