Make the following changes.
The first is WebMvcConfiguration
:
@Configurationpublicclassextends WebMvcConfigurerAdapter { @Bean publicauthenticationInterceptor() { returnnewAuthenticationInterceptor(); } @Override publicvoidaddInterceptors(InterceptorRegistry registry) { registry.addInterceptor(authenticationInterceptor()).addPathPatterns("/**"); }}
Then you need to make the springboot
scan current Interceptor
, that is, add comments on the class @Component
.
@Component Public classAuthenticationinterceptorImplementsHandlerinterceptor {@Autowired PrivateUserService UserService;@Override Public Boolean Prehandle(HttpServletRequest request, httpservletresponse response, Object O)throwsException {User user = UserService.getById(ID); }@Override Public void Posthandle(HttpServletRequest request, httpservletresponse response, Object O, Modelandview modelandview)throwsException {}@Override Public void aftercompletion(HttpServletRequest request, httpservletresponse response, Object O, Exception e)throwsException {}}
@Autowired injection service is not available in the Springboot interceptor