2 回答
TA贡献1862条经验 获得超7个赞
除了 fstanis在这里回答的内容之外,我想指出您应该注意 echo group 对象的引用。
所以我认为你应该这样做
e := echo.New()
g := e.Group("")
g.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
if username == "joe" && password == "secret" {
return true, nil
}
return false, nil
}))
// note that it was previously referring to the echo instance, not group.
g.GET("/", func(c echo.Context) error {
return c.HTML(http.StatusOK, html)
})
注意是g指组e.Group(""),这样可以确保 GET "/" 的处理程序将返回正确的html。因此,基本身份验证中间件是应用在 Group 还是 Echo 的根实例上没有歧义e。
TA贡献1934条经验 获得超2个赞
您正在Group为您的路线注册一个内部回调。相反,您想在顶层注册组并向它们添加路由:
e := echo.New()
g := e.Group("")
g.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
if username == "joe" && password == "secret" {
return true, nil
}
return false, nil
}))
e.GET("/", func(c echo.Context) error {
return c.HTML(http.StatusOK, html)
})
- 2 回答
- 0 关注
- 189 浏览
添加回答
举报
