doujian1050 2015-05-12 20:45
浏览 49
已采纳

Go Martini中的Auth0

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条回答 默认 最新

  • doumeng1897 2015-05-12 21:42
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条