request.getScheme() is returning http instead of returning https in java

后端 未结 3 1254
太阳男子
太阳男子 2021-02-05 18:05
function demo(request,response){
        request.getScheme() is returning http instead of returning https.
        System.out.println(\"\"+request.getScheme());
}


        
相关标签:
3条回答
  • 2021-02-05 18:21

    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:

    HttpRequest httpRequest = new ServletServerHttpRequest(request); //request is HttpServletRequest
    UriComponents uriComponents = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    
    String scheme = uriComponents.getScheme(); // http/https
    
    0 讨论(0)
  • 2021-02-05 18:23

    I used to have a similar problem with getScheme()

    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    

    I've solved using "//" instead:

    String basePath = "//"+request.getServerName()+":"+request.getServerPort()+path+"/";
    
    0 讨论(0)
  • 2021-02-05 18:29

    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.

    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.

    0 讨论(0)
提交回复
热议问题