spring如何配置拦截所有请求?
解决方法:
1.spring.xml配置
<mvc:interceptors>
<bean class="com.crm.base.interceptor.AuthInterceptor" />
</mvc:interceptors>
2.拦截器AuthInterceptor
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class AuthInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
String baseUri = request.getContextPath();
String url=baseUri+"/view/crm/tologin.jsp";
String path = request.getServletPath().toLowerCase().replace("//", "/");
if(path.startsWith("/login")) {//以login开头就不拦截
}else{//return false拦截
return false;
}
return super.preHandle(request, response, handler);
}
@Override
public void afterCompletion(HttpServletRequest request,HttpServletResponse response, Object handler, Exception ex) throws Exception {
if(ex != null){
response.setStatus(response.SC_SERVICE_UNAVAILABLE);
Map<String, Object> result = new HashMap<String, Object>();
result.put("success", false);
result.put("msg", msg);
HtmlUtil.writerJson(response, result);
}else{
super.afterCompletion(request, response, handler, ex);
}
}
}
本文链接:http://yayihouse.com/yayishuwu/chapter/1493