简体   繁体   English

在Rails代理中间件中添加身份验证信息

[英]Adding authentication info in Rails proxy middleware

I am using the rack-proxy gem in Rails to proxy requests to an external server. 我在Rails中使用rack-proxy gem将请求代理到外部服务器。 Thing is, the external endpoint requires authentication. 事实是,外部端点需要身份验证。 How do I provide that information from the middleware? 如何从中间件提供该信息?

Here's what I have so far: 这是我到目前为止的内容:

require 'rack/proxy'

class MyProxy < Rack::Proxy
  MY_REQUEST = %r{^/path/(.*)}

  def initialize(app)
    @app = app
  end

  def call(env)
    if m = MY_REQUEST.match(env['PATH_INFO'])
      env['PATH_INFO'] = "https://otherserver.org/#{m[1]}"
      env['HTTP_HOST'] = "otherserver.org"
      #the otherserver.org endpoint requires authentication
      super env
    else
      @app.call(env)
    end
  end
end

Depends on what kind of authentication the other server is using. 取决于另一台服务器正在使用哪种身份验证。 If its just plain HTTP Authentication you can do something like: 如果只是普通的HTTP身份验证,则可以执行以下操作:

env['Authentication'] = 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='

Where the value part follows the spec at: http://en.wikipedia.org/wiki/Basic_access_authentication#cite_ref-8 值部分遵循规范的位置: http : //en.wikipedia.org/wiki/Basic_access_authentication#cite_ref-8

Following the Rack spec here gave me good pointers - http://rubydoc.info/github/rack/rack/master/file/SPEC 遵循Rack规范,这里给了我很好的指导-http: //rubydoc.info/github/rack/rack/master/file/SPEC

Had to do: 不得不做:

env['HTTP_AUTHORIZATION'] = 'Basic <base64 username:password>'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails:将Sidekiq防反跳添加到中间件链 - Rails: Adding Sidekiq Debounce to Middleware Chain 当nginx用作代理时,为什么Rails中间件开销如此之高? - Why is Rails middleware overhead so high when nginx is used as a proxy? 将Facebook身份验证添加到Rails自定义身份验证 - Adding Facebook Authentication to Rails Custom Authentication Rails:Watir 中的代理身份验证(Chrome 驱动程序) - Rails: Proxy Authentication in Watir (Chrome Driver) Ruby on Rails:如何从机架中间件身份验证中排除某些路径? - Ruby on rails: how to exclude certain path from rack middleware authentication? 为什么添加这样的 Rails 中间件会导致无休止的重定向? - Why is adding a Rails middleware like this causing endless redirects? 添加 &#39;SameSite=None;&#39; 通过 Rack 中间件到 Rails 的 cookie? - Adding 'SameSite=None;' cookies to Rails via Rack middleware? 在Rails中使用restful_authentication插件更新用户信息? - Update User Info with restful_authentication plugin in Rails? 如何在Wordpress网站和Rails应用程序之间共享身份验证信息? - How to share authentication info between wordpress site and a rails app? 在Rails API软件包中添加管理员身份验证 - Adding Admin authentication in rails API packages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM