You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.5 KiB

  1. package service
  2. import (
  3. "errors"
  4. "gin-vue-admin/global"
  5. "gin-vue-admin/model"
  6. "gorm.io/gorm"
  7. "time"
  8. )
  9. //@author: [piexlmax](https://github.com/piexlmax)
  10. //@function: JsonInBlacklist
  11. //@description: 拉黑jwt
  12. //@param: jwtList model.JwtBlacklist
  13. //@return: err error
  14. func JsonInBlacklist(jwtList model.JwtBlacklist) (err error) {
  15. err = global.GVA_DB.Create(&jwtList).Error
  16. return
  17. }
  18. //@author: [piexlmax](https://github.com/piexlmax)
  19. //@function: IsBlacklist
  20. //@description: 判断JWT是否在黑名单内部
  21. //@param: jwt string
  22. //@return: bool
  23. func IsBlacklist(jwt string) bool {
  24. err := global.GVA_DB.Where("jwt = ?", jwt).First(&model.JwtBlacklist{}).Error
  25. isNotFound := errors.Is(err, gorm.ErrRecordNotFound)
  26. return !isNotFound
  27. }
  28. //@author: [piexlmax](https://github.com/piexlmax)
  29. //@function: GetRedisJWT
  30. //@description: 从redis取jwt
  31. //@param: userName string
  32. //@return: err error, redisJWT string
  33. func GetRedisJWT(userName string) (err error, redisJWT string) {
  34. redisJWT, err = global.GVA_REDIS.Get(userName).Result()
  35. return err, redisJWT
  36. }
  37. //@author: [piexlmax](https://github.com/piexlmax)
  38. //@function: SetRedisJWT
  39. //@description: jwt存入redis并设置过期时间
  40. //@param: jwt string, userName string
  41. //@return: err error
  42. func SetRedisJWT(jwt string, userName string) (err error) {
  43. // 此处过期时间等于jwt过期时间
  44. timer := time.Duration(global.GVA_CONFIG.JWT.ExpiresTime) * time.Second
  45. err = global.GVA_REDIS.Set(userName, jwt, timer).Err()
  46. return err
  47. }