gin_middleware

package module
v0.0.0-...-5715289 Latest Latest
Warning

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

Go to latest
Published: May 20, 2020 License: Apache-2.0 Imports: 20 Imported by: 0

README

gin-middleware

介绍

收集以及完善gin的各种中间件,使gin用起来更加方便,本中间件的编写参考了echo框架的官方中间件

软件架构

软件架构说明

安装教程
  1. xxxx
  2. xxxx
  3. xxxx
使用说明
  1. xxxx
  2. xxxx
  3. xxxx
参与贡献
  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request
码云特技
  1. 使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.md, Readme_zh.md
  2. 码云官方博客 blog.gitee.com
  3. 你可以 https://gitee.com/explore 这个地址来了解码云上的优秀开源项目
  4. GVP 全称是码云最有价值开源项目,是码云综合评定出的优秀开源项目
  5. 码云官方提供的使用手册 https://gitee.com/help
  6. 码云封面人物是一档用来展示码云会员风采的栏目 https://gitee.com/gitee-stars/

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrJWTMissing = errors.New("未找到token")

	DefaultJWTConfig = JwtConfig{
		Skipper:       DefaultSkipper,
		SigningMethod: algorithmHS256,
		TokenLookup:   "header:" + consts.HeaderAuthorization,
		AuthScheme:    "Bearer",
		SigningKey:    "111111111111111111",
		Expire:        time.Hour,
		Unauthorized: func(ctx *gin.Context, httpCode int, err error) {
			ctx.Status(httpCode)
			ctx.Abort()
		},
	}
)
View Source
var DefaultCORSConfig = CORSConfig{
	Skipper:         DefaultSkipper,
	AllowAllOrigins: true,
	AllowOrigins:    []string{"*"},
	AllowMethods: []string{
		http.MethodGet,
		http.MethodHead,
		http.MethodPut,
		http.MethodPatch,
		http.MethodPost,
		http.MethodDelete,
	},
}
View Source
var (
	DefaultMethodOverrideConfig = MethodOverrideConfig{
		Skipper: DefaultSkipper,
		Getter:  MethodFromHeader(consts.HeaderXHTTPMethodOverride),
	}
)
View Source
var (
	DefaultSecureConfig = SecureConfig{
		Skipper:            DefaultSkipper,
		XSSProtection:      "1; mode=block",
		ContentTypeNosniff: "nosniff",
		XFrameOptions:      "SAMEORIGIN",
		HSTSPreloadEnabled: false,
	}
)

Functions

func DefaultCORS

func DefaultCORS() gin.HandlerFunc

func DefaultLogger

func DefaultLogger() gin.HandlerFunc

func DefaultMethodOverride

func DefaultMethodOverride() gin.HandlerFunc

func DefaultRecover

func DefaultRecover() gin.HandlerFunc

func DefaultRequestID

func DefaultRequestID() gin.HandlerFunc

func DefaultSecureMiddleware

func DefaultSecureMiddleware() gin.HandlerFunc

func DefaultSkipper

func DefaultSkipper(c *gin.Context) bool

默认不跳过验证

func DefaultTokenValidator

func DefaultTokenValidator(c *gin.Context) bool

默认算法token生成算法为 secret + "-" + 时间戳 + "-" + 8位随机数

func NewCORS

func NewCORS(config *CORSConfig) gin.HandlerFunc

func NewCSRF

func NewCSRF(c CSRFConfig) gin.HandlerFunc

func NewLogger

func NewLogger(fs ...AfterResponseFunc) gin.HandlerFunc

func NewMethodOverride

func NewMethodOverride(config MethodOverrideConfig) gin.HandlerFunc

func NewRecover

func NewRecover(c RecoverConfig) gin.HandlerFunc

func NewRequestID

func NewRequestID(c RequestIDConfig) gin.HandlerFunc

func NewSecureMiddleware

func NewSecureMiddleware(c SecureConfig) gin.HandlerFunc

Types

type AfterResponseFunc

type AfterResponseFunc func(ctx *gin.Context, l LogMap)

NOTICE:

type CORSConfig

type CORSConfig struct {
	Skipper          Skipper
	AllowAllOrigins  bool     `mapstructure:"allowAllOrigins"`
	AllowOrigins     []string `mapstructure:"allowOrigins"`
	AllowOriginFunc  func(origin string) bool
	AllowMethods     []string `mapstructure:"allowMethods"`
	AllowHeaders     []string `mapstructure:"allowHeaders"`
	AllowCredentials bool     `mapstructure:"allowCredentials"`
	ExposeHeaders    []string `mapstructure:"exposeHeaders"`
	MaxAge           int64    `mapstructure:"maxAge"`
}

func (*CORSConfig) Validate

func (c *CORSConfig) Validate() error

验证是用户定义的检查配置。

type CSRFConfig

type CSRFConfig struct {
	Skipper           Skipper
	QueryAllowHosts   func() []string
	QueryAllowPattern func() []string
	Validator         func(c *gin.Context) bool
}

type JwtConfig

type JwtConfig struct {
	Skipper       Skipper
	TokenLookup   string
	SigningMethod string
	AuthScheme    string
	SigningKey    string
	// 过期时间 单位为分钟
	Expire time.Duration
	// 验证登陆用户身份是否合规
	Authenticator func(ctx *gin.Context, claims jwt.MapClaims, token string) error

	// 未登录时候的回调函数
	Unauthorized func(ctx *gin.Context, httpCode int, err error)
}

type JwtEntity

type JwtEntity struct {
	// contains filtered or unexported fields
}

func NewJwt

func NewJwt(c JwtConfig) JwtEntity

func (JwtEntity) GenerateJwtToken

func (entity JwtEntity) GenerateJwtToken(claims jwt.MapClaims) (token string, expired int64, err error)

生成token

func (JwtEntity) Middleware

func (entity JwtEntity) Middleware() gin.HandlerFunc

func (*JwtEntity) RefreshToken

func (entity *JwtEntity) RefreshToken(ctx *gin.Context) (tokenStr string, exp int64, err error)

func (*JwtEntity) ResolveToken

func (entity *JwtEntity) ResolveToken(tokenString string) (jwt.MapClaims, error)

解析token

type LogMap

type LogMap map[string]interface{}

type MethodOverrideConfig

type MethodOverrideConfig struct {
	Skipper Skipper
	Getter  MethodOverrideGetter
}

type MethodOverrideGetter

type MethodOverrideGetter func(ctx *gin.Context) string

func MethodFromForm

func MethodFromForm(param string) MethodOverrideGetter

func MethodFromHeader

func MethodFromHeader(header string) MethodOverrideGetter

func MethodFromQuery

func MethodFromQuery(param string) MethodOverrideGetter

type RecoverConfig

type RecoverConfig struct {
	Skipper           Skipper
	StackSize         int  `mapstructure:"stack_size"`
	DisableStackAll   bool `mapstructure:"disable_stack_all"`
	DisablePrintStack bool `mapstructure:"disable_print_stack"`
}

type RequestIDConfig

type RequestIDConfig struct {
	Skipper   Skipper
	Generator func() string
}

type ResponseWriter

type ResponseWriter struct {
	gin.ResponseWriter
	// contains filtered or unexported fields
}

用于读取返回信息

func (ResponseWriter) Write

func (w ResponseWriter) Write(b []byte) (int, error)

func (ResponseWriter) WriteString

func (w ResponseWriter) WriteString(b string) (int, error)

type SecureConfig

type SecureConfig struct {
	Skipper               Skipper
	XSSProtection         string `mapstructure:"xss_protection"`
	ContentTypeNosniff    string `mapstructure:"content_type_nosniff"`
	XFrameOptions         string `mapstructure:"x_frame_options"`
	HSTSMaxAge            int    `mapstructure:"hsts_max_age"`
	HSTSExcludeSubdomains bool   `mapstructure:"hsts_exclude_subdomains"`
	ContentSecurityPolicy string `mapstructure:"content_security_policy"`
	CSPReportOnly         bool   `mapstructure:"csp_report_only"`
	HSTSPreloadEnabled    bool   `mapstructure:"hsts_preload_enabled"`
	ReferrerPolicy        string `mapstructure:"referrer_policy"`
}

type Skipper

type Skipper func(c *gin.Context) bool

跳过验证函数

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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