码上敲享录 > Spring框架常见问题详解分享 > spring如何配置拦截所有请求?

spring如何配置拦截所有请求?

上一章章节目录下一章 2018-08-16已有3655人阅读 评论(0)

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


0

有建议,请留言!

  • *您的姓名:

  • *所在城市:

  • *您的联系电话:

    *您的QQ:

  • 咨询问题:

  • 提 交