加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

ruby-on-rails – 在SSL模式下运行nginx后,Omniauth和open_id与

发布时间:2020-12-13 21:32:03 所属栏目:Nginx 来源:网络整理
导读:Rails 3.0.12,最新的omniauth,我可以连接到谷歌并获得用户的电子邮件地址就好了.但是我在SSL模式下在nginx后面运行相同的rails应用程序,并且它在Google页面中失败: "The page you requested is invalid." 这是我的nginx配置吗?我的omniauth设置? 我知道X-

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;
    }
  }
}

omn??iauth.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修复了这个问题.

最佳答案
找到你的问题,我仍然想找到更干净的东西,但这里是快速的&脏修复:

在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:如果我找到一个更清洁的方法,我会编辑答案.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读