var http=require('http');
http.createServer(function(req,res){
var au=req.headers.authorization;
if(au == undefined){
res.writeHead(401,{
'content-Type':'text/plain',
'WWW-Authenticate':'Basic realm="family"'
});
res.end('');
}else{
var str=req.headers.authorization;
str = str.slice(6,str.length);
console.log(str);
var str1 =new Buffer(str,'base64').toString();
if(str1 != "cd:123"){
res.writeHead(401,{
'content-Type':'text/plain',
'WWW-Authenticate':'Basic realm="family"'
});
res.end('');
}else {
res.end("aaaaaaaaaaaaa")
}
}
}).listen(8888,'127.0.0.1');
console.log('server running at localhost:8888 ');
本文介绍了一个使用Node.js实现的基本HTTP认证示例。该示例展示了如何创建一个简单的服务器,该服务器通过检查请求头中的授权信息来验证客户端身份。若未提供正确的认证信息,则返回401状态码并提示进行基本认证。
995

被折叠的 条评论
为什么被折叠?



