Helmet

原文: https://docs.gofiber.io/api/middleware/helmet

Helmet

Helmet middleware helps secure your apps by setting various HTTP headers.

​ Helmet 中间件通过设置各种 HTTP 头来帮助保护您的应用。

Signatures 签名

1
func New(config ...Config) fiber.Handler

Examples 示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
package main

import (
  "github.com/gofiber/fiber/v2"
  "github.com/gofiber/fiber/v2/middleware/helmet"
)

func main() {
  app := fiber.New()

  app.Use(helmet.New())

  app.Get("/", func(c *fiber.Ctx) error {
    return c.SendString("Welcome!")
  })

  app.Listen(":3000")
}

Test: 测试:

curl -I http://localhost:3000

Config 配置

Property 属性Type 输入Description 说明Default 默认
Next 下一步func(*fiber.Ctx) boolNext defines a function to skip middleware. 接下来定义一个跳过中间件的函数。nil
XSSProtectionstringXSSProtection“0”
ContentTypeNosniffstringContentTypeNosniff“nosniff” “nosniff”
XFrameOptionsstringXFrameOptions“SAMEORIGIN” “SAMEORIGIN”
HSTSMaxAgeintHSTSMaxAge HSTS 最大生存期0
HSTSExcludeSubdomainsboolHSTSExcludeSubdomains HSTS 排除子域false
ContentSecurityPolicystringContentSecurityPolicy 内容安全策略""
CSPReportOnlyboolCSPReportOnly CSP 仅报告false
HSTSPreloadEnabledboolHSTSPreloadEnabled 启用 HSTS 预加载false
ReferrerPolicystringReferrerPolicy“ReferrerPolicy”
PermissionPolicystringPermissions-Policy""
CrossOriginEmbedderPolicystringCross-Origin-Embedder-Policy“require-corp”
CrossOriginOpenerPolicystringCross-Origin-Opener-Policy“same-origin” “同源”
CrossOriginResourcePolicystringCross-Origin-Resource-Policy“same-origin” “同源”
OriginAgentClusterstringOrigin-Agent-Cluster“?1” “?1”
XDNSPrefetchControlstringX-DNS-Prefetch-Control“off” “off”
XDownloadOptionsstringX-Download-Options“noopen”
XPermittedCrossDomainstringX-Permitted-Cross-Domain-Policies“none”

Default Config 默认配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
var ConfigDefault = Config{
    XSSProtection:             "0",
    ContentTypeNosniff:        "nosniff",
    XFrameOptions:             "SAMEORIGIN",
    ReferrerPolicy:            "no-referrer",
    CrossOriginEmbedderPolicy: "require-corp",
    CrossOriginOpenerPolicy:   "same-origin",
    CrossOriginResourcePolicy: "same-origin",
    OriginAgentCluster:        "?1",
    XDNSPrefetchControl:       "off",
    XDownloadOptions:          "noopen",
    XPermittedCrossDomain:     "none",
}
最后修改 October 10, 2024: 更新 (a4b8f85)