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.

61 lines
1.7 KiB

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