Documentation ¶
Index ¶
- Variables
- func DefaultCORS() gin.HandlerFunc
- func DefaultLogger() gin.HandlerFunc
- func DefaultMethodOverride() gin.HandlerFunc
- func DefaultRecover() gin.HandlerFunc
- func DefaultRequestID() gin.HandlerFunc
- func DefaultSecureMiddleware() gin.HandlerFunc
- func DefaultSkipper(c *gin.Context) bool
- func DefaultTokenValidator(c *gin.Context) bool
- func NewCORS(config *CORSConfig) gin.HandlerFunc
- func NewCSRF(c CSRFConfig) gin.HandlerFunc
- func NewLogger(fs ...AfterResponseFunc) gin.HandlerFunc
- func NewMethodOverride(config MethodOverrideConfig) gin.HandlerFunc
- func NewRecover(c RecoverConfig) gin.HandlerFunc
- func NewRequestID(c RequestIDConfig) gin.HandlerFunc
- func NewSecureMiddleware(c SecureConfig) gin.HandlerFunc
- type AfterResponseFunc
- type CORSConfig
- type CSRFConfig
- type JwtConfig
- type JwtEntity
- func (entity JwtEntity) GenerateJwtToken(claims jwt.MapClaims) (token string, expired int64, err error)
- func (entity JwtEntity) Middleware() gin.HandlerFunc
- func (entity *JwtEntity) RefreshToken(ctx *gin.Context) (tokenStr string, exp int64, err error)
- func (entity *JwtEntity) ResolveToken(tokenString string) (jwt.MapClaims, error)
- type LogMap
- type MethodOverrideConfig
- type MethodOverrideGetter
- type RecoverConfig
- type RequestIDConfig
- type ResponseWriter
- type SecureConfig
- type Skipper
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 DefaultTokenValidator ¶
默认算法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 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"` }
type CSRFConfig ¶
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 (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 (*JwtEntity) ResolveToken ¶
解析token
type MethodOverrideConfig ¶
type MethodOverrideConfig struct { Skipper Skipper Getter MethodOverrideGetter }
type MethodOverrideGetter ¶
func MethodFromForm ¶
func MethodFromForm(param string) MethodOverrideGetter
func MethodFromHeader ¶
func MethodFromHeader(header string) MethodOverrideGetter
func MethodFromQuery ¶
func MethodFromQuery(param string) MethodOverrideGetter
type RecoverConfig ¶
type RequestIDConfig ¶
type ResponseWriter ¶
type ResponseWriter struct { gin.ResponseWriter // contains filtered or unexported fields }
用于读取返回信息
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"` }
Source Files ¶
Click to show internal directories.
Click to hide internal directories.