拦截器引入redisUtil为null

原因:拦截器在bean初始化前执行的,这时候redisUtil是null,需要通过下面这个方式去获取

主要解决的代码:
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
redisOpsUtil = wac.getBean(RedisOpsUtil.class);

package com.hzh.studyonline.config;
import com.hzh.studyonline.util.JwtUtil;
import com.hzh.studyonline.util.RedisOpsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

//新建拦截器,在token验证前执行
@Slf4j
public class AuthenticationInterceptor implements HandlerInterceptor {
    @Autowired
    RedisOpsUtil redisOpsUtil;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String token = request.getHeader("token");
        String phone = request.getHeader("phone");
        // 如果不是映射到方法直接通过
        if(!(handler instanceof HandlerMethod)){
            return true;
        }
        //如果接口或者类上有@JwtIgnore注解,意思该接口不需要token就能访问,需要放行
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        //先从类上获取该注解,判断类上是否加了@JwtIgnore ,代表不需要token,直接放行
        JwtIgnore annotation = handlerMethod.getBeanType().getAnnotation(JwtIgnore.class);
        if(annotation == null){
            //再从方法上获取该注解
            if(method.isAnnotationPresent(JwtIgnore.class)){
                annotation = method.getAnnotation(JwtIgnore.class);
                log.info("请求方法 {} 上有注解 {} ",method.getName(),annotation);
            }
        }
        if (annotation!=null){
            return true;
        }

        //开始检验没有权限的接口
        //1、jwt解析正确
        JwtUtil jwtUtil = new JwtUtil();
        boolean parse = jwtUtil.parse(token);
        //2、从redis中获取的token一致
        if (redisOpsUtil==null){
            //拦截器在bean初始化前执行的,这时候redisUtil是null,需要通过下面这个方式去获取
            WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
            redisOpsUtil = wac.getBean(RedisOpsUtil.class);
        }
        String redisToken =(String) redisOpsUtil.hget("token", phone);
        if (redisToken==null){
            log.info("redisToken过期 电话是:{} 的用户 ",phone);
            return false;
        }
        if (parse==true && redisToken.equals(token)){
            return true;
        }
        return false;
    }


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值