问题描述
我有一个用expressJs制作的API,我想保护它。 为此,我使用了基本的身份验证身份验证。我正在使用这种中间件来使它工作
let admin = false
app.use((req,res,next) => {
// -----------------------------------------------------------------------
// authentication middleware
const auth = {login: '123',password: '123'} // change this
// parse login and password from headers
const b64auth = (req.headers.authorization || '').split(' ')[1] || ''
const [login,password] = Buffer.from(b64auth,'base64').toString().split(':')
// Verify login and password are set and correct
if (login && password && login === auth.login && password === auth.password) {
// Access granted...
admin=true
return next()
}
// Access denied...
admin=false
return next()
//res.set('WWW-Authenticate','Basic realm="401"') // change this
//res.status(401).send('Authentication required.') // custom message
// -----------------------------------------------------------------------
})
在原始代码(我在stackOverflow上找到)中没有变量“ admin”。但是我想拥有一个不具有GET功能认证的非管理员访问权限。 因此,在其他所有协议(POST / PUT / DELETE)中,我都会测试admin === true。
但是我发现它太容易被保护。 您如何看待?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)