这里只介绍我们如何使用该中间件,具体内容请点击基于go-zero实现JWT认证
package main
import (
"flag"
"net/http"
"github.com/tal-tech/go-zero/core/conf"
"github.com/tal-tech/go-zero/example/graceful/etcd/api/config"
"github.com/tal-tech/go-zero/rest"
)
var configFile = flag.String("f", "etc/graceful-api.json", "the config file")
func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
engine := rest.MustNewServer(c.RestConf)
defer engine.Stop()
engine.AddRoute(rest.Route{
Method: http.MethodGet,
Path: "/",
Handler: func(writer http.ResponseWriter, request *http.Request) {
//do something
},
},rest.WithJwt("your secret")) //Change to your own jwt secret
engine.Start()
}
engine.AddRoute(rest.Route{
Method: http.MethodGet,
Path: "/",
Handler: func(writer http.ResponseWriter, request *http.Request) {
//do something
},
},rest.WithJwtTransition("your secret","your prevSecret")) //Change to your own jwt secret and prevSecret
engine := rest.MustNewServer(c.RestConf,rest.WithUnauthorizedCallback(func(w http.ResponseWriter, r *http.Request, err error) {
// your custom callback function
}))