重写实现 DefaultInterceptor

    • 注意要写 @Primary
    1. package com.detabes.gzyanalysis.config;
    2. import cn.hutool.core.util.ObjectUtil;
    3. import com.detabes.gzyanalysis.constant.RedisConstant;
    4. import com.detabes.gzyanalysis.controller.user.vo.UserVO;
    5. import com.detabes.gzyanalysis.util.UserTokenUtil;
    6. import com.detabes.jwt.util.JwtUtil;
    7. import com.detabes.jwtweb.server.impl.DefaultInterceptor;
    8. import com.detabes.redis.core.config.RedisProxy;
    9. import lombok.extern.slf4j.Slf4j;
    10. import org.springframework.beans.factory.annotation.Value;
    11. import org.springframework.context.annotation.Primary;
    12. import org.springframework.stereotype.Service;
    13. /**
    14. * 登录验证实现
    15. *
    16. * @author tn
    17. * @className JWTCheckTokenInterceptorImpl
    18. * @date 2021-10-11 09:28
    19. */
    20. @Service
    21. @Primary
    22. @Slf4j
    23. public class JWTCheckTokenInterceptorImpl extends DefaultInterceptor {
    24. private final RedisProxy redisProxy;
    25. @Value("${gzy.onlylogin:true}")
    26. private String onlylogin;
    27. public JWTCheckTokenInterceptorImpl(RedisProxy redisProxy) {
    28. this.redisProxy = redisProxy;
    29. }
    30. @Override
    31. public boolean checkToken(String token) {
    32. try {
    33. UserVO userVO = UserTokenUtil.getTokenInfoByStr(token);
    34. // 存储方式(k:loginName, v:token)
    35. if(Boolean.parseBoolean(onlylogin)){
    36. Object userToken = redisProxy.hget(RedisConstant.USER_LIST_KEY + ":" + userVO.getUserNo(),
    37. userVO.getUserNo());
    38. if(ObjectUtil.isNull(userToken)||!token.equals(userToken)){
    39. // redis 中没有该用户的登录信息 qie token 当前token跟redis中的token要一直
    40. return false;
    41. }
    42. }
    43. // 验证token有效期
    44. return JwtUtil.verity(token);
    45. }catch (Exception e){
    46. log.error("redis中获取用户失败",e);
    47. return false;
    48. }
    49. }
    50. }