英文:
cannot use func literal (type func(string, string, echo.Context) bool) as type middleware.BasicAuthValidator in argument to middleware.BasicAuth
问题
我想在使用Echo框架的Golang中应用基本身份验证。我遇到了以下错误:
"# command-line-arguments
.\main.go:44:29: 无法将函数文字(类型
func(string, string, echo.Context) bool)用作middleware.BasicAuthValidator类型的参数
middleware.BasicAuth"
我的代码如下:
package main
import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"net/http"
)
// 主函数
func main(){
e := echo.New()
g := e.Group("/home")
g.Use(middleware.BasicAuth(func (username, password string, c echo.Context) bool {
if(username=="iamsns" && password=="Iamsns355@"){
return true
} else {
return false
}
}))
g.POST("/login", getHome)
e.Start(":8008")
}
英文:
I want to apply Basic authentication in golang using echo framework. I have following error :
"# command-line-arguments
.\main.go:44:29: cannot use func literal (type
func(string, string, echo.Context) bool) as type middleware.BasicAuthValidator in argument to
middleware.BasicAuth"
my code:
package main
import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"net/http"
)
// main function
func main(){
e := echo.New()
g := e.Group("/home")
g.Use(middleware.BasicAuth(func (username, password string, c echo.Context) bool {
if(username=="iamsns" && password=="Iamsns355@"){
return true
} else {
return false
}
}))
g.POST("/login", getHome)
e.Start(":8008")
}
答案1
得分: 1
根据文档,middleware.BasicAuthValidator 的定义是 func(string, string, echo.Context) (bool, error)
而不是 func(string, string, echo.Context) bool
。你需要修改函数签名,使其也返回一个 error
。
英文:
According to the docs middleware.BasicAuthValidator is defined as func(string, string, echo.Context) (bool, error)
not func(string, string, echo.Context) bool
. You need to change the signature to also return an error
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论