<context:component-scan base-package="com.stack" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:annotation-config/>
<mvc:annotation-driven/>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/secure/**"/>
<bean class="com.stack.interceptors.SecurityInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
控制器看起来像这样
@Controller
@RequestMapping("secure")
public class SecureController {
@RequestMapping(value = "/good", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Boolean getSecure() {
return true;
}
拦截器看起来像这样,永远不会被调用:
public class SecurityInterceptor extends HandlerInterceptorAdapter {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
throws Exception {
System.out.println("Intercepted");
String token = request.getHeader("TOKEN");
if(token.equals("TEST")){
return true;
}
response.sendRedirect("/secure/other/" ); //sends to other controller method
return false;
}
到目前为止,仅使用显示的上下文剪断器和 springs 测试模拟进行了测试(对于其他所有东西都可以正常工作,控制器测试在现在之前已经设置并可以正常工作)
@Autowired
protected WebApplicationContext webApplicationContext;
和
Mock mockMvc = webAppContextSetup(webApplicationContext).build();