久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

嘗試在節(jié)點(diǎn)中實(shí)現(xiàn) JWT 身份驗(yàn)證.在受保護(hù)的路線

Trying to implement JWT authentication in node. Getting unauthorized in protected routes(嘗試在節(jié)點(diǎn)中實(shí)現(xiàn) JWT 身份驗(yàn)證.在受保護(hù)的路線中獲得未經(jīng)授權(quán))
本文介紹了嘗試在節(jié)點(diǎn)中實(shí)現(xiàn) JWT 身份驗(yàn)證.在受保護(hù)的路線中獲得未經(jīng)授權(quán)的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試實(shí)現(xiàn) JWT 身份驗(yàn)證./login 和/register 工作正常,它們返回身份驗(yàn)證令牌,但是當(dāng)我嘗試使用標(biāo)頭 'Authorization' = 'JWT token_received' 獲取/secret 時(shí),它返回一個(gè)字符串'unauthorized',我看不到來自 JWTStrategy 的日志記錄.請(qǐng)讓我知道我哪里出錯(cuò)了.

I am trying to implement JWT authentication. /login and /register work fine and they return authentication token, but when I try to GET /secret with a header 'Authorization' = 'JWT token_received', it returns a string 'unauthorized' and I can see no logging from JWTStrategy. Please let me know where I am going wrong.

var opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = "very_secret"

passport.use(new JwtStrategy(opts, function(payload, next){
    console.log("payload received" + payload);
    User.findById(payload.id, function(err, user){
        console.log("user found:" + user);
        if(err){
            return next(err, false)
        }
        else if(user){
            return next(null, user)
        }
        else{
            return next(null, false)
        }
    });
    }
));

app.use(passport.initialize());

app.post("/login", function(req, res){

        var email = req.body.email;
        var password = req.body.password;

        var user = User.findOne({"email": email}, function(err, user){

        if(err){
            res.json({"error": err});
            return;
        }
        if(!user){
            res.json({"message": "No user found"});
            return;
        }
        if(user.password == password){
            res.json(
                { 
                    "message": "User found",
                    "token": jwt.sign({"id": user.id}, opts.secretOrKey)
                }
            );
        }
        else{
            res.json({"message": "Password did not match"});
        }
    });
});

app.post("/register", function(req, res){
new User({ email: req.body.email, password: req.body.password}).
    save(function(err, user){
            if(err){
                res.json({"message": "User cannot be created"});
            }
            else{
                res.json(
                    { 
                        "message": "ok",
                        "token": jwt.sign({"id": user.id}, opts.secretOrKey)
                    }
                );
            }
    });
});

app.get("/secret", passport.authenticate("jwt", {session: false}), function(req, res){
    console.log(req.get('Authorization'));
    res.json(req.user);
});

使用郵遞員作為客戶端.向郵遞員索取詳細(xì)信息,

Using postman as the client. Request details from postman,

GET /secret HTTP/1.1
Host: localhost:3000
Content-Type: application/x-www-form-urlencoded
Authorization: JWT token_I_received_on_login
Cache-Control: no-cache
Postman-Token: 114060a3-3074-6688-6245-0b0cfe7e9f04

推薦答案

知道了.請(qǐng)求中有錯(cuò)誤.

Got it. Made a mistake in the request.

根據(jù) README,應(yīng)該是 'Authorization' = 'bearer token_received_on_login'

As per the README, it should be 'Authorization' = 'bearer token_received_on_login'

這篇關(guān)于嘗試在節(jié)點(diǎn)中實(shí)現(xiàn) JWT 身份驗(yàn)證.在受保護(hù)的路線中獲得未經(jīng)授權(quán)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Is Math.random() cryptographically secure?(Math.random() 在密碼學(xué)上是安全的嗎?)
Secure random numbers in javascript?(在javascript中保護(hù)隨機(jī)數(shù)?)
How to avoid multiple token refresh requests when making simultaneous API requests with an expired token(使用過期令牌發(fā)出同時(shí) API 請(qǐng)求時(shí)如何避免多個(gè)令牌刷新請(qǐng)求)
JWT not decoding quot;JWT malformedquot; - Node Angular(JWT 未解碼“JWT malformed;- 節(jié)點(diǎn)角度)
How to invalidate a JWT token with no expiry time(如何使沒有到期時(shí)間的 JWT 令牌無效)
Authorization header in img src link(img src 鏈接中的授權(quán)標(biāo)頭)
主站蜘蛛池模板: 91久久精品国产免费一区 | 精品久久久久久亚洲国产800 | 亚洲精品成人在线 | jizz在线免费观看 | 国产线视频精品免费观看视频 | 91视频一区 | 黄色毛片免费看 | 成人国产精品 | 成人永久免费视频 | 91影院在线观看 | 欧美成人高清视频 | 玖玖免费 | 一道本不卡| 国产成人精品一区二区三区 | 99精品国自产在线 | 亚洲高清久久 | 国产成人精品999在线观看 | 日韩中文一区二区三区 | 久久精品国产一区二区电影 | 国产成人精品午夜 | 久热精品视频 | 91久久精品一区二区三区 | 久久高清免费视频 | 国产97在线视频 | 在线观看av网站永久 | 中文字幕一区二区三区精彩视频 | 欧美精品国产精品 | 五月婷婷在线视频 | 羞羞视频在线网站观看 | 国产精品日韩高清伦字幕搜索 | 超碰免费在线观看 | 蜜桃精品视频在线 | 久久av.com | 国产美女永久免费无遮挡 | av免费在线观看网站 | 女女百合av大片一区二区三区九县 | 久草在线中文888 | 99精品欧美一区二区三区 | 天天色天天射天天干 | 在线日韩精品视频 | 午夜播放器在线观看 |