在特定路由中禁用基本身份验证

在特定路由中禁用基本身份验证

本文介绍了Hubot/Express:在特定路由中禁用基本身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Hubot,并且已经定义了环境变量EXPRESS_USER和EXPRESS_PASSWORD以启用基本身份验证. Hubot使用express,基本上是

I am using Hubot and I've defined the environment variables EXPRESS_USER and EXPRESS_PASSWORD to enable basic authentication. Hubot uses express and basically it's

setupExpress: ->
    user    = process.env.EXPRESS_USER
    pass    = process.env.EXPRESS_PASSWORD
    stat    = process.env.EXPRESS_STATIC

    express = require 'express'

    app = express()

    app.use (req, res, next) =>
      res.setHeader "X-Powered-By", "hubot/#{@name}"
      next()

    app.use express.basicAuth user, pass if user and pass
    app.use express.query()
    app.use express.bodyParser()
    app.use express.static stat if stat`

我想在不需要基本身份验证的脚本中公开HTTP命令.但是我无法更改在Hubot中表示正在初始化的代码的更改

I want to expose an HTTP command in a script that doesn't need basic auth. However I'm not able to change change the code in Hubot where express it's being initialized

robot.router.get '/some-anonymous-path', (req, res) ->
  console.log 'Should be here without need to authenticate

有人知道是否可以在expressjs中做到这一点.

Does anyone know if it's possible to do it in expressjs.

预先感谢

布鲁诺

推荐答案

如何将nginx放在Hubot的前面?然后,您还可以添加SSL,仅允许访问特定路径,重写url甚至提供由hubot脚本创建的静态内容.一个简单的示例nginx.conf块:

How about putting nginx in front of your Hubot? Then you can also add SSL, only allow access to specific paths, rewrite urls or even serve static content created by hubot scripts. A simple example nginx.conf block:

upstream hubot {
  server localhost:8080;
}
server {
  listen 80;
  satisfy any;
  allow 127.0.0.1;
  deny all;
  location ~ ^/(public|obscured) {
    allow all;
    proxy_pass http://hubot;
  }
  location ~ ^/(static) {
    auth_basic "Restricted";
    auth_basic_user_file htpasswd;
    root /www;
  }
  location ~ {
    auth_basic "Restricted";
    auth_basic_user_file htpasswd;
    proxy_pass http://hubot;
  }
}

然后在/etc/nginx/htpasswd和您的hubot初始化脚本集中将htpasswd对扔掉(默认绑定为0.0.0.0),您将可以参加比赛.

Then toss an htpasswd pair in /etc/nginx/htpasswd and in your hubot init script set BIND_ADDRESS=localhost (default bind is 0.0.0.0) and you'll be off to the races.

这篇关于Hubot/Express:在特定路由中禁用基本身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 06:15