zuul adds or modifys request parameters
1. Why use this
In microservice systems built on springcloud, gateway zuul is usually used to perform some filtering operations such as user verification. For example, the user stores tokens in the header or url parameters. The gateway layer needs to use this token to find the user's userId and store it in the request so that subsequent microservices can be used directly to avoid using token query again.
2. Basic knowledge
The biggest usage in zuul is in addition to routing, which is filter. Custom filters need to implement the interface ZuulFilter. In the run() method, you can use it.
RequestContext ctx = (); HttpServletRequest request = ();
Get the request, but there is only getParameter() in the request and there is no setParameter() method, so it is not feasible to directly modify the url parameter. In addition, although setAttribute() can be used in reqeust, the attribute set here may not be obtained in subsequent microservices due to different scopes, so another method must be considered.
3. Specific practices
The final method is to use
(new HttpServletRequestWrapper(request) {})
The way to reconstruct the request in the context, the code is as follows:
// For example, add userId to the request parametertry { InputStream in = ().getInputStream(); String body = (in, ("UTF-8")); if((body)){ body = "{}"; } JSONObject jsonObject = (body); ("userId", 666); String newBody = (); final byte[] reqBodyBytes = (); (new HttpServletRequestWrapper(request){ @Override public ServletInputStream getInputStream() throws IOException { return new ServletInputStreamWrapper(reqBodyBytes); } @Override public int getContentLength() { return ; } @Override public long getContentLengthLong() { return ; } }); } catch (IOException e) { (); }
The idea is to get the requested input stream and rewrite it, that is, rewrite the json parameters.
In the subsequent microservice controller, the request parameters added or modified through zuul are obtained in the following way.
InputStream in = request().getInputStream(); String body = (in, ("UTF-8")); if((body)){ JSONObject jsonObject = (body); Object userId = ("userId"); }
zuul modify request url
In addition to modifying the request parameters, setting the response header, and responding to the body, there is another requirement that is url re-or modify the url. Here we briefly describe how to modify the url in zuul.
Forwarding configuration
demo: ribbon: NIWSServerListClassName: listOfServers: 192.168.99.100,192.168.99.101 zuul: routes: demo: path: /demo/** stripPrefix: true serviceId: demo
filter configuration
@Component public class UrlPathFilter extends ZuulFilter{ @Override public String filterType() { return FilterConstants.PRE_TYPE; } @Override public int filterOrder() { return FilterConstants.PRE_DECORATION_FILTER_ORDER + 1; } @Override public boolean shouldFilter() { final String serviceId = (String) ().get("proxy"); return "demo".equals(serviceId); } @Override public Object run() { RequestContext context = (); Object originalRequestPath = (FilterConstants.REQUEST_URI_KEY); //http://localhost:10000/demo/list/data //-->/api/prefix/list/data String modifiedRequestPath = "/api/prefix" + originalRequestPath; (FilterConstants.REQUEST_URI_KEY, modifiedRequestPath); return null; } }
This is done!
The above is personal experience. I hope you can give you a reference and I hope you can support me more.