Spring从反向代理获取实际方案
我有一个SpringBootWeb应用程序在Widlfly服务器上运行。我通过生成一个链接到Facebook登录端点的“使用Facebook登录”按钮来实现Facebook OAuth登录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的值
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
访问网站时,makerequest.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,但我无法控制生产环境和其他测试环境。