天天看點

basic-auth的使用basic-auth的使用

basic-auth的使用

basic-auth 會幫我們解析

http header

authorization

内的值,這個值通常是使用

base64

加密的。

導入

解析header中authorization的值

在 postman 中這樣送出就可以被解析到

basic-auth的使用basic-auth的使用

手動解析authorization

const Koa = require('koa');
const app = new Koa();
 
app.use(async (ctx, next) => {
    let auth = ctx.request.header.authorization;    //http header的值
    auth = auth.split(' ')[1];  //有"basic "的字首,用split分割空格取值
    auth = Buffer.from(auth, 'base64').toString().split(':')[0];    //解析base64,轉化為字元串,而且他有一個“:”的符号,需要分割
    console.log(auth);  //結果
})
 
app.listen(3000);
           

繼續閱讀