Package io.hawt.web.proxy
Class ProxyServlet
java.lang.Object
jakarta.servlet.GenericServlet
jakarta.servlet.http.HttpServlet
io.hawt.web.proxy.ProxyServlet
- All Implemented Interfaces:
jakarta.servlet.Servlet,jakarta.servlet.ServletConfig,Serializable
public class ProxyServlet
extends jakarta.servlet.http.HttpServlet
An HTTP reverse proxy/gateway servlet. It is designed to be extended for customization
if desired. Most of the work is handled by
Apache HttpClient.
There are alternatives to a servlet based proxy such as Apache mod_proxy if that is available to you. However this servlet is easily customizable by Java, secure-able by your web application's security (e.g. spring-security), portable across servlet engines, and is embeddable into another web application.
Inspiration: http://httpd.apache.org/docs/2.0/mod/mod_proxy.html
Original implementation at https://github.com/mitre/HTTP-Proxy-Servlet, released under ASL 2.0.
- Author:
- David Smiley dsmiley@mitre.org
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected ProxyAllowlistprotected static final BitSetstatic final Stringprotected booleanprotected booleanstatic final Stringstatic final Stringstatic final Stringprotected static final org.apache.http.message.HeaderGroupThese are the "hop-by-hop" headers that should not be copied.static final Stringstatic final StringA boolean parameter name to enable forwarding of the client IPstatic final Stringprotected org.apache.http.impl.client.CloseableHttpClient -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcopyRequestHeaders(jakarta.servlet.http.HttpServletRequest servletRequest, org.apache.http.HttpRequest proxyRequest, URI targetUriObj) Copy request headers from the servlet client to the proxy request.protected voidcopyResponseEntity(org.apache.http.HttpResponse proxyResponse, jakarta.servlet.http.HttpServletResponse servletResponse) Copy response body data (the entity) from the proxy to the servlet client.protected voidcopyResponseHeaders(org.apache.http.HttpResponse proxyResponse, jakarta.servlet.http.HttpServletResponse servletResponse) Copy proxied response headers back to the servlet client.voiddestroy()protected booleandoResponseRedirectOrNotModifiedLogic(jakarta.servlet.http.HttpServletRequest servletRequest, jakarta.servlet.http.HttpServletResponse servletResponse, org.apache.http.HttpResponse proxyResponse, int statusCode, URI targetUriObj) voidinit(jakarta.servlet.ServletConfig servletConfig) protected ProxyAddressparseProxyAddress(jakarta.servlet.http.HttpServletRequest servletRequest) protected StringrewriteUrlFromResponse(jakarta.servlet.http.HttpServletRequest servletRequest, String theUrl, String targetUri) For a redirect response from the target server, this translatestheUrlto redirect to and translates it to one the original client can use.protected voidservice(jakarta.servlet.http.HttpServletRequest servletRequest, jakarta.servlet.http.HttpServletResponse servletResponse) Methods inherited from class jakarta.servlet.http.HttpServlet
doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, serviceMethods inherited from class jakarta.servlet.GenericServlet
getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletName, init, log, log
-
Field Details
-
P_FORWARDEDFOR
A boolean parameter name to enable forwarding of the client IP- See Also:
-
PROXY_ALLOWLIST
- See Also:
-
LOCAL_ADDRESS_PROBING
- See Also:
-
DISABLE_PROXY
- See Also:
-
HAWTIO_PROXY_ALLOWLIST
- See Also:
-
HAWTIO_LOCAL_ADDRESS_PROBING
- See Also:
-
HAWTIO_DISABLE_PROXY
- See Also:
-
enabled
protected boolean enabled -
doForwardIP
protected boolean doForwardIP -
acceptSelfSignedCerts
protected boolean acceptSelfSignedCerts -
allowlist
-
proxyClient
protected org.apache.http.impl.client.CloseableHttpClient proxyClient -
hopByHopHeaders
protected static final org.apache.http.message.HeaderGroup hopByHopHeadersThese are the "hop-by-hop" headers that should not be copied. http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html I use an HttpClient HeaderGroup class instead of Setbecause this approach does case-insensitive lookup faster. -
asciiQueryChars
-
-
Constructor Details
-
ProxyServlet
public ProxyServlet()
-
-
Method Details
-
getServletInfo
- Specified by:
getServletInfoin interfacejakarta.servlet.Servlet- Overrides:
getServletInfoin classjakarta.servlet.GenericServlet
-
init
public void init(jakarta.servlet.ServletConfig servletConfig) throws jakarta.servlet.ServletException - Specified by:
initin interfacejakarta.servlet.Servlet- Overrides:
initin classjakarta.servlet.GenericServlet- Throws:
jakarta.servlet.ServletException
-
destroy
public void destroy()- Specified by:
destroyin interfacejakarta.servlet.Servlet- Overrides:
destroyin classjakarta.servlet.GenericServlet
-
service
protected void service(jakarta.servlet.http.HttpServletRequest servletRequest, jakarta.servlet.http.HttpServletResponse servletResponse) throws IOException - Overrides:
servicein classjakarta.servlet.http.HttpServlet- Throws:
IOException
-
parseProxyAddress
-
doResponseRedirectOrNotModifiedLogic
protected boolean doResponseRedirectOrNotModifiedLogic(jakarta.servlet.http.HttpServletRequest servletRequest, jakarta.servlet.http.HttpServletResponse servletResponse, org.apache.http.HttpResponse proxyResponse, int statusCode, URI targetUriObj) throws jakarta.servlet.ServletException, IOException - Throws:
jakarta.servlet.ServletExceptionIOException
-
copyRequestHeaders
protected void copyRequestHeaders(jakarta.servlet.http.HttpServletRequest servletRequest, org.apache.http.HttpRequest proxyRequest, URI targetUriObj) Copy request headers from the servlet client to the proxy request. -
copyResponseHeaders
protected void copyResponseHeaders(org.apache.http.HttpResponse proxyResponse, jakarta.servlet.http.HttpServletResponse servletResponse) Copy proxied response headers back to the servlet client. -
copyResponseEntity
protected void copyResponseEntity(org.apache.http.HttpResponse proxyResponse, jakarta.servlet.http.HttpServletResponse servletResponse) throws IOException Copy response body data (the entity) from the proxy to the servlet client.- Throws:
IOException
-
rewriteUrlFromResponse
protected String rewriteUrlFromResponse(jakarta.servlet.http.HttpServletRequest servletRequest, String theUrl, String targetUri) For a redirect response from the target server, this translatestheUrlto redirect to and translates it to one the original client can use.
-