Files
sse-server/middlewares/security.js
2025-07-01 20:07:24 +08:00

29 lines
1.3 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @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
];