初始化SSE服务端项目
This commit is contained in:
28
middlewares/security.js
Normal file
28
middlewares/security.js
Normal file
@ -0,0 +1,28 @@
|
||||
/**
|
||||
* @file 安全相关中间件集合
|
||||
* @author Yk <yk_9001@icloud.com>
|
||||
* @date 2025-07-01
|
||||
* @description 包含helmet、CORS和限流中间件
|
||||
*/
|
||||
|
||||
// 引入必要的安全中间件模块
|
||||
const helmet = require('helmet'); // 用于设置 HTTP 头,增强前端安全防护
|
||||
const cors = require('cors'); // 跨域资源共享中间件,允许指定来源访问
|
||||
const rateLimit = require('express-rate-limit');// 请求频率限制,防止 DDoS 或暴力攻击
|
||||
const config = require('../config'); // 应用配置文件,包含安全策略参数
|
||||
|
||||
/**
|
||||
* 导出中间件数组,按顺序应用以下安全策略:
|
||||
* 1. Helmet:设置各种 HTTP 安全头,防止常见漏洞
|
||||
* 2. CORS:配置允许的跨域请求来源、方法(GET/POST)并支持凭证
|
||||
* 3. Rate Limit:根据配置限制客户端请求频率
|
||||
*/
|
||||
module.exports = [
|
||||
helmet(), // 使用默认或自定义配置启用安全头防护
|
||||
cors({
|
||||
origin: config.sse.allowedOrigins, // 允许的跨域请求源,从配置中读取
|
||||
methods: ['GET', 'POST'], // 支持的 HTTP 方法
|
||||
credentials: true // 允许携带凭证(如 cookies)
|
||||
}),
|
||||
rateLimit(config.rateLimit) // 应用请求频率限制策略,配置来自 config
|
||||
];
|
Reference in New Issue
Block a user