如何解决使用基本Auth身份验证中间件ExpressJS切换为true的变量admin是否安全?
我有一个用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 举报,一经查实,本站将立刻删除。