[英]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.