Skip to content

Instantly share code, notes, and snippets.

@nickgrealy
Last active October 26, 2015 01:22
Show Gist options
  • Save nickgrealy/b4a582c8e1c411c5396d to your computer and use it in GitHub Desktop.
Save nickgrealy/b4a582c8e1c411c5396d to your computer and use it in GitHub Desktop.
143-X-Forwarded-Path
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import javax.servlet.Filter;
import java.net.URI;
import static hello.HeaderOverrideFilter.buildForwardedHeadersFilter;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Value("${proxy.url:https://mydefaultproxy.net:1234/somecontext}")
URI baseProxyUrl;
@Bean
Filter interceptor() {
return buildForwardedHeadersFilter(baseProxyUrl);
}
}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URI;
import java.util.*;
import static org.springframework.util.StringUtils.hasText;
/**
* A RequestWrapper, which provides static utility methods to facilitate creating a {@link Filter}, to auto add
* X-Forwarded-* HTTP headers to incoming HTTP requests.
*
* @author Nick Grealy
*/
public class HeaderOverrideFilter extends HttpServletRequestWrapper {
protected static final Logger log = LoggerFactory.getLogger(HeaderOverrideFilter.class);
Map<String, List<String>> headerOverrides = new HashMap<>();
public HeaderOverrideFilter(ServletRequest request, Map<String, List<String>> overrides) {
super((HttpServletRequest) request);
this.headerOverrides = overrides;
}
@Override
public String getHeader(String name) {
if (headerOverrides.containsKey(name)) {
return headerOverrides.get(name).get(0);
} else {
return super.getHeader(name);
}
}
@Override
public Enumeration<String> getHeaders(String name) {
List<String> headers = new ArrayList<>();
try {
headers.addAll(Collections.list(super.getHeaders(name)));
if (headerOverrides.containsKey(name)){
headers.addAll(headerOverrides.get(name));
}
} catch (Throwable t){
log.error("Error caught", t);
throw t;
}
return Collections.enumeration(headers);
}
@Override
public Enumeration<String> getHeaderNames() {
List<String> headers = new ArrayList<>();
headers.addAll(headerOverrides.keySet());
headers.addAll(Collections.list(super.getHeaderNames()));
return Collections.enumeration(headers);
}
public static Filter buildForwardedHeadersFilter(URI uri) {
return buildForwardedHeadersFilter(uri.getScheme(), uri.getHost(), uri.getPort(), uri.getPath());
}
public static Filter buildForwardedHeadersFilter(String protocol, String hostname, Integer port, String path) {
Map<String, List<String>> headers = new HashMap<>();
if (hasText(protocol)){
headers.put("X-Forwarded-Proto", Arrays.asList(protocol));
}
if (hasText(hostname)){
headers.put("X-Forwarded-Host", Arrays.asList(hostname));
}
if (port != null){
headers.put("X-Forwarded-Port", Arrays.asList(String.valueOf(port)));
}
if (hasText(path)){
headers.put("X-Forwarded-Path", Arrays.asList(path));
}
return buildHeaderOverridesFilter(headers);
}
public static Filter buildHeaderOverridesFilter(Map<String, List<String>> headerOverrides) {
return new OncePerRequestFilter() {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain
filterChain) throws ServletException, IOException {
log.debug("Added HeaderOverrideFilter request wrapper.");
filterChain.doFilter(new HeaderOverrideFilter(request, headerOverrides), response);
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment