跳至主要内容
版本:v2.x

Helmet

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

签名

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

示例

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")
}

测试

curl -I https://127.0.0.1:3000

配置

属性类型说明默认值
下一个func(*fiber.Ctx) boolNext 定义一个函数来跳过中间件。nil
XSSProtectionstringXSSProtection"0"
ContentTypeNosniffstringContentTypeNosniff"nosniff"
XFrameOptionsstringXFrameOptions"SAMEORIGIN"
HSTSMaxAgeintHSTSMaxAge0
HSTSExcludeSubdomainsboolHSTSExcludeSubdomainsfalse
ContentSecurityPolicystringContentSecurityPolicy""
CSPReportOnlyboolCSPReportOnlyfalse
HSTSPreloadEnabledboolHSTSPreloadEnabledfalse
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"
XDNSPrefetchControlstringX-DNS-Prefetch-Control"off"
XDownloadOptionsstringX-Download-Options"noopen"
XPermittedCrossDomainstringX-Permitted-Cross-Domain-Policies"none"

默认配置

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",
}