Node.js验证和授权

时间:2015-12-27 09:19:36

标签: node.js authentication express authorization

我在Practical Node.js引用了这本书Azat Mardan。一个博客项目,有两种类型的用户即。 (i)管理员和(ii)普通用户。我对身份验证和授权不了解的部分如下 -

app.js包含代码段

//Authentication middleware
app.use(function(req, res, next){
    if(req.session && req.session.admin)
    res.locals.admin = true;
    next();
});

//Authorization middleware
var authorize = function(req,res,next){
    if(req.session && req.session.admin)
        return next();
    else
        return.send(401);
};

//Pages & routes
app.get(‘/login’, routes.user.login);   //This displays the login form
app.post(‘/login’, routes.user.authenticate);
app.get(‘/admin’, authorize, routes.article.admin);
app.get(‘/post’, authorize, routes.article.post);
app.post(‘/post’, authorize, routes.article.postArticle);

用户路线即user.js具有以下代码段

exports.login = function(req, res, next){
    res.render(‘login’);    //Pretty simple, it renders the login form page
};

exports.authenticate = function(req, res, next){
    //The sanitation check is not show here
    if(req.body.email && req.body.password)
    {
        //Here I assume, all credentials are correct
        req.session.user = user;
        req.session.admin = user.admin;
        res.redirect(‘/admin’);
    }
};

我的问题是,在app.js中,作者如何采取或做出以下陈述,或者我理解错误。

if(req.session && req.session.admin)
    res.locals.admin = true;

并且还在授权功能中,

if(req.session && req.session.admin)

req.session.admin如何成真。我们设置req.session.admin = user.admin是用户路线。

请注意:这不仅仅是对管理员进行身份验证,而且普通用户也应该能够登录和发布文章。

2 个答案:

答案 0 :(得分:1)

我的名字是Azat Mardan,我是Practical Node的作者和你在上面提到的代码。

如果您使用快速会话中间件提供的Web会话,

req.session.admin将自动可用。 req.session将在来自同一客户端的请求之间保持不变(感谢Cookie中的会话ID)。您可以在此req.session对象上存储任何内容,包括admin = true。

答案 1 :(得分:0)

当用户输入用户名&密码req.session.admin = user.admin被调用。它没有显示,但是应该查找用户(比如在数据库中),然后在会话中设置凭据user(从查找中返回app.use( function... ))。

所有请求都通过身份验证中间件var authorize = function...,并检查是否已设置会话管理标志(即某人已成功以管理员身份登录)并将其添加到响应中。

对管理区域的所有请求也都通过授权中间件public class DataBaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "scheduleDB"; private static final int DB_VERSION = 1; Context context; public DataBaseHelper(Context context) { super(context, DATABASE_NAME, null, DB_VERSION); this.context = context; } @Override public void onCreate(SQLiteDatabase db) { try { db.execSQL("CREATE TABLE time (id INTEGER PRIMARY KEY AUTOINCREMENT, arrival VARCHAR(10), departure VARCHAR(10));"); insertTime(db,"city"); } catch (SQLException e) { e.printStackTrace(); } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { try { onCreate(db); } catch (SQLException e) { e.printStackTrace(); } } public void insertTime(SQLiteDatabase database, String table) { BufferedReader br = null; String line; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { database.beginTransactionNonExclusive(); } else { database.beginTransaction(); } try { br = new BufferedReader(new InputStreamReader(context.getAssets().open("time.txt"))); while ((line = br.readLine()) != null) { String query = "INSERT INTO "+table+" (arrival, departure) VALUES (?,?)"; SQLiteStatement statement = database.compileStatement(query); String[] time = line.split(","); for(int i = 1, n = time.length; i < n; i+=2) { statement.clearBindings(); statement.bindString(1,time[i-1]);//arrival statement.bindString(2,time[i]);//departure statement.execute(); } } br.close(); } catch (IOException e) { e.printStackTrace(); } finally { database.setTransactionSuccessful(); database.endTransaction(); if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } } ,这将再次检查会话管理标志,但如果某人尚未以管理员身份登录,则会返回401错误。

相关问题