middleware

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Auth deprecated

func Auth(af AuthFunc) gin.HandlerFunc

Deprecated: 为了兼容旧版本而存在,请使用 BaseAuthMiddleware

func BaseAuthMiddleware added in v0.1.0

func BaseAuthMiddleware(f AuthFunc, errResp interface{}) gin.HandlerFunc

BaseAuthMiddleware 拦截请求,并使用 f 验证请求状态,如果验证不通过(f 返回 nil, false) 会直接响应 401(Unauthorized), 反之,验证通过该中间件会将请求状态(由 f 返回)保存在上下文的 user 属性中,在应用逻辑中,你可以使用 context.Get("user") 获取到。 如果传入的 f 为空,该中间件不起任何作用; errResp 用于自定义认证失败时的响应数据,默认为 {"header": {"code": 401, "msg": "Unauthorized"}}.

Example

ExampleBaseAuthMiddleware 演示 BaseAuthMiddleware 在原生 gin 中的用法

engine := gin.Default()
defer engine.Run()
engine.Use(BaseAuthMiddleware(XExampleAuthFunc, nil))
engine.POST("/ping", func(context *gin.Context) {
	context.JSON(http.StatusOK, "pong")
})
Output:

Example (GinUtils)

ExampleBaseAuthMiddleware_ginUtils 演示 BaseAuthMiddleware 配合 ginUtils.URLPatterns 的用法.

engine := gin.Default()
defer engine.Run()
router := func(g *gin.RouterGroup) {
	g.POST("/",
		// auth
		BaseAuthMiddleware(XExampleAuthFunc, nil),
		func(context *gin.Context) {
			context.JSON(200, "pong")
		})
}
ginUtils.URLPatterns(engine, "/ping", router)
Output:

func CorsHandler

func CorsHandler(accessList []string) gin.HandlerFunc

accessList: 允许访问的白名单

func Permiter

func Permiter(license PermiterFunc) gin.HandlerFunc

鉴权中间件, 注册该中间件后, 如果 license 返回 true 则表示 有权限访问该组(个)接口, 否则响应 403

func SimpleTooManyReqResp

func SimpleTooManyReqResp(waitingSeconds float64) interface{}

访问频率太快时的默认返回值:

{
    "code": 429,
    "msg": "您的请求太快了,休息一下吧 ^_^ (5s)"
}

func Throttled

func Throttled(f ThrottledFunc) gin.HandlerFunc

Throttled 节流中间件,f 第二个返回值为 false 时,拦截请求,响应状态码 为 429(StatusTooManyRequests),f 的第一个返回值是拦截请求后给客户端返回的内容。

Types

type AuthFunc

type AuthFunc func(ctx *gin.Context) (user interface{}, authed bool)

AuthFunc 用于身份验证,如果验证通过,应该将用户对象返回,反之第二个参数应该 返回 false,该类型只用于作为参数传递给认证中间件,具体用法请参考 BaseAuthMiddleware 的示例。

type KeyFunc

type KeyFunc func(ctx *gin.Context) string

type PermiterFunc

type PermiterFunc func(context *gin.Context) bool

type ThrottledFunc

type ThrottledFunc func(ctx *gin.Context) (interface{}, bool)

func BaseThrottled

func BaseThrottled(rate string, keyFunc KeyFunc, respFunc TooManyRequestResponseFunc) ThrottledFunc

BaseThrottled 是一个更基础打函数,相比于 SimpleThrottledWithKeyFunc, BaseThrottled 允许自定义用户访问频率太快时的返回值

func SimpleThrottle

func SimpleThrottle(rule ThrottledRule, rate string) ThrottledFunc

SimpleThrottle 会根据 rule 为每一次请求生成一个 key, key 相同 就会被认为是同一个用户,最简单的情况下,我们会以 IP 和 UA 来判断请求 是不是来自同一个人,这时,rule 可以使用下面三个选项:

  • ThrottledRuleByUserAgent: 只要 UA 相同即视为同一用户
  • ThrottledRuleByIP: 根据客户端 IP 判断是否是统一用户
  • ThrottledRuleByUserAgentAndIP: UA 和 IP 都匹配才视为同一个用户

这样最简单但页不安全,因为用户可以随便更换自己使用的 UA 和 IP. rate 用来控制同一个用户的访问频率,如使用 16/3m 表示三分钟内最多可以访问 16 次 “/” 后面的 Duration 支持 s(秒),m(分),h(小时),d(天),大小写不敏感

func SimpleThrottledWithFields

func SimpleThrottledWithFields(rate string, fields []string) ThrottledFunc

SimpleThrottledWithFields 允许你传入一组请求头字符串,这些请求头的值 将会被合在一起作为 key (参考 SimpleThrottle)

func SimpleThrottledWithKeyFunc

func SimpleThrottledWithKeyFunc(rate string, keyFunc KeyFunc) ThrottledFunc

SimpleThrottledWithKeyFunc 允许传入一个函数 keyFunc 该函数应该返回一个 string 类型的 key 相同的 key 表示同一个用户

type ThrottledRule

type ThrottledRule int

节流规则

const (
	// 只根据 IP 节流
	ThrottledRuleByIP ThrottledRule = iota + 1

	// 只根据 UA 节流
	ThrottledRuleByUserAgent

	// 根据 UA 和 IP 节流
	ThrottledRuleByUserAgentAndIP
)

type TooManyRequestResponseFunc

type TooManyRequestResponseFunc func(waitingSeconds float64) interface{}

TooManyRequestResponseFunc 用来返回访问频率过快时的返回值 waitingSeconds 表示用户还应该等待多长时间才能进行下一次正常访问

Jump to

Keyboard shortcuts

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