前言
这两天在试着用springcloud的网关进行请求转发,但有那么奇奇怪怪的问题出现,这不是来了!!!在用zuul转发路径的时候遇到了request的头在原本的请求路径中可以轻轻松松拿到,但就是在用zuul代理的时候总是拿到null。这就是zuul向下传递请求头丢失。
开搞
1.默认已经创建了一个可以进行正常crud的项目,那么把它放进注册中心中
pom:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableEurekaClient //启用eureka客户端注解
public class BaseApplication {
public static void main(String[] args) {
SpringApplication.run(BaseApplication.class, args);
}
}
yml
server:
port: 9001
spring:
application:
name: tensquare-base #指定服务名
#数据库交互的配置就不写了,我用的jpa
eureka:
instance:
prefer-ip-address: true #可进行跨域
client:
service-url:
defaultZone: http://127.0.0.1:eureka端口/eureka/
controller层被代理的方法(其中一个)
图片 2.创建一个zuul网关,把它放进eureka中从而进行路径的跳转,
pom
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId> //eureka的客户端
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId> //zuul
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
</dependencies>
yml
server:
port: 9013
spring:
application:
name: tensquare-web-zuul
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://127.0.0.1:eureka端口/eureka/
zuul:
routes:
tensquare-base: #要拦截的那个微服务
path: /base/** #拦截的路径
serviceId: tensquare-base #拦截后要跳转的路径
sensitive-headers: #这玩意儿最重要,可以让被代理的路径拿到请求头的信息
- Cookie,Set-Cookie,Authorization
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy //开启用zuul进行路径代理
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
zuul自带的过滤器
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
@Component
public class WebFilter extends ZuulFilter {
//请求前过滤
@Override
public String filterType() {
return "pre";
}
//这个过滤器的等级,数字越小,最先使用
@Override
public int filterOrder() {
return 0;
}
//开启这个过滤器
@Override
public boolean shouldFilter() {
return true;
}
//这里面写自己的业务逻辑,从这里面可以拿到请求头,然后设置之后就可以让被代理的接口拿到
@Override
public Object run() throws ZuulException {
//得到上下文
RequestContext currentContext = RequestContext.getCurrentContext();
//得到requset域
HttpServletRequest request = currentContext.getRequest();
if (request.getMethod().equals("OPTIONS")){//zuul自带的进行请求转发的那个方法
return null;
}
//得到头信息
String header = request.getHeader("Authorization");
System.out.println("过滤器的header -------> " + header);
if (header != null && !"".equals(header)) {
//把头信息继续向下传递
currentContext.addZuulRequestHeader("Authorization", header);
System.out.println("currentContext -------> " + currentContext);
}
return currentContext;
}
}
3.测试
原接口测试
原接口
头
代理接口测试
代理接口
头
4.应该就是那个Authorization太敏感了,zuul把它过滤了,同时zuul还会过滤(cookie,Set-Cookie),我就把它们都写进zuul的yml中了
网友评论