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

在標頭中傳遞 JWT

Pass JWT in Header(在標頭中傳遞 JWT)
本文介紹了在標頭中傳遞 JWT的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在使用 NodeJs 學習 JWT.我被困在通過標頭中的 JWT 實際上我不知道該怎么做.

I am learning JWT with NodeJs. I am stuck at passing the JWT in header actually i do not know how to do this.

index.js 文件

var express = require('express'),
 app = express(),
 routes = require('./routes'),
 bodyParser = require('body-parser'),
 path = require('path'),
 ejs = require('ejs'),
 jwt = require('jsonwebtoken');

app.use(bodyParser.urlencoded({ extended: false })); 
app.use(bodyParser.json());

app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

app.post('/home',routes.loginUser);

app.get('/', function(req, res) {
  res.render('index');
});

app.get('/home',function(req, res) {
  jwt.verify(req.token, 'qwertyu6456asdfghj', function(err, data) {
    if (err) {
      res.sendStatus(403);
    } 
  });
});

 app.listen(3000,function(){
  console.log("Server running at Port 3000");
});

routes/index.js 文件

var  jwt = require('jsonwebtoken');

exports.home = function(req, res){
  res.render('home',{error: false});
};

exports.loginUser = function(req, res) {
    var uname = req.body.Username;
    var pwd = req.body.Password;

    if(uname && pwd === 'admin'){
        res.render('home');

    var token = jwt.sign({ user: uname }, 'qwertyuiopasdfghj');
    console.log('Authentication is done successfully.....');
    console.log(token);
    }

    response.json({
        authsuccess: true,
        description: 'Sending the Access Token',
        token: token
    });
};

當我運行應用程序時,我在 console.log 中獲取了令牌,但是如何在 header 中傳遞令牌并將其存儲在瀏覽器的 localStorage 中?

when i run the application i am getting the token in console.log but How can I pass token in header and store it in localStorage of browser?

推薦答案

所以你想將令牌發送到前端而不是正文中.

So you want to send the token to frontend but not in the body.

推薦的方法是使用 cookie.可以在cookie中設置token,可以在前端和后端自動訪問.

The Recommended way to do so is to use cookies. You can set the token in the cookie and it can be automatically accessed in front-end and in the backend.

res.cookie('tokenKey', 'ajsbjabcjcTOKENajbdcjabdcjdc');

使用授權標頭也是一個好方法,但同樣,在前端,您必須從標頭中獲取令牌,然后保存在 localStorage 或 cookie 中,如果是 cookie,您不必這樣做.

Using authorization headers is also a good approach, but again, in front-end, you have to fetch the token from headers and then save in localStorage or cookie, which you don't have to do in case of cookie.

res.header(field [, value]);

這篇關于在標頭中傳遞 JWT的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Is Math.random() cryptographically secure?(Math.random() 在密碼學上是安全的嗎?)
Secure random numbers in javascript?(在javascript中保護隨機數?)
How to avoid multiple token refresh requests when making simultaneous API requests with an expired token(使用過期令牌發出同時 API 請求時如何避免多個令牌刷新請求)
JWT not decoding quot;JWT malformedquot; - Node Angular(JWT 未解碼“JWT malformed;- 節點角度)
How to invalidate a JWT token with no expiry time(如何使沒有到期時間的 JWT 令牌無效)
Authorization header in img src link(img src 鏈接中的授權標頭)
主站蜘蛛池模板: 色av一区二区三区 | www.亚洲 | 日韩中文在线 | 国产视频一区二区 | 精品视频在线一区 | 美女操网站 | 久久久xxx | 久久er精品 | 日韩av在线不卡 | 中文字幕一区二区三区精彩视频 | 一区二区三区视频在线观看 | 黄网站免费在线观看 | www.国产 | 色偷偷888欧美精品久久久 | 色婷婷av99xx| 毛片久久久 | 精品在线视频播放 | 亚洲一区二区三区四区在线观看 | www一级片 | 成人二区| 中文字幕高清免费日韩视频在线 | 91精品亚洲 | 日韩欧美综合在线视频 | www.成人免费视频 | 青青久草| 久久久久久久一区 | 久久久久亚洲视频 | 国产精品视频一二三区 | 亚洲成人精选 | 99精品免费久久久久久久久日本 | 91精品国产乱码久久久久久久 | 成人欧美一区二区 | 97超级碰碰 | 青青草精品 | 国产精品欧美一区二区 | 久久久久av| 国产精品视频在 | 精品久久影院 | 国产免费福利小视频 | 久久久精品网 | 精品中文字幕久久 |