如何解决反向代理:从 Java Web 应用程序访问 .Net Web 应用程序
这里是我们已经/想要实现的场景:
- 我们在 apache tomcat 上部署了一个基于 java spring 的 Web 应用程序。
- 在我们的网络应用程序中,我们将向用户提供基于 .net 的 3rd 方网络应用程序链接。单击哪个用户将被重定向到 .net 应用程序,但浏览器 url 中显示的域和上下文根将保持与 java 应用程序相同。
- 我们不能要求第 3 方团队更改其申请中的任何内容。
- 无论我们需要更改什么,都应该在我们的代码中,而且我们应该尽可能避免任何 apache tomcat 级别的更改。
以下是我们目前实施的内容:
- 我们尝试使用以下链接实现反向代理:https://github.com/mitre/HTTP-Proxy-Servlet
- 因此,基本原理是将第 3 方 url 作为网络服务调用并编辑 httpresponse 并将我们的域和上下文路径添加到作为响应接收的任何文件中的 url。虽然这不是一个好的解决方案,而且会占用大量的时间和空间,但它确实可以做到。
我们面临的问题:
- 除了 .net 应用程序中的 ajax 请求外,一切正常。
- 抛出的错误是:Sys.WebForms.PageRequestManagerParserErrorException:无法解析从服务器收到的消息。此错误的常见原因是通过调用 Response.Write()、响应过滤器、HttpModules 或启用服务器跟踪来修改响应。
这里是做httpresponse修改任务的代码片段:
protected void copyResponseEntity(HttpResponse proxyResponse,HttpServletResponse servletResponse,HttpRequest proxyRequest,HttpServletRequest servletRequest)throws IOException {
HttpEntity entity = new BufferedHttpEntity(proxyResponse.getEntity());
if (entity.isChunked()) {
InputStream is = entity.getContent();
String proxyBody = EntityUtils.toString(entity);
proxyBody = proxyBody.replaceAll("/.netContextRoot/","/ourContextRoot/.netContextRoot/");
InputStream stream = new ByteArrayInputStream(proxyBody.getBytes(StandardCharsets.UTF_8));
OutputStream os = servletResponse.getOutputStream();
byte[] buffer = new byte[10 * 1024];
int read;
while ((read = stream.read(buffer)) != -1) {
os.write(buffer,read);
if (doHandleCompression || stream.available() == 0 /* next is.read will block */) {
os.flush();
}
}
// Entity closing/cleanup is done in the caller (#service)
} else {
String proxyBody = EntityUtils.toString(entity);
proxyBody = proxyBody.replaceAll("/.netContextRoot/","/ourContextRoot/.netContextRoot/");
EntityUtils.updateEntity(proxyResponse,new StringEntity(proxyBody));
HttpEntity entity2 = proxyResponse.getEntity();
OutputStream servletOutputStream = servletResponse.getOutputStream();
entity2.writeTo(servletOutputStream);
}
}
任何人都可以帮助我们解决这种情况,另外,如果您有任何其他解决方案而不对 apache 级别进行任何更改,那么请提及。
提前致谢。
解决方法
我终于找到了一些避免 PageRequestManagerParserErrorException 的答案。
- You can refer this link if you are allowed to make changes in 3rd party application:请阅读评论以更好地理解。
- 您可以创建一个仅用于反向代理的新应用程序,其上下文根名称与第 3 方的上下文根相同,并将其部署在您的应用程序域中,这样您就不必编辑 http 响应的正文内容来设置您的应用程序小路。然后您可以从旧的应用程序调用此应用程序,因为两者都在同一个域中。
- 我确实在 PageRequestManagerParserErrorException 中挖掘了一点,以及一些如何找到流程的方法,当知道您已经编辑了 httpresponse 时,.net 的事件验证会触发 PageRequestManagerParserErrorException 并且他们使用的逻辑是他们只是用httpresponse的实际长度检查httpresponse的长度。因此,如果您以某种方式设法在不更改响应长度的情况下编辑 httpresponse,那么您就完成了任务。
注意:
a) You can use this link to achieve reverse proxy:由于这只有代码级别的更改,因此实现起来非常简单,而且不需要服务器级别的配置更改。
b) PageRequestManagerParserErrorException 当您在将 httpResponse 发送到客户端之前编辑它时发生 PageRequestManagerParserErrorException,并且它由 .net 的 eventValidation 触发。
c) 如果您不允许像我的场景一样在 3rd 方应用程序中进行任何更改,则第一种解决方案是无用的。暂时我选择了第三个,因为它毫不费力地完成了这项工作。
,这最好作为 Servlet 上的过滤器来完成。 Filter API 允许您插入一些有关处理请求的逻辑,但它应该与处理请求的方式无关。 API 定义了一个巧妙的过滤器链,它就像一堆请求事件,每个事件都独立地处理响应。过滤器可以放置在链中的任何点,它可以在使用 doFilter()
方法执行时观察请求和响应。您应该能够检测第 3 方请求并将其包装在过滤器中,而无需知道其实现。
这里的常用方法是编写请求或响应包装器,以访问底层流的读写方法。我已经修改了 this example 中的示例以满足您的需要:
public class ThirdPartyRoutingResponseWrapper extends HttpServletResponseWrapper {
private CharArrayWriter writer;
private boolean _isThirdParty;
public ThirdPartyRoutingResponseWrapper (HttpServletResponse response,boolean isThirdParty) {
super(response);
writer = new CharArrayWriter();
_isThirdParty=isThirdParty;
}
public PrintWriter getWriter() {
return new PrintWriter(writer);
}
public String toString() {
if(_isThirdParty){
return writer.toString().replaceAll("/.netContextRoot/","/ourContextRoot/.netContextRoot/");
}
else {
return writer.toString();
}
}
}
如果在构造函数中设置了 isThirdParty
标志,此响应包装器将替换响应的内容。然后过滤器在调用 doFilter
将包装器添加到链时设置标志:
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws ServletException,IOException {
//decide if the request is routable:
boolean needsRoute = ((HttpServletRequest) request).getRequestURI().startsWith("/.netContextRoot/") ) ;
// execute the request with the wrapper configured for this response
chain.doFilter(request,new ThirdPartyRoutingResponseWrapper ( response,needsRoute ));
}
只要您的第三方请求在链上的较低过滤器中处理,这应该允许拦截和修改响应。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。