java getscheme_request.getScheme() 取到https正确的协议(转载)
发布日期:2021-06-24 13:23:59 浏览次数:2 分类:技术文章

本文共 3191 字,大约阅读时间需要 10 分钟。

最近在做一个项目, 架构上使用了 Nginx +tomcat 集群, 且nginx下配置了SSL,tomcat no SSL,项目使用https协议

4aee9a3db949a59a68f3ae37a6f183fa.png

但是,明明是https url请求,发现 log里面,

0428 15:55:55 INFO (PaymentInterceptor.java:44) preHandle() -requestStringForLog: {"request.getRequestURL():": "http://trade.feilong.com/payment/paymentChannel?id=212&s=a84485e0985afe97fffd7fd7741c93851d83a4f6","request.getMethod:": "GET","_parameterMap": {"id": ["212"],"s": ["a84485e0985afe97fffd7fd7741c93851d83a4f6"]

}

}

request.getRequestURL() 输出出来的 一直是  http://trade.feilong.com/payment/paymentChannel?id=212&s=a84485e0985afe97fffd7fd7741c93851d83a4f6

但是浏览器中的URL却是 https://trade.feilong.com/payment/paymentChannel?id=212&s=a84485e0985afe97fffd7fd7741c93851d83a4f6

瞬间要颠覆我的Java观

cc7bcbc49cc06ba937dc3d75715cc2dc.gif,API上写得很清楚:

getRequestURL():

Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.

也就是说, getRequestURL() 输出的是不带query string的路经(含协议 端口 server path等信息).

b480c161a5daeca96be5c38c2545178e.png

并且,还发现

request.getScheme() //总是 http,而不是实际的http或https

request.isSecure() //总是false(因为总是http)

request.getRemoteAddr() //总是 nginx 请求的 IP,而不是用户的IP

request.getRequestURL() //总是 nginx 请求的URL 而不是用户实际请求的 URL

response.sendRedirect( 相对url ) //总是重定向到 http 上 (因为认为当前是 http 请求)

查阅了一些资料,找到了解决方案:

解决方法很简单,只需要分别配置一下 Nginx 和 Tomcat 就好了,而不用改程序。

配置 Nginx 的转发选项:

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header X-Forwarded-Proto $scheme;

proxy_set_header X-Forwarded-Proto $scheme;

配置Tomcat server.xml 的 Engine 模块下配置一个 Valve:

配置双方的 X-Forwarded-Proto 就是为了正确地识别实际用户发出的协议是 http 还是 https。

这样以上5项测试就都变为正确的结果了,就像用户在直接访问 Tomcat 一样。

关于 RemoteIpValve,有兴趣的同学可以阅读下 doc

Tomcat port of mod_remoteip, this valve replaces the apparent client remote IP address and hostname for the request with the IP address list presented by a proxy or a load balancer via a request headers (e.g. "X-Forwarded-For").

Another feature ofthis valve is to replace the apparent scheme (http/https) and server port with the scheme presented by a proxy or a load balancer via a request header (e.g. "X-Forwarded-Proto").

看了下他们的源码,比较简单,在各种框架,各种算法面前,这个类对性能影响很小

如果没有配置protocolHeader 属性, 什么都不做.

如果配置了protocolHeader,但是request.getHeader(protocolHeader)取出来的值是null,什么都不做

如果配置了protocolHeader,但是request.getHeader(protocolHeader)取出来的值(忽略大小写)是 配置的protocolHeaderHttpsValue(默认https),scheme设置为https,端口设置 为 httpsServerPort

其他设置为 http

if (protocolHeader != null) {

String protocolHeaderValue=request.getHeader(protocolHeader);if (protocolHeaderValue == null) {//don't modify the secure,scheme and serverPort attributes//of the request

} else if(protocolHeaderHttpsValue.equalsIgnoreCase(protocolHeaderValue)) {

request.setSecure(true);//use request.coyoteRequest.scheme instead of request.setScheme() because request.setScheme() is no-op in Tomcat 6.0

request.getCoyoteRequest().scheme().setString("https");

request.setServerPort(httpsServerPort);

}else{

request.setSecure(false);//use request.coyoteRequest.scheme instead of request.setScheme() because request.setScheme() is no-op in Tomcat 6.0

request.getCoyoteRequest().scheme().setString("http");

request.setServerPort(httpServerPort);

}

}

转载地址:https://blog.csdn.net/weixin_33137081/article/details/114118866 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:50道java经典练习题_Java基础50道经典练习题(7)——处理字符串
下一篇:java 读取本地txt文件_java实现读取本地txt文件(行政区划制作层级标记表格)

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月26日 11时30分53秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

想花钱速学互联网行业,大概花两三个月的时间,出来好找工作吗 2019-04-28
为何没有中文编程? 2019-04-28
招聘信息薪资范围是12-20K,能否要20K的薪资? 2019-04-28
软件外包商都是黑心的吗? 2019-04-28
Vue @import ‘~@/css/reset.css’;报错,解决方案 2019-04-28
Vue安装支持SCSS插件 2019-04-28
webstorm设置点击(单击)左侧项目资源管理器里面的文件,自动在右侧打开源代码文件 2019-04-28
360浏览器如何设置为像chrome谷歌浏览器那样输入网址回车在当前页面,而不是新建一个标签打开 2019-04-28
【记录】我在团队合作中遇到过的胎神(扑街仔)级别前端小伙伴 之 莫名其妙配置0.0.0.0这种IP访问 2019-04-28
【经典】全局公共scss文件的引入使用 2019-04-28
【抬杠】在某些时候不希望用户缩小浏览器的宽度,因为咳咳~会导致你的布局混乱,那么这个代码就是帮助你如何限制浏览器宽度的 2019-04-28
喂!讲真~我很讨厌chrome谷歌浏览器的默认填充输入框input样式咧,敲击讨厌滴啦,怎么去掉介个样式尼??? 2019-04-28
【原生基础版】js原生实现拖拽效果,注意不要忘了div的cursor用grab和grabbing(还是古法炮制、传统工艺的原生代码兼容性最好,推荐!!!) 2019-04-28
【加强版】js原生实现拖拽效果,这次没有用document的mousedown、mousemove、mouseup事件,我们来点干货! 2019-04-28
如何用bat文件快速计算项目代码行数 2019-04-28
《你的背包》歪唱 2019-04-28
散文《犯贱》 2019-04-28
【老司机开车了】CSS3实现“微信拍一拍头像抖动效果” 2019-04-28
如何在本地安装tomcat、jdk并且配置tomcat环境变量(window7) 2019-04-28
修改IDEA项目的JDK应用路径 2019-04-28