jwt简单使用
下载
go get -u github.com/golang-jwt/jwt/v5
构建token
package main
import (
"fmt"
"time"
"github.com/golang-jwt/jwt/v5"
)
func main() {
mySigningKey := []byte("deb0uuxjjxh32268a2aafb")
type MyCustomClaims struct {
Userid int64 `json:"userid"`
jwt.RegisteredClaims
}
// Create claims while leaving out some of the optional fields
claims := MyCustomClaims{
122556222222,
jwt.RegisteredClaims{
// Also fixed dates can be used for the NumericDate
ExpiresAt: jwt.NewNumericDate(time.Now().Add(24 * time.Hour)),
Issuer: "alsark",
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
ss, _ := token.SignedString(mySigningKey)
fmt.Println(ss)
}
解析token
package main
import (
"fmt"
"log"
"github.com/golang-jwt/jwt/v5"
)
func main() {
tokenString := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOjEyMjU1NjIyMjIyMiwiaXNzIjoiYWxzYXJrIiwiZXhwIjoxNzMxODUxMTgzfQ.n_8gcQIMMgiR4WFq4DjKrYUqIorSBnyfebIMseOc0-0"
type MyCustomClaims struct {
Userid int64 `json:"userid"`
jwt.RegisteredClaims
}
token, err := jwt.ParseWithClaims(tokenString, &MyCustomClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte("deb0uuxjjxh32268a2aafb"), nil
})
if err != nil {
log.Fatal(err)
} else if claims, ok := token.Claims.(*MyCustomClaims); ok {
fmt.Println(claims.Userid)
} else {
log.Fatal("unknown claims type, cannot proceed")
}
}
B站视频讲解
gin结合jwt-go
package main
import (
"fmt"
"time"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v5"
)
func main() {
r := gin.Default()
// r.Use(Jwtyz())
v1 := r.Group("/v1")
{
v1.GET("/hqtoken", Hqtoken)
}
v2 := r.Group("/v2", Jwtyz())
{
v2.GET("/yztoken", Yztoken)
}
r.Run() // 监听并在 0.0.0.0:8080 上启动服务
}
// 获取token
func Hqtoken(c *gin.Context) {
mySigningKey := []byte("Xye8pxrmuM")
type MyCustomClaims struct {
Userid string `json:"userid"`
jwt.RegisteredClaims
}
userid := "144551222554"
claims := MyCustomClaims{
userid,
jwt.RegisteredClaims{
// Also fixed dates can be used for the NumericDate
ExpiresAt: jwt.NewNumericDate(time.Now().Add(24 * time.Hour)),
Issuer: "alsark",
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
ss, _ := token.SignedString(mySigningKey)
fmt.Println(ss)
c.JSON(200, ss)
}
// 需要token接口
func Yztoken(c *gin.Context) {
userid, _ := c.Get("userid")
fmt.Println(userid)
}
// jwt中间件
func Jwtyz() gin.HandlerFunc {
return func(c *gin.Context) {
tokenString := c.GetHeader("Authorization")
if tokenString == "" {
c.JSON(401, gin.H{"error": "Authorization异常"})
c.Abort()
return
}
type MyCustomClaims struct {
Userid string `json:"userid"`
jwt.RegisteredClaims
}
token, err := jwt.ParseWithClaims(tokenString, &MyCustomClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte("Xye8pxrmuM"), nil
})
if err != nil {
c.JSON(401, gin.H{"error": "Authorization异常"})
c.Abort()
return
} else if claims, ok := token.Claims.(*MyCustomClaims); ok {
c.Set("userid", claims.Userid)
} else {
c.JSON(401, gin.H{"error": "Authorization异常"})
c.Abort()
return
}
c.Next()
}
}
gitee代码
https://gitee.com/alsark/gin-combined-with-jwt.githttps://gitee.com/alsark/gin-combined-with-jwt.git