在springmvc配置文件中加入下面的代码,路由为"/login"的url还是会被拦截到拦截器中
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/login" />
<bean class="com.stooges.common.interceptor.LoginInterceptor"> </bean>
</mvc:interceptor>
</mvc:interceptors>
拦截器LoginInterceptor
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
if (session.getAttribute(Constants.SESS_MANAGER) == null) {
if (request.getHeader("x-requested-with") != null
&& request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){ //如果是ajax请求响应头会有,x-requested-with
response.setHeader("sessionstatus", "timeout");//在响应头设置session状态
response.setHeader("redirectUrl", request.getContextPath() + "/login");
}else{
String path = request.getRequestURI();////原页面
//根据方法不同拼接参数
String queryString="";
if(request.getMethod().equals("GET")){
queryString = request.getQueryString();
}else{
Enumeration<String> params=request.getParameterNames();
while(params.hasMoreElements()){
String paraName=params.nextElement();
queryString+=paraName+"="+request.getParameter(paraName)+"&";
}
}
if (queryString!=null && (!queryString.equals(""))) {
path+="?"+queryString;
}
response.sendRedirect(request.getContextPath() + "/login");
}
return false;
}else{
return true;
}
}
The reason found out today is that the returned view was also intercepted by the interceptor and redirected all the time. Just modify web.xml
The following is to configure all requests to be handled by springmvc
Static resources configured below
Corresponding to
<mvc:default-servlet-handler/>
default-servlet-handler in springmvc.xml will define a DefaultServletHttpRequestHandler in the SpringMVC context, which will screen the requests entering the DispatcherServlet. If it is found that there is no mapping request, the request will be handed over to the WEB The default Servlet processing of the application server. If the request is not a static resource, it will continue to be processed by the DispatcherServlet
Generally, the default Servlet name of a WEB application server is default. If the default Servlet name of the WEB server used is not default, you need to specify it explicitly through the default-servlet-name attribute
Whether your interceptor inherits the class HandlerInterceptorAdapter and overrides the method.
Perhaps you can create a regular pattern without interception, as follows:
Then make regular judgment in the interception method preHandle
<mvc:exclude-mapping path="/login" />Wrong writing?