2014-09-02 57 views
0

我在Rails中使用rack-proxy gem將代理請求代理到外部服務器。事情是,外部端點需要認證。我如何從中間件提供這些信息?在Rails代理中間件中添加身份驗證信息

這是我到目前爲止有:

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 

回答

1

要看是什麼樣的身份驗證的其他服務器使用。如果它只是普通的HTTP認證,你可以這樣做:

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

當值部分遵循的規範:http://en.wikipedia.org/wiki/Basic_access_authentication#cite_ref-8

+0

有沒有這些東西的任何文件?我甚至不確定我是否正在用'PATH_INFO'做正確的事情 – manojlds 2014-09-02 18:12:41