英文:
Auth0 in Go Martini
问题
我正在尝试在Go的Martini中使用Auth0。我正在使用他们的示例,但无论我尝试什么,似乎都无法使其工作。
这是我的代码:
package main
import (
"flag"
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
"github.com/auth0/go-jwt-middleware"
"encoding/base64"
"github.com/dgrijalva/jwt-go"
"net/http"
)
func main() {
m := martini.Classic()
port := flag.String("port", "8000", "HTTP Port")
flag.Parse()
jwtMiddleware := jwtmiddleware.New(jwtmiddleware.Options{
ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
decoded, err := base64.URLEncoding.DecodeString("<token>")
if err != nil {
return nil, err
}
return decoded, nil
},
})
m.Use(render.Renderer(render.Options{
IndentJSON: true, // Output human readable JSON
}))
m.Get("/", jwtMiddleware.Handler, func(res http.ResponseWriter, req *http.Request) { // res and req are injected by Martini
res.WriteHeader(200) // HTTP 200
})
// Get the PORT from the environment.
m.RunOnAddr(":" + *port)
}
当我运行时,我得到一个恐慌,显示Value not found for type http.Handler
。
如果我将jwtMiddleware.Handler
更改为jwtMiddleware.HandlerWithNext
,我会得到一个恐慌,显示Value not found for type http.HandlerFunc
。
有人知道我做错了什么吗?
英文:
I'm trying to use Auth0 with Martini in Go. I'm using their examples but I can't seem to get it working no matter what I try.
Here is my code:
package main
import (
"flag"
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
"github.com/auth0/go-jwt-middleware"
"encoding/base64"
"github.com/dgrijalva/jwt-go"
"net/http"
)
func main() {
m := martini.Classic()
port := flag.String("port", "8000", "HTTP Port")
flag.Parse()
jwtMiddleware := jwtmiddleware.New(jwtmiddleware.Options{
ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
decoded, err := base64.URLEncoding.DecodeString("<token>")
if err != nil {
return nil, err
}
return decoded, nil
},
})
m.Use(render.Renderer(render.Options{
IndentJSON: true, // Output human readable JSON
}))
m.Get("/", jwtMiddleware.Handler, func(res http.ResponseWriter, req *http.Request) { // res and req are injected by Martini
res.WriteHeader(200) // HTTP 200
})
// Get the PORT from the environment.
m.RunOnAddr(":" + *port)
}
When I run that, I get a panic that says Value not found for type http.Handler
If I change the jwtMiddleware.Handler
to jwtMiddleware.HandlerWithNext
, I get a panic for Value not found for type http.HandlerFunc
.
Does anyone have any ideas what I'm doing wrong?
答案1
得分: 2
使用jwt-middleware与Martini一起使用,只需使用CheckJWT
方法而不是Handler方法。
请参考以下示例:https://github.com/auth0/go-jwt-middleware/blob/master/examples/martini-example/main.go#L27
如果有帮助,请告诉我。
祝好!
英文:
To use the jwt-middleware with Martini, you just have to use the CheckJWT
method instead of the Handler method.
Check this example: https://github.com/auth0/go-jwt-middleware/blob/master/examples/martini-example/main.go#L27
Let me know if this helps.
Cheers!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论