我将我的 spring mvc 应用程序配置从 xml 更改为代码。自更改以来,我的拦截器中的所有注入属性均为空(authenticationService)。
代码如下所示:
public class WebAuthenticationInterceptor extends HandlerInterceptorAdapter {
@Resource(type=WebAuthenticationService.class)
private IAuthenticationService authenticationService;
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
if(authenticationService.authenticate(request).authenticated == false)
{
if(isAjax(request))
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
else
response.sendRedirect(String.format("%s/#/account/logout", request.getContextPath()));
return false;
}
return true;
}
public static boolean isAjax(HttpServletRequest request) {
return "XMLHttpRequest".equals(request.getHeader("X-Requested-With"));
}
}
和拦截器配置:
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new WebAuthenticationInterceptor()).addPathPatterns("/home/**");
registry.addInterceptor(new MobileAuthenticationInterceptor()).addPathPatterns("/api/**");
}
你能说明我在做什么错吗?
谢谢