security

package
v0.25.3 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIKeyAuth

type APIKeyAuth struct {
	Keys       []string `mapstructure:"keys" yaml:"keys" json:"keys"`                     // 有效API Key列表
	HeaderName string   `mapstructure:"header-name" yaml:"header-name" json:"headerName"` // API Key头名称
	QueryParam string   `mapstructure:"query-param" yaml:"query-param" json:"queryParam"` // API Key查询参数名
}

APIKeyAuth API Key认证配置

type Auth

type Auth struct {
	Enabled     bool        `mapstructure:"enabled" yaml:"enabled" json:"enabled"`               // 是否启用认证
	Type        string      `mapstructure:"type" yaml:"type" json:"type"`                        // 认证类型 (basic, bearer, custom, apikey)
	HeaderName  string      `mapstructure:"header-name" yaml:"header-name" json:"headerName"`    // 认证头名称
	TokenPrefix string      `mapstructure:"token-prefix" yaml:"token-prefix" json:"tokenPrefix"` // 令牌前缀
	Basic       *BasicAuth  `mapstructure:"basic" yaml:"basic" json:"basic"`                     // Basic认证
	Bearer      *BearerAuth `mapstructure:"bearer" yaml:"bearer" json:"bearer"`                  // Bearer认证
	APIKey      *APIKeyAuth `mapstructure:"apikey" yaml:"apikey" json:"apikey"`                  // APIKey认证
	Custom      *CustomAuth `mapstructure:"custom" yaml:"custom" json:"custom"`                  // 自定义认证
}

Auth 通用认证配置 - 支持多种认证方式

type BasicAuth

type BasicAuth struct {
	Users []User `mapstructure:"users" yaml:"users" json:"users"` // 用户列表
}

BasicAuth Basic认证配置

type BearerAuth

type BearerAuth struct {
	Tokens []string `mapstructure:"tokens" yaml:"tokens" json:"tokens"` // 有效令牌列表
}

BearerAuth Bearer认证配置

type CSP

type CSP struct {
	Enabled bool   `mapstructure:"enabled" yaml:"enabled" json:"enabled"` // 是否启用CSP
	Mode    string `mapstructure:"mode" yaml:"mode" json:"mode"`          // CSP模式: strict, development, balanced, relaxed, api, custom
	Custom  string `mapstructure:"custom" yaml:"custom" json:"custom"`    // 自定义CSP策略(当mode=custom时使用)
}

CSP 内容安全策略配置

func (*CSP) GetPolicy

func (c *CSP) GetPolicy() string

GetPolicy 获取CSP策略字符串

type CSRF

type CSRF struct {
	Enabled         bool     `mapstructure:"enabled" yaml:"enabled" json:"enabled"`                             // 是否启用 CSRF 防护
	ExemptMethods   []string `mapstructure:"exempt-methods" yaml:"exempt-methods" json:"exemptMethods"`         // 豁免校验的 HTTP 方法
	TokenFormField  string   `mapstructure:"token-form-field" yaml:"token-form-field" json:"tokenFormField"`    // 表单字段名
	TokenCookieName string   `mapstructure:"token-cookie-name" yaml:"token-cookie-name" json:"tokenCookieName"` // Cookie 名称
	TokenLength     int      `mapstructure:"token-length" yaml:"token-length" json:"tokenLength"`               // Token 长度
	Secret          string   `mapstructure:"secret" yaml:"secret" json:"secret"`                                // Token 生成密钥
}

CSRF 防护配置

func DefaultCSRF

func DefaultCSRF() *CSRF

DefaultCSRF 创建默认 CSRF 配置

type CustomAuth

type CustomAuth struct {
	HeaderName    string            `mapstructure:"header-name" yaml:"header-name" json:"headerName"`          // 自定义头名称
	ExpectedValue string            `mapstructure:"expected-value" yaml:"expected-value" json:"expectedValue"` // 期望值
	Headers       map[string]string `mapstructure:"headers" yaml:"headers" json:"headers"`                     // 自定义头部验证
}

CustomAuth 自定义认证配置

type JWT

type JWT struct {
	Enabled   bool   `mapstructure:"enabled" yaml:"enabled" json:"enabled"`       // 是否启用JWT
	Secret    string `mapstructure:"secret" yaml:"secret" json:"secret"`          // JWT密钥
	Expiry    int    `mapstructure:"expiry" yaml:"expiry" json:"expiry"`          // 过期时间(小时)
	Issuer    string `mapstructure:"issuer" yaml:"issuer" json:"issuer"`          // 签发者
	Algorithm string `mapstructure:"algorithm" yaml:"algorithm" json:"algorithm"` // 加密算法
}

JWT JWT配置

type Pprof

type Pprof struct {
	Enabled      bool     `mapstructure:"enabled" yaml:"enabled" json:"enabled"`                  // 是否启用安全认证
	Username     string   `mapstructure:"username" yaml:"username" json:"username"`               // 用户名
	Password     string   `mapstructure:"password" yaml:"password" json:"password"`               // 密码
	AllowedIPs   []string `mapstructure:"allowed-ips" yaml:"allowed-ips" json:"allowedIps"`       // 允许的IP地址
	RequireHTTPS bool     `mapstructure:"require-https" yaml:"require-https" json:"requireHttps"` // 是否要求HTTPS
}

Pprof 安全配置

type Protection

type Protection struct {
	Swagger *ServiceProtection `mapstructure:"swagger" yaml:"swagger" json:"swagger"` // Swagger保护
	PProf   *ServiceProtection `mapstructure:"pprof" yaml:"pprof" json:"pprof"`       // PProf保护
	Metrics *ServiceProtection `mapstructure:"metrics" yaml:"metrics" json:"metrics"` // Metrics保护
	Health  *ServiceProtection `mapstructure:"health" yaml:"health" json:"health"`    // Health保护
	API     *ServiceProtection `mapstructure:"api" yaml:"api" json:"api"`             // API保护
}

Protection 服务保护配置 - 针对不同服务的安全设置

type Security

type Security struct {
	ModuleName string      `mapstructure:"module-name" yaml:"module-name" json:"moduleName"` // 模块名称
	JWT        *JWT        `mapstructure:"jwt" yaml:"jwt" json:"jwt"`                        // JWT配置
	Auth       *Auth       `mapstructure:"auth" yaml:"auth" json:"auth"`                     // 通用认证配置
	Protection *Protection `mapstructure:"protection" yaml:"protection" json:"protection"`   // 服务保护配置
	CSP        *CSP        `mapstructure:"csp" yaml:"csp" json:"csp"`                        // CSP内容安全策略配置
	CSRF       *CSRF       `mapstructure:"csrf" yaml:"csrf" json:"csrf"`                     // CSRF 防护配置
	Validation *Validation `mapstructure:"validation" yaml:"validation" json:"validation"`   // 安全校验规则
}

Security 统一安全配置 - 直接使用Security而不是SecurityConfig

func Default

func Default() *Security

Default 创建默认安全配置

func (*Security) Clone

func (s *Security) Clone() internal.Configurable

Clone 返回配置的副本

func (*Security) EnableAuth

func (s *Security) EnableAuth() *Security

EnableAuth 启用认证

func (*Security) EnableCSP

func (s *Security) EnableCSP() *Security

EnableCSP 启用CSP

func (*Security) EnableJWT

func (s *Security) EnableJWT() *Security

EnableJWT 启用JWT

func (*Security) Get

func (s *Security) Get() interface{}

Get 返回配置接口

func (*Security) IsAuthEnabled

func (s *Security) IsAuthEnabled() bool

IsAuthEnabled 检查认证是否启用

func (*Security) IsCSPEnabled

func (s *Security) IsCSPEnabled() bool

IsCSPEnabled 检查CSP是否启用

func (*Security) IsJWTEnabled

func (s *Security) IsJWTEnabled() bool

IsJWTEnabled 检查JWT是否启用

func (*Security) Set

func (s *Security) Set(data interface{})

Set 设置配置数据

func (*Security) Validate

func (s *Security) Validate() error

Validate 验证配置

func (*Security) WithAuth

func (s *Security) WithAuth(enabled bool, authType, headerName, tokenPrefix string) *Security

WithAuth 设置认证配置

func (*Security) WithCSP

func (s *Security) WithCSP(enabled bool, mode, custom string) *Security

WithCSP 设置CSP配置

func (*Security) WithJWT

func (s *Security) WithJWT(enabled bool, secret string, expiry int, issuer, algorithm string) *Security

WithJWT 设置JWT配置

func (*Security) WithModuleName

func (s *Security) WithModuleName(moduleName string) *Security

WithModuleName 设置模块名称

type ServiceProtection

type ServiceProtection struct {
	Enabled      bool     `mapstructure:"enabled" yaml:"enabled" json:"enabled"`                  // 是否启用保护
	AuthRequired bool     `mapstructure:"auth-required" yaml:"auth-required" json:"authRequired"` // 是否需要认证
	AuthType     string   `mapstructure:"auth-type" yaml:"auth-type" json:"authType"`             // 认证类型
	IPWhitelist  []string `mapstructure:"ip-whitelist" yaml:"ip-whitelist" json:"ipWhitelist"`    // IP白名单
	RequireHTTPS bool     `mapstructure:"require-https" yaml:"require-https" json:"requireHttps"` // 是否要求HTTPS
	Username     string   `mapstructure:"username" yaml:"username" json:"username"`               // 用户名
	Password     string   `mapstructure:"password" yaml:"password" json:"password"`               // 密码
}

ServiceProtection 服务保护配置 - 统一的服务安全配置

type User

type User struct {
	Username    string   `mapstructure:"username" yaml:"username" json:"username"`          // 用户名
	Password    string   `mapstructure:"password" yaml:"password" json:"password"`          // 密码(建议加密存储)
	Role        string   `mapstructure:"role" yaml:"role" json:"role"`                      // 角色
	Permissions []string `mapstructure:"permissions" yaml:"permissions" json:"permissions"` // 权限列表
}

User 用户信息

type Validation

type Validation struct {
	HTTPMethods         []string `mapstructure:"http-methods" yaml:"http-methods" json:"httpMethods"`                         // 有效 HTTP 方法集合
	DangerousExtensions []string `mapstructure:"dangerous-extensions" yaml:"dangerous-extensions" json:"dangerousExtensions"` // 危险文件扩展名
	XSSPatterns         []string `mapstructure:"xss-patterns" yaml:"xss-patterns" json:"xssPatterns"`                         // XSS 检测模式
}

Validation 安全校验规则配置

func DefaultValidation

func DefaultValidation() *Validation

DefaultValidation 创建默认安全校验规则

Jump to

Keyboard shortcuts

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