Spring从反向代理获取实际方案

Spring从反向代理获取实际方案,spring,redirect,nginx,https,reverse-proxy,Spring,Redirect,Nginx,Https,Reverse Proxy,我有一个SpringBootWeb应用程序在Widlfly服务器上运行。我通过生成一个链接到Facebook登录端点的“使用Facebook登录”按钮来实现Facebook OAuth登录 https://www.facebook.com/v2.5/dialog/oauth?client_id=****&response_type=code&redirect_uri=http://example.com/login/facebook 我使用以下java代码生成重定向uri的值

我有一个SpringBootWeb应用程序在Widlfly服务器上运行。我通过生成一个链接到Facebook登录端点的“使用Facebook登录”按钮来实现Facebook OAuth登录

https://www.facebook.com/v2.5/dialog/oauth?client_id=****&response_type=code&redirect_uri=http://example.com/login/facebook
我使用以下java代码生成重定向uri的值

public static String getFacebookRedirectUrl() {
    RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
    if (attrs instanceof ServletRequestAttributes) {
        HttpServletRequest request = ((ServletRequestAttributes) attrs).getRequest();
        return request.getScheme() + "://"
                + request.getServerName() + ":"
                + request.getServerPort()
                + request.getContextPath()
                + "/login/facebook";
    } else {
        throw new RuntimeException("Cannot determine facebook oauth redirect url");
    }
}
我的网站在内部部署到
http://my-ip-address:8080
并具有反向代理(nginx)转发来自
https://example.com
http://my-ip-address:8080

问题是
重定向uri总是作为
http://example.com/login/facebook
而不是
https://example.com/login/facebook
(非
https

请帮助建议当用户通过
https
访问网站时,make
request.getScheme()
如何正确返回
https
。以下是反向代理配置
/etc/nginx/sites enalbed/mysite.com

server {
    listen 80;
    listen 443;
    server_name example.com www.example.com;

    ssl on;
    ssl_certificate         /etc/nginx/cert.crt;
    ssl_certificate_key     /etc/nginx/cert.key;


    location / {
        proxy_pass http://my-ip-address:8080;
        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;
    }
}
request.getScheme()
将始终是http,因为您是通过http代理的,但您是在头中传递请求方案,因此请使用该选项

return request.getScheme()+“:/”
更改为
return request.getHeader('X-Forwarded-Proto')+“:/”


不确定java如何解释标题,因此
X-Forwarded-Proto
可能会变成
X\u-Forwarded\u-Proto
xForwardedProto
或其他东西,但您明白了…

谢谢您的建议。不幸的是,我不能依赖头,因为它是特定于反向代理引擎的。我在测试环境中使用Nginx,但我无法控制生产环境和其他测试环境。