null pointer exception
Closed this issue · 3 comments
Describe the bug
null pointer exception
To Reproduce
Steps to reproduce the behavior:
- Go to jwt
- different places 'Authenticator: func(){ .... users, err := dal.CheckUser(loginStruct.Account, loginStruct.Password)}'
func CheckUser(account, password string) ([]*po.User, error) {
res := make([]*po.User, 0)
if err := DB.Where(DB.Or("username = ?", account).
Or("phone_number = ?", account)).
Find(&res).Error; err != nil {
return nil, err
} else {
if !utils.CheckPass(res[0].Password, password) {
return nil, errors.New("密码匹配错误")
}
}
return res, nil
}
route:
// customizeRegister registers customize routers.
func customizedRegister(r *server.Hertz) {
r.POST("/login", middleware.JwtMiddleware.LoginHandler)
auth := r.Group("/api/v1/auth")
auth.Use(middleware.JwtMiddleware.MiddlewareFunc())
{
auth.GET("/ping", handler.Ping)
}
}
- debug track down
func (mw *HertzJWTMiddleware) LoginHandler(ctx context.Context, c *app.RequestContext) {
if mw.Authenticator == nil <<==== this error
- See error
2022/10/26 12:17:07.444854 recovery.go:46: [Error] [Recovery] 2022/10/26 - 12:17:07 panic recovered:
runtime error: invalid memory address or nil pointer dereference
Expected behavior
Correctly Authentication
Screenshots
debug-image-url : ·=https://cdn.m2dd.top/blog/hertz-jwt-error-01.gif
Hertz version:
v0.3.2
Environment:
m2@m2:~$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/m2/.cache/go-build"
GOENV="/home/m2/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/usr/local/GoCode/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/usr/local/GoCode"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/usr/local/devEnv/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/devEnv/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1477738438=/tmp/go-build -gno-record-gcc-switches"
Additional context
nil
I don't reproduce the problem locally, and I don't see the full stack information to allow me to determine why your code is panic.but I think it may be a problem with the business code.
sorry, close this issues, have already solved; I forgot to call it JwtInit funcation.
sorry, close this issues, have already solved; I forgot to call it JwtInit funcation.
OK and thank you for using hertz.