Rails 3.0.12,最新的omniauth,我可以连接到谷歌并获得用户的电子邮件地址就好了.但是我在SSL模式下在Nginx后面运行相同的rails应用程序,并且它在Google页面中失败:
"The page you requested is invalid."
这是我的Nginx配置吗?我的omniauth设置?
我知道X-Forwarded-Proto:https是这里的特殊酱,我还需要做些什么才能在SSL网络服务器后面获得开心的快乐吗?
下面是完整的示例代码:您可以克隆此repo,捆绑安装,并运行rails以查看它是否正常工作,然后运行rake服务器以查看它是否失败.
https://github.com/jjulian/open_id_ssl
Nginx.conf:
worker_processes 2;
pid tmp/Nginx.pid;
error_log log/error.log;
daemon off;
events {
}
http {
client_body_temp_path tmp/body;
proxy_temp_path tmp/proxy;
fastcgi_temp_path tmp/fastcgi;
uwsgi_temp_path tmp/uwsgi;
scgi_temp_path tmp/scgi;
server {
listen 3000 ssl;
ssl_certificate development.crt;
ssl_certificate_key development.key;
ssl_verify_depth 6;
access_log log/access.log;
proxy_buffering off;
location / {
proxy_pass http://127.0.0.1:3300;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Forwarded-Proto https;
}
}
}
omniauth.rb初始化程序:
require 'openid/store/filesystem'
Rails.application.config.middleware.use OmniAuth::Builder do
provider :open_id,:identifier => 'https://www.google.com/accounts/o8/id'
end
routes.rb中:
OpenIdSsl::Application.routes.draw do
match '/auth/open_id/callback' => 'accounts#update'
match '/auth/failure' => 'accounts#failure'
root :to => 'accounts#show'
end
更新:此示例使用Rails 3.1.12和OmniAuth 1.0.3.升级到Rails 3.1.4和OmniAuth 1.1.0修复了这个问题.
最佳答案
找到你的问题,我仍然想找到更干净的东西,但这里是快速的&脏修复:
原文链接:https://www.f2er.com/nginx/434465.html在config / initializers / omniauth.rb中添加:
class Rack::OpenID
def realm_url(req)
'https://localhost:3000'
end
end
现在解释一下:当rack-openid gem构建发送到google openid服务器的请求时,它使用rails应用程序访问URL而不是Nginx(使用ssl)在一个地方失败,导致这被发送到openid服务器:
openid.realm:http://localhost:3001
openid.return_to:https://localhost:3001/auth/open_id/callback
领域使用http url(rails url),而return_to指向正确的https url(Nginx),当openid服务器看到它停止并返回错误时.
PS:如果我找到一个更清洁的方法,我会编辑答案.