为什么request.getRequestURL()返回非https url?

use*_*882 5 jsf servlets

在我们的一个项目中,我们仍然必须使用JSF 1.2 + Tomcat 6,问题是当我向https服务器发送-request并尝试在托管bean中获取请求的URL时,如下所示:

ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest request = (HttpServletRequest)context.getRequest();
String url = request.getRequestURL().toString()
Run Code Online (Sandbox Code Playgroud)

发送请求的按钮只是一个提交按钮,如下所示:

<h:form id="contactform">
    <h:commandButton id="submit" action="#{forgotPasswordBean.doSend}"
 </h:form>
Run Code Online (Sandbox Code Playgroud)

我得到了http基于URL的intead https.在Web浏览器的调试面板中,我确保https实际发送了一个-request,但是URL包含一个只是http请求的链接.什么问题或者只是一个错误?

Ale*_*lex 11

如果您在应用程序前面有一个负载均衡器,则会发生此行为.即使请求是在HTTPS中完成的,负载均衡器也会将它们重新发布为产生此行为的纯http请求.

一个例子是使用GAE(Google App Engine).您可以使用HTTPS端点(https://my-app.appspot.com),但您的应用将继续接收HTTP中的所有请求.

@ user3663882在批准的答案的评论中指出了这一点.


Kon*_*kov 3

其中HttpServletRequest#getRequestUrl()包含协议、服务器名称、端口号和服务器路径,即https如果连接实际上是安全的并且在 HTTP 下,则它应该包含。

但是,这并不是确定连接是否安全的唯一方法。该ServelRequest接口定义了另外两个选项(ServletRequest#getScheme()ServletRequest#isSecure())来检测请求是否安全:

String scheme = request.getScheme(); //will return "https" when connection is secured
//or
boolean isSecured = request.isSecure(); //will return true when connection is secured
Run Code Online (Sandbox Code Playgroud)

更多信息:

  • 实际上,问题在于我们有一个发送纯 http 请求的负载平衡器。 (5认同)