11

Có thể thực hiện auth cơ bản trong Node.js giống như trong Apache không?Node.js http basic auth

http://doc.norang.ca/apache-basic-auth.html

Tôi biết rằng nếu sử dụng Express hoặc kết nối tôi có thể thêm chức năng trung ware và làm xác minh người dùng, nhưng tôi đang cố gắng để hạn chế toàn bộ khu vực (Tôi không cần phải xác thực người dùng từ một cơ sở dữ liệu chỉ là một vài người dùng được định nghĩa) - Tôi đang sử dụng Ubuntu.

https://github.com/kaero/node-http-digest

Đó là điều mà tôi có thể làm, nhưng tôi không chắc chắn nếu "phơi bày" hoặc trực tiếp bằng văn bản cho người dùng và mật khẩu trong mã là đủ an toàn.

Rất cám ơn.

Trả lời

15

Passport cung cấp cơ chế sạch để triển khai xác thực cơ bản. Tôi sử dụng nó trong ứng dụng Node.js Express để bảo vệ cả giao diện người dùng dựa trên Angularjs cũng như API RESTful của tôi. Để có được hộ chiếu và chạy trong ứng dụng của bạn thực hiện như sau:

  • NPM cài đặt hộ chiếu

  • NPM cài đặt hộ chiếu-http (chứa "BasicStrategy" đối tượng cho auth cơ bản)

  • Mở app.js và bạn thêm dòng sau:

    var passport = require('passport')  
    var BasicStrategy = require('passport-http').BasicStrategy 
    
    passport.use(new BasicStrategy(
        function(username, password, done) { 
        if (username.valueOf() === 'yourusername' && 
         password.valueOf() === 'yourpassword') 
         return done(null, true); 
        else 
         return done(null, false); 
        } 
    )); 
    
    // Express-specific configuration section 
    // *IMPORTANT* 
    // Note the order of WHERE passport is initialized 
    // in the configure section--it will throw an error 
    // if app.use(passport.initialize()) is called after 
    // app.use(app.router) 
    app.configure(function(){ 
        app.use(express.cookieParser()); 
        app.use(express.session({secret:'123abc',key:'express.sid'})); 
        app.set('views', __dirname + '/views'); 
        app.set('view engine', 'jade'); 
        app.set('view options', { 
        layout: false 
        }); 
        app.use(express.bodyParser()); 
        app.use(express.methodOverride()); 
        app.use(express.static(__dirname + '/public')); 
        app.use(passport.initialize()); 
        app.use(app.router); 
        app.use(logger); 
    }); 
    
    // Routes 
    
    app.get('/', passport.authenticate('basic', { session: false }), routes.index); 
    app.get('/partials/:name', routes.partials); 
    
    // JSON API 
    
    app.get('/api/posts', passport.authenticate('basic', { session: false }), api.posts); 
    app.get('/api/post/:id', passport.authenticate('basic', { session: false }), api.post) 
    // --Repeat for every API call you want to protect with basic auth-- 
    
    app.get('*', passport.authenticate('basic', { session: false }), routes.index); 
    
+4

Ví dụ của bạn hoạt động tốt. Nó thực sự đơn giản hơn (và dễ hiểu hơn) so với cái được cung cấp bởi hộ chiếu-http trên GitHub. – Stilltorik

+0

Cảm ơn Stilltorik! – TWright

3

Có một cái nhìn tại địa chỉ: user authentication libraries for node.js?

Nó không trả lời câu hỏi của bạn 100% - nhưng có lẽ nó giúp.

+0

Hi @nivoc, nhờ để trả lời, có lẽ phần giữa là cách để đi, tôi chưa thấy bất kỳ giải pháp nào khác cho trường hợp này. – Jaime

12

Đặt thi s

app.use(express.basicAuth(function(user, pass) { 
    return user === 'test' && pass === 'test'; 
})); 

trước dòng để

app.use(app.router); 

để bảo vệ tất cả các tuyến bằng http auth cơ bản

3

Tôi nghĩ rằng sự lựa chọn tốt có thể là mô-đun http-auth

// Authentication module. 
var auth = require('http-auth'); 
var basic = auth.basic({ 
    realm: "Simon Area.", 
    file: __dirname + "/../data/users.htpasswd" // gevorg:gpass, Sarah:testpass ... 
}); 

// Application setup. 
var app = express(); 
app.use(auth.connect(basic)); 

// Setup route. 
app.get('/', function(req, res){ 
    res.send("Hello from express - " + req.user + "!"); 
}); 
+3

Tôi nghĩ rằng tuyên bố từ chối trách nhiệm rằng bạn là người duy trì 'http-auth' sẽ thích hợp ở đây. –

Các vấn đề liên quan