首先写一个过滤器继承ZuulFilter ,然后重写其中的run()方法, 1、获取request对象的信息:
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
String servletPath = request.getServletPath();
拿到request对象之后就能获得请求的信息:
String accessSys = request.getParameter("accessSys");
String paramData = getRequestBody(request);
private String getRequestBody(HttpServletRequest request) {
try {
InputStream v_inputstream = request.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int x = 0;
while ((x = v_inputstream.read()) != -1) {
baos.write(x);
}
baos.flush();
return new String(baos.toByteArray(), UTF_8);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return "";
}
2、遍历routes路由信息 路由的配置信息如下:
@ConfigurationProperties(prefix="application.myroutes")
public class MyRoutesConfig {
private Map<String, String> routes;
public Map<String, String> getRoutes() {
return routes;
}
public void setRoutes(Map<String, String> routes) {
this.routes = routes;
}
}
配置文件的信息如下:
#自定义过滤器 路由,参考:RestFaceFilter.java
application:
myroutes:
routes: {
service: 'epassport-platform',
proxyservice: 'epassport-exservice'
}
if (routes != null) {
Set<String> keySet = routes.getRoutes().keySet();
for (String str : keySet) {
// logger.info("get route key: {}", str);
if (servletPath.startsWith("/" + str + "/")) {
restface_forward = routes.getRoutes().get(str);
break;
}
}
}