Zuul使用Filter修改请求参数、请求头和响应头

修改请求参数

RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
Map<String, List<String>> requestQueryParams = ctx.getRequestQueryParams();

if (requestQueryParams == null) {
    requestQueryParams = new HashMap<>();
}

//将要新增的参数添加进去,被调用的微服务可以直接 去取,就想普通的一样,框架会直接注入进去
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("1");
requestQueryParams.put("test", arrayList);

ctx.setRequestQueryParams(requestQueryParams);

修改请求头

RequestContext ctx = RequestContext.getCurrentContext();  
ctx.addZuulRequestHeader("original_requestURL",request.getRequestURL().toString());

修改响应头

RequestContext ctx = RequestContext.getCurrentContext();
HttpServletResponse response = ctx.getResponse();
String info = response.getHeader("info");
String infoSize = response.getHeader("info_size");
/**
 * 设置响应头,使请求变为文件下载
 */
ctx.addZuulResponseHeader("Content-Type", "application/octet-stream");
ctx.addZuulResponseHeader("Content-Disposition", "attachment;fileName=" + info);
ctx.addZuulResponseHeader("Content-Length", "" + infoSize);

重写请求

private void rewriteRequestParams(RoutConfig.Service service) throws Exception {
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();
    Map<String, Object> requestMap = getParams();

    // 请求为Get
    if (HttpMethod.GET.name().equalsIgnoreCase(request.getMethod())) {
        ctx.setRequest(new HttpServletRequestWrapper(request) {
            @Override
            public String getMethod() {
                return HttpMethod.GET.name();
            }

        });
        Map<String, List<String>> r = wrapGetRequestParams();
        ctx.setRequestQueryParams(r);
    }

    // 请求为Post
    if (HttpMethod.POST.name().equalsIgnoreCase(request.getMethod())){
        //byte[] body = JSON.toJSONBytes(requestMap);
        byte[] body = wrapPostRequestBody();
        log.info("Post body: {}", new String(body));
        ctx.setRequest(new HttpServletRequestWrapper(request) {
            @Override
            public ServletInputStream getInputStream() throws IOException {
                return new ServletInputStreamWrapper(body);
            }
            @Override
            public int getContentLength() {
                return body.length;
            }

            @Override
            public long getContentLengthLong() {
                return body.length;
            }

            @Override
            public String getMethod() {
                return HttpMethod.POST.name();
            }

        });
    }
}

private Map<String, List<String>> wrapGetRequestParams() {
    Map<String, List<String>> r = new LinkedHashMap<>();
    r.put("name", Collections.singletonList("Joe.Ye"));
    return r;
}

private byte[] wrapPostRequestBody() {
    Map<String, Object> requestBody = new LinkedHashMap();
    requestBody.put("name", "Joe.Ye");
    return JSON.toJSONBytes(requestBody);
}
上一篇 Spring Cloud Zuul 会话保持问题
下一篇 高并发下Zuul参数调优
目录
文章列表
1 Prometheus + Alertmanager 报警实现(email报警)
Prometheus + Alertmanager 报警实现(email报警)
2
MySQL实现主从复制功能
MySQL实现主从复制功能
3
设计模式(11)命令模式
设计模式(11)命令模式
4
BigDecimal比较相等,不能用equals,要用compareTo
BigDecimal比较相等,不能用equals,要用compareTo
5
OKHttp3学习之九:文件上传(拦截器获取上传进度)
OKHttp3学习之九:文件上传(拦截器获取上传进度)
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。