如果需要自定义拦截器,可实现接口cn.com.tcc.ofa.common.interceptor.BaseHandlerInterceptor
,重写接口的方法,然后用@Component
将其注册为Bean。框架会自动将其添加到spring mvc的拦截调用链中。
例如:
@Component
public class AccessTokenInterceptor implements BaseHandlerInterceptor {
@Override
public List<String> pathPatterns() {
//希望对哪些请求进行拦截
return Lists.newArrayList("/open-api/**", "/oauth2/tcc/**");
}
@Override
public List<String> excludePathPatterns() {
//排除掉哪些请求不进行拦截,没有的话就返回空
return null;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//编写自己的逻辑
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 {
//编写自己的逻辑
}
}