hapi-auth-basic
hapi-auth-basic ---- 支持HTTP Basic 身份验证。可用于网站和API。建议仅使用SSL/TLS协议将用户名/密码作为明文通过网络发送
在官网上身份验证例子的插件就是hapi-auth-basic
1. 在npm install hapi-auth-basic bcrypt --save 的同时 记得还有一个bcrypt
bcrypt: 一种安全存储密码, 加密解密
hapi-auth-basic:这个插件提供了一个称为Basic的身份验证模式,我们使用Basic 身份衍生模式,通过调用server.auth.strategy()方法来创建一个新的身份验证策略。
这份是官方贴出的代码 我将每句的作用写在后边
const Bcrypt = require('bcrypt');
const Hapi = require('hapi');
const users = { // 模拟数据库数据,跟参数进行对比
john: {
username: 'john',
password: '123', // '密文'
name: 'John Doe',
id: '2133d32a'
}
};
const validate = async (request, username, password, h) => {
/**
* request 是正在进行身份验证的请求的hapi请求对象
* username 从客户端接收的用户名
* password 从客户端接收的密码
* h 响应工具
*/
const user = users[username];
if (!user) {
return { credentials: null, isValid: false };
}
// const isValid = await Bcrypt.compare(password, user.password); // 解密密码对比
const credentials = { id: user.id, name: user.name }, isValid = false;
return { isValid, credentials };
};
const start = async () => {
const server = Hapi.server({ port: 4000 });
await server.register(require('hapi-auth-basic')); // 引入插件
/**
* validate (必选) 可以编译代码来判定请求中的用户和密码能否被解析成一个有效用户。
* allowEmptyUsername-(可选)如果为真,则允许使用空用户名进行请求。默认为false。
* unauthorizedAttributes—(可选)如果设置,则直接传递给Boom.unauthor.(如果没有引发自定义错误)。用于在WWW-Authenticate头部中设置realm属性。未定义的默认值
*/
server.auth.strategy('simple', 'basic', { validate });
server.auth.default('simple'); // 默认
server.route({
method: 'GET',
path: '/',
handler: function (request, h) {
return 'welcome';
}
});
await server.start();
console.log('server running at: ' + server.info.uri);
};
start();
上边的这行const isValid = await Bcrypt.compare(password, user.password); // 解密密码对比
咱们没有存储的时候加密 就先跳过, 直接写一个死值
来实践下
node app.js 起服务 搞起!!!
// 没输入账号密码, 我的锅
$.ajax({
type: 'get',
url: 'http://127.0.0.1:3000',
success: function (res) {
console.log(res)
}
})
// 搞它
$.ajax({
type: 'get',
url: 'http://127.0.0.1:3000',
data: {
username: 'john',
password: '123'
},
success: function (res) {
console.log(res)
}
})
// 再来
$.ajax({
type: 'get',
url: 'http://127.0.0.1:3000',
data: JSON.stringify({
username: 'john',
password: '123'
}),
success: function (res) {
console.log(res)
}
})
// 还不成
$.ajax({
type: 'get',
url: 'http://127.0.0.1:3000',
headers:{
Authorization: 'john 123'
},
success: function (res) {
console.log(res)
}
})
{
statusCode: 401,
error: "Unauthorized",
message: "Missing authentication"
}
四连败 我擦嘞 再来
$.ajax({
type: 'get',
url: 'http://127.0.0.1:3000',
headers:{
username: 'john',
password: '123'
},
success: function (res) {
console.log(res)
}
})
// statusCode 200 成功但是返回错误
{
message: "CORS error: Some headers are not allowed"
}
这个报错说白了就是请求头不合法
hapi中默认情况下任何跨域请求允许request header 集合
- Accept
- Authorization
- Content-Type
- If-None-Match
- Origin
如果请求中包含一个不再上述列表中的额外header,这个请求将失败,如果需要自定义请求头
{
config: {
cors: {
additionalHeaders: ['username']
}
}
}
还是401 不试了,
还有一种方案应该是ok的
$.ajax({
type: 'get',
url: 'http://127.0.0.1:3000',
headers:{
'Authorization' : 'Basic base64编码'
},
success: function (res) {
console.log(res)
}
})
换个环境浏览器直接调用
输入 john 密码 123
我擦嘞, 可以了
查阅了挺多的资料, 加上自己的实践,还是不算太理解这个basic,只能尽量的解释。
谢谢。