在使用smiley-http-proxy-servlet实现反向代理时,发现在进行转发时原始request中的cookie信息丢失了。翻看了其源代码后发现在ProxyServlet进行http转发时是有copy原始request中的header(包括cookie)的操作的,如下:
/** Copy request headers from the servlet client to the proxy request. */
protected void copyRequestHeaders(HttpServletRequest servletRequest, HttpRequest proxyRequest) {
// Get an Enumeration of all of the header names sent by the client
Enumeration enumerationOfHeaderNames = servletRequest.getHeaderNames();
while (enumerationOfHeaderNames.hasMoreElements()) {
String headerName = (String) enumerationOfHeaderNames.nextElement();
//Instead the content-length is effectively set via InputStreamEntity
if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH))
continue;
if (hopByHopHeaders.containsHeader(headerName))
continue;
Enumeration headers = servletRequest.getHeaders(headerName);
while (headers.hasMoreElements()) {//sometimes more than one value
String headerValue = (String) headers.nextElement();
// In case the proxy host is running multiple virtual servers,
// rewrite the Host header to ensure that we get content from
// the correct virtual server
if (headerName.equalsIgnoreCase(HttpHeaders.HOST)) {
HttpHost host = getTargetHost(servletRequest);
headerValue = host.getHostName();
if (host.getPort() != -1)
headerValue += ":"+host.getPort();
// 就是下面这句copy了cookie
} else if (headerName.equalsIgnoreCase(org.apache.http.cookie.SM.COOKIE)) {
headerValue = getRealCookie(headerValue);
}
proxyRequest.addHeader(headerName, headerValue);
}
}
}
但是在进行copy时做了限定——只copy name为特定前缀的cookie,该方法代码如下:
/** The string prefixing rewritten cookies. */ protected String getCookieNamePrefix() { return "!Proxy!" + getServletConfig().getServletName(); }
而原始的cookie均不是该前缀,因此出现了cookie丢失的问题。找到问题原因之后,要修改就容易了很多。要么将原始cookie name追加相应的前缀,要么实现一个URITemplateProxyServlet的子类,覆写getCookieNamePrefix方法即可。
相关推荐
java运行依赖jar包
java运行依赖jar包
Smiley的HTTP代理Servlet 这是Java Servlet形式的HTTP代理(也称为网关)。 HTTP代理对于AJAX应用程序与托管Web应用程序以外的主机上的Web可访问服务进行通信很有用。 它是反向代理,实际上不是转发代理,尽管...
Spring Boot 使用 ProxyServlet 代理并统一响应,详见 https://blog.csdn.net/z1353095373/article/details/125005408
smileys-proxy-servlet 使用Smiley的库( )的Java http代理servlet的简单实现使用Idea Maven框架支持构建项目
在做银联二维码支付(银行卡支付有一整套的请求响应流程),用于接收银联返回的支付结果的通知.
主要介绍了Spring boot2X负载均衡和反向代理实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
smiley-sans-v1.1.1.zip
smiley-sans-v1.0.0.zip
Zuul与紧密,Smiley的HTTP代理Servlet很简单,没有高级功能。 Charon是通用的Spring Boot工具。 它已经实现了许多功能,其体系结构提供了添加新功能的简便方法。产品特点高度可配置和可扩展Spring 和支持多请求转发...
smiley, python 应用程序跟踪程序 smiley----python-应用程序跟踪程序Smiley包括几个运行 python 程序和监视记录和报告的内部细节的子命令。 安装和使用 有关详细信息,请参阅 https://smiley.readthedocs.org
$ cd smiley $ sudo npm -g install grunt-cli karma bower $ npm install $ bower install $ grunt watch 最后,在浏览器中打开file:///path/to/smiley/build/index.html 。 祝您黑客愉快! 目的 ngBoilerplate...
PHP Smiley 是处理文本中笑脸的简单支持! 通过一个简单的函数调用自动转换所有笑脸代码,如:)!
Diese Seite bei 教程 迪斯(Dieses Projekt)Bearbeiten Um在MakeCode zu bearbeiten中死亡存储库。 öffnehttps //makecode.microbit.org/ klicke auf Importieren和dann auf Importiere URL ...
动画笑脸评级动画的客户反馈和评分界面 :page_with_curl: 执照版权所有2021 Aiman Muzafar 根据Apache许可证2.0版(“许可证”)获得许可; 除非遵守许可,否则您不得使用此文件。 您可以在以下位置获得许可证的副本...
扩展名附属代码Unicode和笑脸Unicode。 简单的扩展名将其代码编码为Unicode或微笑符号为Unicode,但不包括加号复印机/折页机。 支持语言:Français
Smiley
在打开此页面 讲解 编辑这个专案 要在MakeCode中编辑此存储库。 打开 单击导入,然后单击导入URL 粘贴并单击导入 方块预览 此图显示了master中最后一次提交的块代码。 该图像可能需要几分钟才能刷新。...
笑脸 创建用R绘制的笑脸的动画gif 博客文章: :