jwt认证就是用户注册之后, 服务器生成一个 JWT token返回给浏览器, 浏览器向服务器请求数据时将 JWT token 发给服务器, 服务器用 signature 中定义的方式解码 。
安装:go get github.com/dgrijalva/jwt-go
go get github.com/dgrijalva/jwt-go/request
main.go
package main
import (
"data"
"fmt"
"net/http"
"github.com/dgrijalva/jwt-go"
"github.com/dgrijalva/jwt-go/request"
"gopkg.in/gin-gonic/gin.v1"
)
func main() {
router := gin.Default()
router.POST("/customer/register", data.Register)
router.POST("/login", data.Login)
//添加群组中间件
authorized := router.Group("/user", MyMiddelware())
authorized.POST("/info", func(c *gin.Context) {
c.String(http.StatusOK, "info")
})
router.Run(":8080")
}
func MyMiddelware() gin.HandlerFunc {
return func(c *gin.Context) {
token, err := request.ParseFromRequest(c.Request, request.AuthorizationHeaderExtractor,