request.getScheme() 正在返回 http 而不是在 java 中返回 https
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25911469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
request.getScheme() is returning http instead of returning https in java
提问by Sumit
function demo(request,response){
request.getScheme() is returning http instead of returning https.
System.out.println(""+request.getScheme());
}
output:http
输出:http
--above function demo is being called from main method but it prints http instead it should print https while working on internet server.
--above 函数演示正在从 main 方法调用,但它打印 http 而它应该在 Internet 服务器上工作时打印 https。
回答by jonnybot
See the answer https://stackoverflow.com/a/19599143/1524502, and note the issues about being behind a reverse proxy or load balancer. Most likely, that is your problem.
请参阅答案https://stackoverflow.com/a/19599143/1524502,并注意有关落后于反向代理或负载均衡器的问题。最有可能的是,那是你的问题。
The answerer in that question recommended using
该问题的回答者建议使用
request.getHeader("x-forwarded-proto")
instead, though that is dependent on your load balancer setting the header correctly.
相反,尽管这取决于您的负载均衡器正确设置标头。
回答by Mamun Sardar
If your server is running behind a proxy server, make sure your proxy header is set:
如果您的服务器在代理服务器后面运行,请确保设置了代理标头:
proxy_set_header X-Forwarded-Proto $scheme;
Then to get the right scheme
you can use springframework's classes:
然后为了得到正确的scheme
你可以使用 springframework 的类:
HttpRequest httpRequest = new ServletServerHttpRequest(request); //request is HttpServletRequest
UriComponents uriComponents = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
String scheme = uriComponents.getScheme(); // http/https
回答by El Atendedor
I used to have a similar problem with getScheme()
我曾经对 getScheme() 有类似的问题
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
I've solved using "//" instead:
我已经使用“//”解决了:
String basePath = "//"+request.getServerName()+":"+request.getServerPort()+path+"/";