使用Node.js实现JWT

person 无限可能    watch_later 2024-07-12 22:24:00
visibility 164    class NodeJs,JWT,Token    bookmark 分享

使用Node.js实现JWT(JSON Web Token)认证是一个常见的任务,通常用于保护API的访问。以下是一个完整的代码示例,展示了如何在Node.js中使用JWT进行用户认证。我们将使用jsonwebtoken库来生成和验证JWT。

步骤

  1. 初始化Node.js项目并安装必要的依赖。
  2. 创建一个简单的用户注册和登录系统。
  3. 生成JWT并返回给客户端。
  4. 验证JWT以保护受保护的路由。

1. 初始化项目并安装依赖

首先,初始化一个新的Node.js项目并安装必要的依赖:

mkdir jwt-auth-example
cd jwt-auth-example
npm init -y
npm install express body-parser jsonwebtoken bcryptjs

2. 创建项目结构

创建以下文件结构:

jwt-auth-example
├── server.js
├── config.js
└── routes
    └── auth.js

3. 配置JWT密钥

config.js中定义JWT密钥:

module.exports = {
  jwtSecret: 'your_secret_key', // 替换为您的密钥
};

4. 实现用户注册和登录

routes/auth.js中实现用户注册和登录逻辑:

const express = require('express');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const config = require('../config');

const router = express.Router();

const users = []; // 临时存储用户

// 注册路由
router.post('/register', async (req, res) => {
  const { username, password } = req.body;

  const existingUser = users.find(user => user.username === username);
  if (existingUser) {
    return res.status(400).json({ message: 'User already exists' });
  }

  const hashedPassword = await bcrypt.hash(password, 10);

  const user = { username, password: hashedPassword };
  users.push(user);

  res.status(201).json({ message: 'User registered successfully' });
});

// 登录路由
router.post('/login', async (req, res) => {
  const { username, password } = req.body;

  const user = users.find(user => user.username === username);
  if (!user) {
    return res.status(400).json({ message: 'Invalid credentials' });
  }

  const isPasswordValid = await bcrypt.compare(password, user.password);
  if (!isPasswordValid) {
    return res.status(400).json({ message: 'Invalid credentials' });
  }

  const token = jwt.sign({ username: user.username }, config.jwtSecret, {
    expiresIn: '1h',
  });

  res.json({ token });
});

module.exports = router;

5. 保护受保护的路由

server.js中配置Express应用并保护受保护的路由:

const express = require('express');
const bodyParser = require('body-parser');
const jwt = require('jsonwebtoken');
const config = require('./config');
const authRoutes = require('./routes/auth');

const app = express();

app.use(bodyParser.json());

app.use('/auth', authRoutes);

// 保护路由中间件
const authenticateJWT = (req, res, next) => {
  const token = req.headers.authorization;

  if (!token) {
    return res.status(401).json({ message: 'Access denied. No token provided.' });
  }

  try {
    const decoded = jwt.verify(token, config.jwtSecret);
    req.user = decoded;
    next();
  } catch (error) {
    res.status(400).json({ message: 'Invalid token.' });
  }
};

// 受保护的路由
app.get('/protected', authenticateJWT, (req, res) => {
  res.json({ message: 'This is a protected route', user: req.user });
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});

6. 测试API

现在,您可以使用curl或Postman等工具测试API。

注册用户

curl -X POST http://localhost:3000/auth/register -H "Content-Type: application/json" -d '{"username": "testuser", "password": "testpass"}'

登录用户

curl -X POST http://localhost:3000/auth/login -H "Content-Type: application/json" -d '{"username": "testuser", "password": "testpass"}'

登录成功后,您将收到一个JWT令牌:

{
  "token": "YOUR_JWT_TOKEN"
}

访问受保护的路由

使用获取的JWT令牌访问受保护的路由:

curl -X GET http://localhost:3000/protected -H "Authorization: YOUR_JWT_TOKEN"

您将收到受保护路由的响应:

{
  "message": "This is a protected route",
  "user": {
    "username": "testuser",
    "iat": 1600000000,
    "exp": 1600003600
  }
}

总结

以上是一个完整的Node.js示例,展示了如何实现JWT认证。通过这个示例,您可以了解如何生成和验证JWT令牌,并保护您的API路由。希望这对您有所帮助。

评论区
评论列表
menu