123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- /*
- * @description:
- * @Author: CP
- * @Date: 2021-12-25 15:06:19
- * @FilePath: \construction_management\web\viewmodels\jl.go
- */
- package viewmodels
- import (
- // jwtmiddleware "github.com/iris-contrib/middleware/jwt"
- validation "github.com/go-ozzo/ozzo-validation/v3"
- )
- type Jl struct {
- Token string `form:"token" json:"token" `
- Code string `form:"code" json:"code" `
- }
- // 校验项目接口
- func (l Jl) ValidateToken() error {
- return validation.ValidateStruct(&l,
- validation.Field(&l.Token, validation.Required.Error("token不能为空")),
- )
- }
- // 验证传参
- func (l Jl) ValidateParameter() error {
- return validation.ValidateStruct(&l,
- validation.Field(&l.Code, validation.Required.Error("token不能为空")),
- )
- }
- // // 校验jwt
- // func (l Jl) ValidateJwt(tokenStr string, ctx iris.Context) (jwt.MapClaims, error) {
- // // 验证
- // // j := jwtmiddleware.New(jwtmiddleware.Config{
- // // // 返回一个密匙
- // // ValidationKeyGetter: func(token *jwt.Token) (i interface{}, e error) {
- // // return conf.JLSecretKey, nil
- // // },
- // // Extractor: jwtmiddleware.FromParameter("token"),
- // // SigningMethod: jwtmiddleware.SigningMethodHS256,
- // // })
- // // if err := j.CheckJWT(ctx); err != nil {
- // // fmt.Println(err)
- // // }
- // // 解析
- // // tokenClaims, err := jwt.ParseWithClaims(tokenStr, &MyCustomClaims{}, func(token *jwt.Token) (interface{}, error) {
- // // return []byte(conf.JLSecretKey), nil
- // // })
- // // if err != nil {
- // // return nil, err
- // // }
- // // 初始化`Claims`实例
- // // claims := &Claims{}
- // // 解析JWT字符串并将结果存储在`claims`中。
- // // 请注意,我们也在此方法中传递了密钥。
- // // 如果令牌无效(如果令牌已根据我们设置的登录到期时间过期)或者签名不匹配,此方法会返回错误.
- // // tkn, err := jwt.ParseWithClaims(tokenStr, claims, func(token *jwt.Token) (interface{}, error) {
- // // return []byte(conf.JLSecretKey), nil
- // // })
- // // 在这里声明令牌的到期时间,我们将其保留为5分钟
- // expirationTime := time.Now().Add(5 * time.Minute)
- // claims := &Claims{
- // Username: "蔡频",
- // StandardClaims: jwt.StandardClaims{
- // // In JWT, the expiry time is expressed as unix milliseconds
- // ExpiresAt: expirationTime.Unix(),
- // },
- // }
- // token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
- // // 创建JWT字符串
- // tokenString, err := token.SignedString([]byte(conf.JLSecretKey))
- // fmt.Println(tokenString)
- // fmt.Println("tokenString")
- // parseAuth, err := jwt.Parse(tokenStr, func(*jwt.Token) (interface{}, error) {
- // return []byte(conf.JLSecretKey), nil
- // })
- // if err != nil {
- // log.Println("JWT异常, error=", err)
- // return nil, err
- // }
- // if !parseAuth.Valid {
- // return nil, errors.New("无效签名")
- // }
- // // claims := parseAuth.Claims.(jwt.MapClaims)
- // fmt.Println(claims)
- // return nil, nil
- // }
|