因为拦截器的执行在 Bean 初始化之前,所以在拦截器中 Bean无法注入。
解决方案一:将拦截器以 Bean 加载
1.1 WebMvcConfigurerAdapter.java 监听器配置:
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebMvcAdapter extends WebMvcConfigurerAdapter {
@Bean
public SignatureIntercepter getSignatureIntercepter() {
return new SignatureIntercepter();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(getSignatureIntercepter()).addPathPatterns("/**");
super.addInterceptors(registry);
}
}
1.2 拦截器配置:
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class SignatureIntercepter implements HandlerInterceptor {
@Resource
public SignatureServer signatureServer;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (!(handler instanceof HandlerMethod)) {
return true;
}
HandlerMethod method = (HandlerMethod) handler;
SignatureAnnotation signatureAnnotation = method.getMethod().getAnnotation(SignatureAnnotation.class);
if (signatureAnnotation == null) {
return true;
}
if(signatureServer.verifySignature(request.getHeader(SignatureUtils.SIGNATURE)) {
return true;
}
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
解决方案二:通过工厂装配 Bean
2.1 装配 Bean
BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
signatureServer = (SignatureServer) factory.getBean("signatureServer");
2.2 拦截器配置
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class SignatureIntercepter implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (!(handler instanceof HandlerMethod)) {
return true;
}
HandlerMethod method = (HandlerMethod) handler;
SignatureAnnotation signatureAnnotation = method.getMethod().getAnnotation(SignatureAnnotation.class);
if (signatureAnnotation == null) {
return true;
}
if(signatureServer.verifySignature(request.getHeader(SignatureUtils.SIGNATURE)) {
return true;
}
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}