jl.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * @description:
  3. * @Author: CP
  4. * @Date: 2021-12-25 15:06:19
  5. * @FilePath: \construction_management\web\viewmodels\jl.go
  6. */
  7. package viewmodels
  8. import (
  9. // jwtmiddleware "github.com/iris-contrib/middleware/jwt"
  10. validation "github.com/go-ozzo/ozzo-validation/v3"
  11. )
  12. type Jl struct {
  13. Token string `form:"token" json:"token" `
  14. Code string `form:"code" json:"code" `
  15. }
  16. // 校验项目接口
  17. func (l Jl) ValidateToken() error {
  18. return validation.ValidateStruct(&l,
  19. validation.Field(&l.Token, validation.Required.Error("token不能为空")),
  20. )
  21. }
  22. // 验证传参
  23. func (l Jl) ValidateParameter() error {
  24. return validation.ValidateStruct(&l,
  25. validation.Field(&l.Code, validation.Required.Error("token不能为空")),
  26. )
  27. }
  28. // // 校验jwt
  29. // func (l Jl) ValidateJwt(tokenStr string, ctx iris.Context) (jwt.MapClaims, error) {
  30. // // 验证
  31. // // j := jwtmiddleware.New(jwtmiddleware.Config{
  32. // // // 返回一个密匙
  33. // // ValidationKeyGetter: func(token *jwt.Token) (i interface{}, e error) {
  34. // // return conf.JLSecretKey, nil
  35. // // },
  36. // // Extractor: jwtmiddleware.FromParameter("token"),
  37. // // SigningMethod: jwtmiddleware.SigningMethodHS256,
  38. // // })
  39. // // if err := j.CheckJWT(ctx); err != nil {
  40. // // fmt.Println(err)
  41. // // }
  42. // // 解析
  43. // // tokenClaims, err := jwt.ParseWithClaims(tokenStr, &MyCustomClaims{}, func(token *jwt.Token) (interface{}, error) {
  44. // // return []byte(conf.JLSecretKey), nil
  45. // // })
  46. // // if err != nil {
  47. // // return nil, err
  48. // // }
  49. // // 初始化`Claims`实例
  50. // // claims := &Claims{}
  51. // // 解析JWT字符串并将结果存储在`claims`中。
  52. // // 请注意,我们也在此方法中传递了密钥。
  53. // // 如果令牌无效(如果令牌已根据我们设置的登录到期时间过期)或者签名不匹配,此方法会返回错误.
  54. // // tkn, err := jwt.ParseWithClaims(tokenStr, claims, func(token *jwt.Token) (interface{}, error) {
  55. // // return []byte(conf.JLSecretKey), nil
  56. // // })
  57. // // 在这里声明令牌的到期时间,我们将其保留为5分钟
  58. // expirationTime := time.Now().Add(5 * time.Minute)
  59. // claims := &Claims{
  60. // Username: "蔡频",
  61. // StandardClaims: jwt.StandardClaims{
  62. // // In JWT, the expiry time is expressed as unix milliseconds
  63. // ExpiresAt: expirationTime.Unix(),
  64. // },
  65. // }
  66. // token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
  67. // // 创建JWT字符串
  68. // tokenString, err := token.SignedString([]byte(conf.JLSecretKey))
  69. // fmt.Println(tokenString)
  70. // fmt.Println("tokenString")
  71. // parseAuth, err := jwt.Parse(tokenStr, func(*jwt.Token) (interface{}, error) {
  72. // return []byte(conf.JLSecretKey), nil
  73. // })
  74. // if err != nil {
  75. // log.Println("JWT异常, error=", err)
  76. // return nil, err
  77. // }
  78. // if !parseAuth.Valid {
  79. // return nil, errors.New("无效签名")
  80. // }
  81. // // claims := parseAuth.Claims.(jwt.MapClaims)
  82. // fmt.Println(claims)
  83. // return nil, nil
  84. // }