springcloud fegin acquisition request header Solutions

Suppose there are A service B service, external use RESTApi A call request service, request the head token field, A service after use, but also use B service, how can the token is also forwarded to the B services? RequestInterceptor Feign may be used herein, but is used directly HttpServletRequest empty context objects in general, how to deal with this, see below.

9028759-1f44811b9fb44bb1.png
springcloud

Show

A service 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;
    }

}

Key Configuration
A service configuration bootstrap.yml

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

A service 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 Services Action

    @Autowired
    private HttpServletRequest servletRequest;

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

B service issues

Should the service B should forward the request header to the other services, configuration services A to B is also applied to the service

Guess you like

Origin blog.csdn.net/weixin_33881753/article/details/90839998