# JWT鉴权中间件

这里只介绍我们如何使用该中间件，具体内容请点击[基于go-zero实现JWT认证](https://github.com/tal-tech/zero-doc/blob/main/doc/jwt.md)


## example


```go
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()
}
```


## add prevSecret


```go
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
```


## 自定义鉴权失败回调函数


```go
engine := rest.MustNewServer(c.RestConf,rest.WithUnauthorizedCallback(func(w http.ResponseWriter, r *http.Request, err error) {
		// your custom callback function
}))
```

<Vssue title="jwtmiddleware" />

