PRE: Filters execute before routing to the origin. For example: request authentication, logging debug info
ROUTING: Filters handle routing the request to an origin.
POST: Filters execute after the request has been routed to the origin.
ERROR: Filters execute when an error occurs during one of the other phases.
package org.springframework.cloud.samplezuulfilters;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
@EnableZuulProxy
@SpringBootApplication
public class ZuulGatewayApplication {
@Bean
public AddResponseHeaderFilter addResponseHeaderFilter() {
return new AddResponseHeaderFilter();
}
@Bean
public ModifyResponseBodyFilter modifyResponseHeaderFilter() {
return new ModifyResponseBodyFilter();
}
@Bean
public ModifyResponseDataStreamFilter modifyResponseDataStreamFilter() {
return new ModifyResponseDataStreamFilter();
}
@Bean
public PrefixRequestEntityFilter prefixRequestEntityFilter() {
return new PrefixRequestEntityFilter();
}
@Bean
public QueryParamPortPreFilter queryParamPortPreFilter() {
return new QueryParamPortPreFilter();
}
@Bean
public QueryParamServiceIdPreFilter queryParamServiceIdPreFilter() {
return new QueryParamServiceIdPreFilter();
}
@Bean
public UppercaseRequestEntityFilter modifyRequestEntityFilter() {
return new UppercaseRequestEntityFilter();
}
public static void main(String[] args) {
SpringApplication.run(ZuulGatewayApplication.class, args);
}
}
--------------------------
package org.springframework.cloud.samplezuulfilters;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import com.netflix.zuul.ZuulFilter;
import org.springframework.util.StreamUtils;
import com.netflix.zuul.context.RequestContext;
import static com.netflix.zuul.context.RequestContext.getCurrentContext;
import static org.springframework.util.ReflectionUtils.rethrowRuntimeException;
/**
* @author Spencer Gibb
*/
public class UppercaseRequestEntityFilter extends ZuulFilter {
public String filterType() {
return "pre";
}
public int filterOrder() {
return 6;
}
public boolean shouldFilter() {
RequestContext context = getCurrentContext();
return context.getRequest().getParameter("service") == null;
}
public Object run() {
try {
RequestContext context = getCurrentContext();
InputStream in = (InputStream) context.get("requestEntity");
if (in == null) {
in = context.getRequest().getInputStream();
}
String body = StreamUtils.copyToString(in, Charset.forName("UTF-8"));
// body = "request body modified via set('requestEntity'): "+ body;
body = body.toUpperCase();
context.set("requestEntity", new ByteArrayInputStream(body.getBytes("UTF-8")));
}
catch (IOException e) {
rethrowRuntimeException(e);
}
return null;
}
}
----------------------------
package org.springframework.cloud.samplezuulfilters;
import javax.servlet.http.HttpServletRequest;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import static com.netflix.zuul.context.RequestContext.getCurrentContext;
/**
* @author Spencer Gibb
*/
public class QueryParamServiceIdPreFilter extends ZuulFilter {
public int filterOrder() {
// run before PreDecorationFilter
return 5 - 1;
}
public String filterType() {
return "pre";
}
@Override
public boolean shouldFilter() {
RequestContext ctx = getCurrentContext();
return ctx.getRequest().getParameter("service") != null;
}
public Object run() {
RequestContext ctx = getCurrentContext();
HttpServletRequest request = ctx.getRequest();
// put the serviceId in `RequestContext`
ctx.put("serviceId", request.getParameter("service"));
return null;
}
}
----------------------------------------
package org.springframework.cloud.samplezuulfilters;
import javax.servlet.http.HttpServletRequest;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import static com.netflix.zuul.context.RequestContext.getCurrentContext;
/**
* @author Spencer Gibb
*/
public class QueryParamPortPreFilter extends ZuulFilter {
public int filterOrder() {
// run after PreDecorationFilter
return 5 + 1;
}
public String filterType() {
return "pre";
}
@Override
public boolean shouldFilter() {
RequestContext ctx = getCurrentContext();
return ctx.getRequest().getParameter("port") != null;
}
public Object run() {
RequestContext ctx = getCurrentContext();
HttpServletRequest request = ctx.getRequest();
// put the serviceId in `RequestContext`
String port = request.getParameter("port");
try {
URL url = UriComponentsBuilder.fromUri(ctx.getRouteHost().toURI())
.port(new Integer(port))
.build().toUri().toURL();
ctx.setRouteHost(url);
} catch (Exception e) {
ReflectionUtils.rethrowRuntimeException(e);
}
return null;
}
}
----------------------------------------
package org.springframework.cloud.samplezuulfilters;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequestWrapper;
import org.springframework.util.StreamUtils;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.http.ServletInputStreamWrapper;
import static com.netflix.zuul.context.RequestContext.getCurrentContext;
import static org.springframework.util.ReflectionUtils.rethrowRuntimeException;
/**
* @author Spencer Gibb
*/
public class PrefixRequestEntityFilter extends ZuulFilter {
public String filterType() {
return "pre";
}
public int filterOrder() {
return 6;
}
public boolean shouldFilter() {
RequestContext context = getCurrentContext();
return context.getRequest().getParameter("service") != null;
}
public Object run() {
try {
RequestContext context = getCurrentContext();
InputStream in = (InputStream) context.get("requestEntity");
if (in == null) {
in = context.getRequest().getInputStream();
}
String body = StreamUtils.copyToString(in, Charset.forName("UTF-8"));
body = "request body modified via request wrapper: "+ body;
byte[] bytes = body.getBytes("UTF-8");
context.setRequest(new HttpServletRequestWrapper(getCurrentContext().getRequest()) {
@Override
public ServletInputStream getInputStream() throws IOException {
return new ServletInputStreamWrapper(bytes);
}
@Override
public int getContentLength() {
return bytes.length;
}
@Override
public long getContentLengthLong() {
return bytes.length;
}
});
}
catch (IOException e) {
rethrowRuntimeException(e);
}
return null;
}
}
----------------------------------
package org.springframework.cloud.samplezuulfilters;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import org.springframework.util.StreamUtils;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import static com.netflix.zuul.context.RequestContext.getCurrentContext;
import static org.springframework.util.ReflectionUtils.rethrowRuntimeException;
/**
* @author Spencer Gibb
*/
public class ModifyResponseDataStreamFilter extends ZuulFilter {
public String filterType() {
return "post";
}
public int filterOrder() {
return 999;
}
public boolean shouldFilter() {
RequestContext context = getCurrentContext();
return context.getRequest().getParameter("service") != null;
}
public Object run() {
try {
RequestContext context = getCurrentContext();
InputStream stream = context.getResponseDataStream();
String body = StreamUtils.copyToString(stream, Charset.forName("UTF-8"));
body = "Modified via setResponseDataStream(): " + body;
context.setResponseDataStream(new ByteArrayInputStream(body.getBytes("UTF-8")));
}
catch (IOException e) {
rethrowRuntimeException(e);
}
return null;
}
}
----------------------------------------
package org.springframework.cloud.samplezuulfilters;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import org.springframework.util.StreamUtils;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import static com.netflix.zuul.context.RequestContext.getCurrentContext;
import static org.springframework.util.ReflectionUtils.rethrowRuntimeException;
/**
* @author Spencer Gibb
*/
public class ModifyResponseBodyFilter extends ZuulFilter {
public String filterType() {
return "post";
}
public int filterOrder() {
return 999;
}
public boolean shouldFilter() {
RequestContext context = getCurrentContext();
return context.getRequest().getParameter("service") == null;
}
public Object run() {
try {
RequestContext context = getCurrentContext();
InputStream stream = context.getResponseDataStream();
String body = StreamUtils.copyToString(stream, Charset.forName("UTF-8"));
context.setResponseBody("Modified via setResponseBody(): "+body);
}
catch (IOException e) {
rethrowRuntimeException(e);
}
return null;
}
}
-----------------------
package org.springframework.cloud.samplezuulfilters;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
/**
* @author Spencer Gibb
*/
public class AddResponseHeaderFilter extends ZuulFilter {
public String filterType() {
return "post";
}
public int filterOrder() {
return 999;
}
public boolean shouldFilter() {
return true;
}
public Object run() {
RequestContext context = RequestContext.getCurrentContext();
HttpServletResponse servletResponse = context.getResponse();
servletResponse.addHeader("X-Foo",
UUID.randomUUID().toString());
return null;
}
}
-------------------
public class RequestLoggerFilter extends ZuulFilter {
private static Logger log = LoggerFactory.getLogger(RequestLoggerFilter.class);
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
log.info(String.format("%s request to %s", request.getMethod(),
request.getRequestURL().toString()));
return null;
}
}
No comments:
Post a Comment