身份驗(yàn)證是Web開發(fā)的重要組成部分。JSON Web令牌(JWT)由于其簡(jiǎn)單性,安全性和可擴(kuò)展性,已成為在Web應(yīng)用程序中實(shí)現(xiàn)身份驗(yàn)證的流行方法。在這篇文章中,我將指導(dǎo)你在Node.js應(yīng)用程序中使用MongoDB進(jìn)行數(shù)據(jù)存儲(chǔ)來實(shí)現(xiàn)JWT身份驗(yàn)證。
在開始之前,我假設(shè)你已經(jīng)安裝了Node.js、MongoDB和VS Code,并且你知道如何創(chuàng)建MongoDB數(shù)據(jù)庫(kù)和基本的RESTful API。
JWT身份驗(yàn)證依賴于JSON Web令牌來確認(rèn)Web應(yīng)用中用戶的身份。JSON Web令牌是使用密鑰對(duì)進(jìn)行數(shù)字簽名的編碼JSON對(duì)象。
簡(jiǎn)而言之,JWT身份驗(yàn)證就像為網(wǎng)站提供一個(gè)密碼。一旦你登錄成功,你就得到了這個(gè)密碼。
JSON Web Token由三部分組成,由點(diǎn)(.)分隔:
以下是JWT的基本結(jié)構(gòu):
xxxx.yyyy.zzzz當(dāng)你登錄成功時(shí),你會(huì)得到這個(gè)代碼。每次你想訪問某個(gè)數(shù)據(jù)時(shí),你都要攜帶這個(gè)代碼來證明是你。系統(tǒng)會(huì)檢查代碼是否有效,然后讓你獲取數(shù)據(jù)!
接下來讓我們看看在node.js項(xiàng)目中進(jìn)行JWT身份驗(yàn)證的步驟。
首先為您的項(xiàng)目創(chuàng)建一個(gè)新目錄,并使用以下命令進(jìn)入到該目錄。
mkdir nodejs-jwt-authcd nodejs-jwt-auth通過在終端中運(yùn)行以下命令初始化項(xiàng)目(確保您位于新創(chuàng)建的項(xiàng)目文件夾中)。
npm init -y接下來通過以下命令安裝必要的依賴項(xiàng):
npm install express mongoose jsonwebtoken dotenv上面的命令將安裝:
現(xiàn)在您的package.json文件應(yīng)該看起來像這樣:
圖片
要連接MongoDB數(shù)據(jù)庫(kù),請(qǐng)查看以下鏈接中的具體操作流程。
https://shefali.dev/restful-api/#Step_4_Creating_a_MongoDB_Database
為了 MongoDB 連接地址的安全,讓我們?cè)诟夸浵聞?chuàng)建一個(gè)名為 .env 的新文件。
將以下代碼添加到.env文件中。
MONGODB_URL=<Your MongoDB Connection String>SECRET_KEY="your_secret_key_here"將<Your MongoDB Connection String>替換為您從MongoDB Atlas獲得的連接字符串(在步驟2中),并將your_secret_key_here替換為您想要的密鑰字符串。現(xiàn)在你的.env文件應(yīng)該是這樣的。
MONGODB_URL='mongodb+srv://shefali:********@cluster0.sscvg.mongodb.net/nodejs-jwt-auth'SECRET_KEY="ThisIsMySecretKey"在MONGODB_URL最后我們加入node.js-jwt-auth,這是我們的數(shù)據(jù)庫(kù)名稱。
在根目錄下創(chuàng)建一個(gè)名為index.js的文件,并將以下代碼添加到該文件中。
const express = require("express");const mongoose = require("mongoose");require("dotenv").config(); //for using variables from .env file.const app = express();const port = 3000;//middleware provided by Express to parse incoming JSON requests.app.use(express.json()); mongoose.connect(process.env.MONGODB_URL).then(() => { console.log("MongoDB is connected!");});app.get("/", (req, res) => { res.send("Hello World!");});app.listen(port, () => { console.log(`Server is listening on port ${port}`);});現(xiàn)在我們可以通過以下命令運(yùn)行服務(wù)器。
node index.js輸出應(yīng)如下圖所示。
圖片
通過使用命令node index.js,您必須在每次更改文件時(shí)重新啟動(dòng)服務(wù)器。為了避免這種情況,您可以使用以下命令安裝nodemon。
npm install -g nodemon現(xiàn)在使用下面的命令運(yùn)行服務(wù)器,它會(huì)在每次更改文件時(shí)自動(dòng)重新啟動(dòng)服務(wù)器。
nodemon index.js在根目錄下創(chuàng)建一個(gè)名為models的新目錄,并在其中創(chuàng)建一個(gè)名為User.js的新文件。
圖片
現(xiàn)在讓我們?yōu)槲覀兊捻?xiàng)目創(chuàng)建一個(gè)簡(jiǎn)單的模型,將以下代碼添加到User.js文件中。
const mongoose = require("mongoose");const userSchema = new mongoose.Schema({ username: { type: String, required: true, unique: true, }, password: { type: String, required: true, },});module.exports = mongoose.model("User", userSchema);在根目錄中,創(chuàng)建一個(gè)名為routes的新目錄,并在其中創(chuàng)建一個(gè)名為auth.js的文件。
圖片
然后將以下代碼添加到該文件中:
const express = require("express");const jwt = require("jsonwebtoken");const User = require("../models/User");const router = express.Router();// Signup routerouter.post("/signup", async (req, res) => { try { const { username, password } = req.body; const user = new User({ username, password }); await user.save(); res.status(201).json({ message: "New user registered successfully" }); } catch (error) { res.status(500).json({ message: "Internal server error" }); }});// Login routerouter.post("/login", async (req, res) => { const { username, password } = req.body; try { const user = await User.findOne({ username }); if (!user) { return res.status(401).json({ message: "Invalid username or password" }); } if (user.password !== password) { return res.status(401).json({ message: 'Invalid username or password' }); } // Generate JWT token const token = jwt.sign( { id: user._id, username: user.username }, process.env.SECRET_KEY ); res.json({ token }); } catch (error) { res.status(500).json({ message: "Internal server error" }); }});module.exports = router;導(dǎo)入依賴:
const express = require("express");const jwt = require("jsonwebtoken");const User = require("../models/User");const router = express.Router();在這里,我們導(dǎo)入以下依賴項(xiàng):
注冊(cè)路由:
// Signup routerouter.post("/signup", async (req, res) => { try { const { username, password } = req.body; const user = new User({ username, password }); await user.save(); res.status(201).json({ message: "New user registered successfully" }); } catch (error) { res.status(500).json({ message: "Internal server error" }); }});登錄路由:
// Login routerouter.post("/login", async (req, res) => { const { username, password } = req.body; try { const user = await User.findOne({ username }); if (!user) { return res.status(401).json({ message: "Invalid username or password" }); } if (user.password !== password) { return res.status(401).json({ message: 'Invalid username or password' }); } // Generate JWT token const token = jwt.sign( { id: user._id, username: user.username }, process.env.SECRET_KEY ); res.json({ token }); } catch (error) { res.status(500).json({ message: "Internal server error" }); }});最后路由被導(dǎo)出以在index.js文件中使用。
module.exports = router;在根目錄中,創(chuàng)建一個(gè)名為middleware.js的新文件,并將以下代碼添加到該文件中。
const jwt = require("jsonwebtoken");function verifyJWT(req, res, next) { const token = req.headers["authorization"]; if (!token) { return res.status(401).json({ message: "Access denied" }); } jwt.verify(token, process.env.SECRET_KEY, (err, data) => { if (err) { return res.status(401).json({ message: "Failed to authenticate token" }); } req.user = data; next(); });}module.exports = verifyJWT;此代碼是一個(gè)中間件函數(shù),用于在應(yīng)用程序中驗(yàn)證JSON Web令牌(JWT)。
分解上面的代碼:
現(xiàn)在要驗(yàn)證JWT,請(qǐng)修改index.js,如下所示:
const express = require('express');const authRouter = require('./routes/auth');const mongoose = require("mongoose");const verifyJWT = require("./middleware")require("dotenv").config(); //for using variables from .env file.const app = express();const PORT = 3000;mongoose.connect(process.env.MONGODB_URL).then(() => { console.log("MongoDB is connected!");});app.use(express.json());//Authentication routeapp.use('/auth', authRouter);//decodeDetails Routeapp.get('/decodeDetails', verifyJWT, (req, res) => { const { username } = req.user; res.json({ username });});app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`);});在上面的代碼中,/auth路由是authRouter處理,其中包含的終端用戶認(rèn)證,例如登錄和注冊(cè)。
app.get('/decodeDetails', verifyJWT, (req, res) => { const { username } = req.user; res.json({ username });});向http://localhost:3000/auth/signup發(fā)送一個(gè)POST請(qǐng)求,其中包含Headers Content-Type : application/json和以下JSON主體:
{ "username": "shefali", "password": "12345678"}在響應(yīng)中,您將看到消息“新用戶注冊(cè)成功”。
向http://localhost:3000/auth/login發(fā)送一個(gè)POST請(qǐng)求,其中包含Header Content-Type : application/json和JSON主體以及用戶名和密碼,這是您在注冊(cè)路由中創(chuàng)建的。
{ "username": "shefali", "password": "12345678"}在響應(yīng)中,您將收到一個(gè)令牌。記下這個(gè)令牌,因?yàn)樵跍y(cè)試decodeDetails路由時(shí)需要它。
向http://localhost:3000/decodeDetails發(fā)送一個(gè)GET請(qǐng)求,并帶有一個(gè)帶有令牌值的Authorization頭(您在測(cè)試登錄路由時(shí)得到了它)。
在響應(yīng)中,您將獲得用戶名。恭喜你!
本文鏈接:http://m.www897cc.com/showinfo-26-71944-0.htmlNodejs - 九步開啟JWT身份驗(yàn)證
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com