简体   繁体   English

如何使用express-ntlm获得Windows用户名而无需使用NODE.js进行身份验证?

[英]how to use express-ntlm to get windows user name without authentication using NODE.js?

I am trying to use express-ntlm to get windows user name without authentication. 我正在尝试使用express-ntlm来获取Windows用户名而不进行身份验证。

in my app.js, i put the following: 在我的app.js中,我输入了以下内容:

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
var ntlm = require('express-ntlm'); 
app.use(ntlm()); 
app.use('/search', search);

This gives me a 401 error in node.js when loading http://localhost:3000/search In chrome console: Failed to load resource: Failed to load resource: net::ERR_UNEXPECTED 在Chrome控制台中加载http:// localhost:3000 / search时,这在node.js中给了我401错误:无法加载资源:无法加载资源:net :: ERR_UNEXPECTED

what is the correct sequence of routing here? 正确的路由顺序是什么? thanks. 谢谢。

========= modified to ============== =========修改为=============

var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var TT = require('./routes/TT');
var KYEC_stat = require('./routes/KYEC_stat');
var ftc = require('./routes/ftc');
var volPerDevice = require('./routes/topVolPerDevice');
var search = require('./routes/search');

var ntlm = require('express-ntlm'); 
var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(ntlm()); 
app.use('/search', search);
app.use('/tt', TT);
app.use('/kyec', KYEC_stat);
app.use('/ftc', ftc);
app.use('/vol', volPerDevice);
app.use('/', routes);

/// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

======= topVolPerDevice.js ================ ======= topVolPerDevice.js ================

var config = require('../config.json');
var express = require('express');
var query = require('pg-query');
var assert = require('assert');
var async = require('async');
var url = require('url');
var queryString = require('querystring');

var router = express.Router();


/* Display quantity tested per device id since 2011 using d3 */
/* The query will first select all lot records and their earliest test date (which is the 'P' insertion incoming material quantity);
   then use {lotid, lotstartdate} to retrieve all 1st insertion lot records, then add up lots per device;
   then return the 1st 20 device id which tops inocming material quantity;
 */
router.get('/', function(req, res) {
        query.connectionParameters = config.reportConnStr;      //connecting to localhost
        var deviceArray = new Array();
        var sqlstr =  "sdfsfdfsdsfds";
        query(sqlstr, function(err, rows, result) {
            assert.equal(rows, result.rows);
            for (var i = 0; i < rows.length; i++) {
                var device = {};
                device.name = rows[i].device;
                device.value = rows[i].totalqtyout;
                deviceArray.push(device);        
            }
            res.render('d3t1', {deviceArray:deviceArray});                      
        });
});

module.exports = router;

===== update 2/25/2015 ============= =====更新2/25/2015 ==============

I am re-visiting the problem this week, and I reached a little bit further. 我将在本周重新讨论该问题,并且进一步了解了该问题。 I tried to put down a few debug statements in express-ntlm.js and also installed firebug in firefox. 我试图在express-ntlm.js中放一些调试语句,并在firefox中安装了firebug。 It turns out that it is probably not about the sequence of middleware. 事实证明,这可能与中间件的顺序无关。

return function(request, response, next) {
    if (!request.connection.id) {
        request.connection.id = utils.uuidv4();
    }

    var auth_headers = request.headers.authorization;

    var user = request.connection.ntlm;
....
}

over here, my request.connection.ntlm is null. 在这里,我的request.connection.ntlm为null。

What could be the reason here? 这可能是什么原因? is it about browser settings or my network settings? 是关于浏览器设置还是我的网络设置?

I am running this over company network, and I am on a network domain. 我正在公司网络上运行此程序,并且我在网络域上。

在此处输入图片说明


UPDATE: 更新:

Change to this in the app.js: 在app.js中更改为:

app.use(ntlm()); 
app.use('/', search);
app.use('/', TT);
app.use('/', KYEC_stat);
app.use('/', ftc);
app.use('/', volPerDevice);
app.use('/', routes);

And add '/vol' in the router in the topVolPerDevice file: 并在topVolPerDevice文件的路由器中添加“ / vol”:

var config = require('../config.json');
var express = require('express');
var query = require('pg-query');
var assert = require('assert');
var async = require('async');
var url = require('url');
var queryString = require('querystring');

var router = express.Router();


/* Display quantity tested per device id since 2011 using d3 */
/* The query will first select all lot records and their earliest test date (which is the 'P' insertion incoming material quantity);
   then use {lotid, lotstartdate} to retrieve all 1st insertion lot records, then add up lots per device;
   then return the 1st 20 device id which tops inocming material quantity;
 */
router.get('/vol', function(req, res) {
        query.connectionParameters = config.reportConnStr;      //connecting to localhost
        var deviceArray = new Array();
        var sqlstr =  "sdfsfdfsdsfds";
        query(sqlstr, function(err, rows, result) {
            assert.equal(rows, result.rows);
            for (var i = 0; i < rows.length; i++) {
                var device = {};
                device.name = rows[i].device;
                device.value = rows[i].totalqtyout;
                deviceArray.push(device);        
            }
            res.render('d3t1', {deviceArray:deviceArray});                      
        });
});

module.exports = router;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM