久久久久久久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 鏈接中的授權標頭)
主站蜘蛛池模板: 欧美午夜精品一区二区三区 | 黄色小视频免费 | 成人三级在线观看 | 欧美日韩中文字幕 | 日韩欧美一区二区在线观看 | 成年人免费在线视频 | 亚洲天堂色 | 日韩欧美精品一区二区 | 在线欧美 | 色婷婷香蕉在线一区二区 | 国产黄色大片 | 久久er99热精品一区二区 | 男女无遮挡xx00动态图120秒 | 日韩有码在线视频 | 在线看91 | 毛片免费播放 | 日批视频免费在线观看 | 久久福利视频导航 | 免费av观看| 四虎影视在线 | 成人一级毛片 | 福利小视频在线观看 | 久艹视频在线观看 | 免费视频一区 | 求av网址 | 91少妇丨porny丨 | 国产三级视频在线播放 | 精品国产乱码久久久久久88av | 国产伦精品一区二区三区照片 | 天天综合久久 | 特级丰满少妇一级aaaa爱毛片 | 精品国产乱码久久久久 | 成人免费毛片嘿嘿连载视频 | 欧美精品在线看 | 国产一级视频在线观看 | 免费理论片 | 黄免费网站 | 国产女人18毛片18精品 | 少妇久久久| 欧美日韩一二区 | 黄色大片在线 |