【问题标题】:Modify headers of proxied request修改代理请求的标头
【发布时间】:2013-06-18 17:08:18
【问题描述】:

我正在限制与经过身份验证的第三方 API 交互的纯客户端 COR 演示应用程序的 IP。我有一个“中间件”服务器正在运行,我用它来代理从 CORs 应用到第三方 API 的请求,但是我无法将基本身份验证凭据注入这些代理请求。 p>

isAllowed = (req, res, next) -> # Do IP check here.

base64Encode = (unencoded) -> new Buffer(unencoded or '').toString 'base64'

app.all "/demoproxy/*", isAllowed, (req, res) ->

  req.url = "/" + req.url.split("/").slice(2).join("/")

  userPass = base64Encode "#{process.env.DEMO_USERNAME}:#{process.env.DEMO_PASSWORD}"

   # This doesn't work.
   # res.setHeader 'Authorization',  "Basic #{userPass}"

   # This doesn't work either.
   ###res.oldWriteHead = res.writeHead

   res.writeHead = (statusCode, headers) ->

     headers = { }
     headers['Authorization'] = "Basic #{userPass}"
     res.oldWriteHead statusCode, headers###

    proxy = new httpProxy.HttpProxy
      target:
        host: 'remote-api.com'
        port: 80

    proxy.proxyRequest req, res

这样做的正确方法是什么?

【问题讨论】:

    标签: node.js http proxy express http-proxy


    【解决方案1】:

    在这种情况下,我认为您想在请求 (req) 对象上设置授权标头,而不是响应 (res)。如果需要对 remote-api.com 进行身份验证,那么它需要知道您发送给它的请求。也许在发出proxy.proxyRequest 请求之前尝试以下操作

    req.headers["authorization"] = "Basic #{userPass}"
    

    req 对象没有 setHeader 函数,headers 属性只是一个 javascript 对象/映射。希望对您有所帮助...

    【讨论】:

    • 所以,我试过了。但它与其他基本身份验证不同吗?更新的问题。
    • 您是否也尝试过设置 req.headers['authorization'] ?根据节点 HTTP 文档 (nodejs.org/api/http.html#http_message_headers),标头对象都是小写的,所以如果在设置大写标头时它不起作用,那么代理可能只查看小写版本。
    【解决方案2】:

    以下是一些适合我的代码,例如:

    # Demo server requiring basic authentication
    servAuth = require("http").createServer (req, res) ->
      if auth = req.headers?.authorization
        res.statusCode = 200
        res.end "Good job, you sent '#{auth}'"
      else
        res.statusCode = 401
        res.end "How about you authenticate first?"
    servAuth.listen(8090)
    
    # Proxy server which checks the IP address and then proxies the request
    servProxy = require("http-proxy").createServer (req, res, proxy) ->
      checkIP req, (err, isOK) ->
        # something wrong happened even with the IP address checking
        if err
          res.statusCode = 500
          res.end "Sorry, everything got fargled", "ascii"
        # IP address not allowed
        else if not isOK
          res.statusCode = 403
          res.end "You ain't from around here, are you?", "ascii"
        # all good, proxy the request with basic auth added
        else
          userPass = new Buffer("#{process.env.USERNAME}:#{process.env.PASSWORD}", "ascii")
          userPass = userPass.toString("base64")
          req.headers.authorization = "Basic #{userPass}"
          proxy.proxyRequest req, res, {
            host: "localhost"
            port: 8090
          }
    servProxy.listen(8080)
    
    # asynchronous IP address checking
    checkIP = (req, done) ->
      # TODO: implement whatever custom IP checking
      # this example just says everything is OK
      done( null, true )
    

    【讨论】:

      猜你喜欢
      • 2011-03-22
      • 2015-09-11
      • 2017-07-21
      • 2014-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-27
      • 2015-08-22
      相关资源
      最近更新 更多