springboot + shiro + jwt登录成功后再请求
2019-08-17 本文已影响0人
嘿麻麻
springboot + shiro + jwt登录成功后再请求接口时报错,接口返回401 Unauthorized,控制台报错代码如下
16:14:40.500 default [http-nio-8080-exec-2] WARN o.a.s.authc.AbstractAuthenticator - Authentication failed for token submission [com.lan.mall.utils.JWTToken@60d03094]. Possible unexpected error? (Typical or expected login exceptions should extend from AuthenticationException).
java.lang.NullPointerException: null
断点查看发现注入的service为null 如图

百度发现是说ShiroRelam属于filter即过滤器,它在Spring未完成注入bean之前就已经拦截了,因此无法注入
参考大神的写法,自己写一个注入工具类
@Component
public class SpringBeanFactoryUtilsimplements ApplicationContextAware {
private static ApplicationContextcontext =null;
public static T getBean(Class type) {
return context.getBean(type);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {
if (SpringBeanFactoryUtils.context ==null) {
SpringBeanFactoryUtils.context = applicationContext;
}
}
}
然后在报错的地方手动注入就可以解决了
