cannot use func literal (type func(string, string, echo.Context) bool) as type middleware.BasicAuthValidator in argument to middleware.BasicAuth

huangapple go评论80阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2021年11月12日 02:29:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/69933200.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定