security

package
v3.1.32 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 21, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package security 提供安全 HTTP 响应头中间件,设置 HSTS、CSP、X-Content-Type-Options 等。

使用方式:

app.Use(security.New(security.DefaultConfig))

零开销 — 仅在首次响应时注入一组静态 header。

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = Config{
	HSTS:               "max-age=31536000; includeSubDomains",
	XFrameOptions:      "DENY",
	ContentTypeOptions: "nosniff",
	XSSProtection:      "1; mode=block",
	ReferrerPolicy:     "strict-origin-when-cross-origin",
}

DefaultConfig 生产环境推荐的安全头配置。

View Source
var StrictConfig = Config{
	HSTS:               "max-age=63072000; includeSubDomains; preload",
	XFrameOptions:      "DENY",
	ContentTypeOptions: "nosniff",
	XSSProtection:      "0",
	ReferrerPolicy:     "no-referrer",
	PermissionsPolicy:  "camera=(), microphone=(), geolocation=()",
}

StrictConfig 比 DefaultConfig 更严格,增加 PermissionsPolicy。

Functions

func New

func New(conf ...Config) core.HandlerFunc

New 返回安全 HTTP 头中间件。

Types

type Config

type Config struct {
	// HSTS 设置 Strict-Transport-Security,如 "max-age=31536000; includeSubDomains"。空字符串表示不设置。
	HSTS string
	// XFrameOptions 设置 X-Frame-Options,如 "DENY"、"SAMEORIGIN"。空字符串表示不设置。
	XFrameOptions string
	// ContentTypeOptions 设置 X-Content-Type-Options,通常 "nosniff"。空字符串表示不设置。
	ContentTypeOptions string
	// XSSProtection 设置 X-XSS-Protection,如 "1; mode=block"。空字符串表示不设置。
	XSSProtection string
	// ReferrerPolicy 设置 Referrer-Policy,如 "strict-origin-when-cross-origin"。
	ReferrerPolicy string
	// PermissionsPolicy 设置 Permissions-Policy,如 "camera=(), microphone=()"。
	PermissionsPolicy string
	// Skip 可选跳过函数
	Skip func(c core.Ctx) bool
}

Config 安全头配置

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL