RedisTemplate为null的问题
最近在搭建一个项目,然后项目框架采用的是spring boot,然后登录我就使用新学习的JWT嘛,然后就想着在请求进来的时候使用拦截器先对传进来的token进行校验,然后惊奇的发现,一致报错token无效,我写单元测试token也没有问题,然后就是缓存一直没有查询到当前的令牌。
先来看看我是怎么写的
@Autowired
private RedisTemplate redisTemplate;
/**
* 请求处理
*
* @author zhanglianyong
* @date 2022/9/6
* @param request 请求
* @param response 响应
* @param handler 处理器
* @return 是否允许通过
**/
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
//获取请求头中的令牌
String token = request.getHeader("Authorization");
log.info("Authorization:{}", token);
// 先从缓存中查询看看有没有当前令牌
try {
JwtUtils.verify(token);
// 先查询token在缓存中有没有存在
Object user = redisTemplate.opsForValue().get(token);
if (null != user) {
return true;
}
throw CommonException.fail("缓存中没有查询到当前token");
} catch (SignatureVerificationException e) {
throw JwtException.signatureVerificationException();
} catch (TokenExpiredException e) {
throw JwtException.tokenExpiredException();
} catch (AlgorithmMismatchException e) {
throw JwtException.algorithmMismatchException();
} catch (InvalidClaimException e) {
throw JwtException.invalidClaimException();
} catch (CommonException e) {
throw CommonException.userFail(e.getMessage());
} catch (Exception e) {
e.printStackTrace();
log.info("报错信息:{}", e.getMessage());
throw CommonException.fail("token无效");
}
}
然后这样看似乎是没有什么问题的,但是不断发现报错去debug后,发现redisTemplate是null,相当于这个RedisTemplate没有注入,这里确实我没有写好,如果严谨点的话,要是判断,然后抛出异常也知道是什么原因了,但是我没有,然后我就不断不断的试,在纳闷…
解决方案
我去查了一下,然后是因为拦截器是在Bean实例化就已经执行了,所以就会出现上述这种情况,解决方法也很简单,就是在加载拦截器时我们先实例化这个拦截器,将这个Bean注入进去,我们用一个实例工厂,在添加拦截器时将这个Bean注入进去。
@Bean
public JwtInterceptor getJwtInterceptor() {
return new JwtInterceptor();
}
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(getJwtInterceptor())
.addPathPatterns("/user/**")
.excludePathPatterns("/doc.html")
.excludePathPatterns("/webjars/**")
.excludePathPatterns("/user/login");
}
最后
通过上面这样就可以解决加载不进去的问题了,这时运行的redisTemplate就是已经加载进去的了