久久久久久久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| 成人av观看| 在线一区二区三区 | 欧美中文 | 久久久久久久久久久久亚洲 | 国产超碰人人爽人人做人人爱 | 麻豆精品久久久 | 欧美日韩三级 | av综合站| 天天躁日日躁狠狠的躁天龙影院 | 日韩国产欧美一区 | 国产欧美精品一区二区三区 | 欧美精品一区二区三区蜜桃视频 | 草久久久 | 狠狠入ady亚洲精品经典电影 | 成人在线不卡 | 中国一级大毛片 | 亚洲国产成人精品女人久久久 | 国产高清视频一区二区 | 久久国产三级 | 在线观看av网站永久 | 日韩欧美一区二区三区免费观看 | 久久中文字幕视频 | 午夜免费看 | 91视频在线看 | 国产内谢 | 国产精品国产三级国产aⅴ中文 | 91精品国产乱码久久久 | 国产免费视频 | 日韩免费毛片 | 色香蕉在线 | 伊人伊成久久人综合网站 | 久久精品久久精品久久精品 |