使用的v1.9.10的GF,现有如下代码:
package main
import (
"fmt"
"net/http"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
)
func MiddlewareAuth1(r *ghttp.Request) {
fmt.Println("middleware 1")
token := r.Get("token")
if token == "123456" {
r.Middleware.Next()
} else {
r.Response.WriteStatus(http.StatusForbidden)
}
}
func MiddlewareAuth2(r *ghttp.Request) {
fmt.Println("middleware 2")
token := r.Get("token")
if token == "123456" {
r.Middleware.Next()
} else {
r.Response.WriteStatus(http.StatusForbidden)
}
}
func MiddlewareFree(r *ghttp.Request) {
fmt.Println("middleware free")
r.Response.CORSDefault()
r.Middleware.Next()
}
func MiddlewareCORS(r *ghttp.Request) {
fmt.Println("middleware CORS")
r.Response.CORSDefault()
r.Middleware.Next()
}
func main() {
s := g.Server()
s.Group("/", func(g *ghttp.RouterGroup) {
g.MiddlewarePattern("/*", func(r *ghttp.Request) {
if r.URL.Path == "/login" {
r.Middleware.Next()
return
}
MiddlewareAuth1(r)
MiddlewareAuth2(r)
})
})
s.Group("/", func(g *ghttp.RouterGroup) {
g.ALL("/login", func(r *ghttp.Request) {
r.Response.Write("login")
})
g.ALL("/dashboard", func(r *ghttp.Request) {
r.Response.Write("dashboard")
})
})
s.Group("/api.v2", func(g *ghttp.RouterGroup) {
g.Middleware(MiddlewareFree, MiddlewareCORS)
g.ALL("/user/list", func(r *ghttp.Request) {
r.Response.Write("list")
})
})
s.SetPort(8199)
s.Run()
}
当我请求localhost:8199/api.v2/user/list
的时候,的确应该返回403,但是问题是我收到了两个403,响应是这样的:ForbiddenForbidden
。我的理解应该是:前一个中间件已经返回403了,不是200了,后面的中间件是不是不要再执行了?或者要其他什么手段控制它是否执行?
如果我把代码改成这样:
package main
import (
"fmt"
"net/http"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
)
func MiddlewareAuth1(r *ghttp.Request) {
fmt.Println("middleware 1")
token := r.Get("token")
if token == "123456" {
r.Middleware.Next()
} else {
r.Response.WriteStatus(http.StatusForbidden)
}
}
func MiddlewareAuth2(r *ghttp.Request) {
fmt.Println("middleware 2")
token := r.Get("token")
if token == "123456" {
r.Middleware.Next()
} else {
r.Response.WriteStatus(http.StatusForbidden)
}
}
func MiddlewareFree(r *ghttp.Request) {
fmt.Println("middleware free")
r.Response.CORSDefault()
r.Middleware.Next()
}
func MiddlewareCORS(r *ghttp.Request) {
fmt.Println("middleware CORS")
r.Response.CORSDefault()
r.Middleware.Next()
}
func main() {
s := g.Server()
s.Group("/", func(g *ghttp.RouterGroup) {
g.Middleware(MiddlewareAuth1, MiddlewareAuth2)
g.MiddlewarePattern("/*", func(r *ghttp.Request) {
if r.URL.Path == "/login" {
r.Middleware.Next()
return
}
})
})
s.Group("/", func(g *ghttp.RouterGroup) {
g.ALL("/login", func(r *ghttp.Request) {
r.Response.Write("login")
})
g.ALL("/dashboard", func(r *ghttp.Request) {
r.Response.Write("dashboard")
})
})
s.Group("/api.v2", func(g *ghttp.RouterGroup) {
g.Middleware(MiddlewareFree, MiddlewareCORS)
g.ALL("/user/list", func(r *ghttp.Request) {
r.Response.Write("list")
})
})
s.SetPort(8199)
s.Run()
}
总结一下:注意第44行,是我的改动,此时再请求
localhost:8199/api.v2/user/list
,只会返回1个403,一个Forbidden
,然而,新的问题来了,不能正常访问login接口了。
- 如果使用MiddlewarePattern绑定中间件,前一个403了之后,后一个还会执行,是否bug?
- 如果使用RouterGroup绑定中间件,官方文档中的例外控制action就失效了,是否bug?