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
斷點查看發(fā)現(xiàn)注入的service為null 如圖
image
百度發(fā)現(xiàn)是說ShiroRelam屬于filter即過濾器蜜自,它在Spring未完成注入bean之前就已經(jīng)攔截了菩貌,因此無法注入
參考大神的寫法,自己寫一個注入工具類
@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;
}
}
}
然后在報錯的地方手動注入就可以解決了
image.png