美文网首页架构
使用zuul代理, 重写request

使用zuul代理, 重写request

作者: 昼暗 | 来源:发表于2018-06-11 12:54 被阅读0次

背景

zuul的作用类似于nginx, 有时候我们希望在使用zuul代理的同时, 添加一些固定参数

1 添加post参数
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();
        try {
        InputStream in = request.getInputStream();
        String body = StreamUtils.copyToString(in, Charset.forName("UTF-8")); 
        if(StringUtils.isNotEmpty(body))
            body += "&";
        body += "clientId=3"; 
        final byte[] reqBodyBytes = body.getBytes(); 
        ctx.setRequest(new HttpServletRequestWrapper(request){
            @Override
            public ServletInputStream getInputStream() throws IOException { 
                return new ServletInputStreamWrapper(reqBodyBytes); 
            } 
            @Override
            public int getContentLength() { 
                return reqBodyBytes.length; 
            } 
            @Override
            public long getContentLengthLong() { 
                return reqBodyBytes.length; 
            } 
        });
          } catch (IOException e) {
        e.printStackTrace();
    } 
return null;
}
2添加get参数
@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();
    
    String method = request.getMethod();
    List<String> clientParam = new ArrayList<>();
    clientParam.add("3");
    Map<String, List<String>> qp = HTTPRequestUtils.getInstance().getQueryParams();
    qp.put("clientId", clientParam);
}

相关文章

网友评论

    本文标题:使用zuul代理, 重写request

    本文链接:https://www.haomeiwen.com/subject/uvoceftx.html