使用身份验证中间件安排路由 [英] Arrange routes with authentication middleware

查看:95
本文介绍了使用身份验证中间件安排路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多路线。
其中大多数需要身份验证。
一个没有。



这里是:

  router.get('/ secure1 ',函数(req,res){...})
router.get('/ secure2',函数(req,res){...})
router.get('/ secure3 ',函数(req,res){...})
router.get('/:id',函数(req,res){...})

1。。让我们想象一下,我没有公共路线。



在页面顶部,我可以放置一个安全检查中间件,一切都很好。
它只会让安全连接通过,而将重定向非安全连接。

  router.use(函数(req,res,next){
securityCheck()
next( )
})
router.get('/ secure1',函数(req,res){...})
router.get('/ secure2',函数(req,res ){...})
router.get('/ secure3',函数(req,res){...})
router.get('/:id',函数(req, res){...})

这可行。这样可以确保所有安全的路由都安全,但是却阻止了我进入公共路由('/:id')。



2。将公共路线移到顶部:

  router.get('/:id',function(req,res){。 ..})
router.use(函数(req,res,next){
securityCheck()
next()
})
router.get(' / secure1',功能(req,res){...})
router.get('/ secure2',功能(req,res){...})
router.get(' / secure3',函数(req,res){...})

但是这样可以捕获我所有的请求和所有安全路径都无法访问。



3。。我可以在每条安全路由上放置一个中间件,但这似乎有点乏味并且容易发生人为错误:

  router.get('/ secure1',securityCheck(),function(req ,res){...})

所以,我没考虑过的更好的选择?什么是最佳做法?



谢谢

解决方案

出于个人选择,我个人更喜欢第一个。在中间件中,您始终可以检查 req.path req.url 来选择要设置为安全的内容。 / p>

另一个选项是使用HTTP身份验证,例如.htaccess。看看 https://github.com/http-auth/http-auth



我之前进行身份验证的一种方法是,将用户名/密码作为json传递给请求主体一次,然后为将来的请求生成无状态令牌( https://github.com/auth0/node-jsonwebtoken )。在我的情况下,不需要身份验证的路由器条目并不多,因此我对条目本身进行了处理。



此外,为了提高安全性,请使用HTTPS或对数据进行编码。例如。 如何在Node.js中创建HTTPS服务器?



希望有帮助!


I have many routes. Most of them require authentication. One doesn't.

Here they are:

router.get('/secure1', function (req,res) {...})
router.get('/secure2', function (req,res) {...})
router.get('/secure3', function (req,res) {...})
router.get('/:id', function (req,res) {...})

1. Let's imagine I didn't have the public route.

At the top of the page I could just put a security check middleware, and all is well. It will only let through secure connections, and will redirect non secure.

router.use(function (req,res,next) {
   securityCheck()
   next()
})
router.get('/secure1', function (req,res) {...})
router.get('/secure2', function (req,res) {...})
router.get('/secure3', function (req,res) {...})
router.get('/:id', function (req,res) {...})

This would work. this makes all the secure routes secure, but it blocks me from the public route ('/:id').

2. I could move the public route to the top:

router.get('/:id', function (req,res) {...})
router.use(function (req,res,next) {
   securityCheck()
   next()
})
router.get('/secure1', function (req,res) {...})
router.get('/secure2', function (req,res) {...})
router.get('/secure3', function (req,res) {...})

But this way it catches all of my requests and all the secure paths are inaccessible.

3. I could put a middleware on every single secure route, but that seems a little tedious and prone to human-errors:

router.get('/secure1',securityCheck(), function (req,res) {...})

So, is there a better option I didn't consider? What is considered the best practice?

Thank you

解决方案

Out of your options I would personally prefer the first one. In the middleware you can always check on req.path or req.url to choose what to set as secure.

Another option is using HTTP authentication like in .htaccess. Have a look at https://github.com/http-auth/http-auth.

A way I have done authentication before was by passing username/password over the request body as json once and then producing a stateless Token for future requests (https://github.com/auth0/node-jsonwebtoken). In my case not many router entries needed authentication, so I handled it on the entries themselves.

Also, for extra security, use HTTPS or encode your data. Eg. How to create an HTTPS server in Node.js?

Hope it helped!

这篇关于使用身份验证中间件安排路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆