使用Node.js实现JWT(JSON Web Token)认证是一个常见的任务,通常用于保护API的访问。以下是一个完整的代码示例,展示了如何在Node.js中使用JWT进行用户认证。我们将使用jsonwebtoken
库来生成和验证JWT。
首先,初始化一个新的Node.js项目并安装必要的依赖:
mkdir jwt-auth-example
cd jwt-auth-example
npm init -y
npm install express body-parser jsonwebtoken bcryptjs
创建以下文件结构:
jwt-auth-example
├── server.js
├── config.js
└── routes
└── auth.js
在config.js
中定义JWT密钥:
module.exports = {
jwtSecret: 'your_secret_key', // 替换为您的密钥
};
在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;
在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}`);
});
现在,您可以使用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路由。希望这对您有所帮助。