basic 認證機制 基本流程
第一次
判斷請求頭headers 中authorization字段的值爲空,這時讓用戶輸入,然後
第二次
的時候判斷用戶輸入的值是否正確 做相應的處理
nodejs 代碼
const http = require('http');
const server = http.createServer(function(req,res){
const authorization = req.headers["authorization"];
if(!authorization){
res.writeHead(401,{
"content-Type":"text/plan",
"WWW-authenticate":'Basic realm="famly"'
})
res.end('');
}else{
const str = authorization.slice(6,authorization.length);
const resStr =new Buffer(str,"base64").toString();
if(resStr!=="xiajiang:123"){
res.writeHead(401,{
"content-Type":"text/plan",
"WWW-authenticate":'Basic realm="famly"'
})
res.end('');
}else{
res.end("aaaaaaaaaaaaa")
}
}
});
server.listen(8080);
basic 認證缺點太多,他雖然用base64 加密後傳輸,但是近乎銘文,一般只有在https的情況下才會使用, 爲了改進basic 認證,他需要加入服務端隨機數來保護認證的過程。