首页 > 代码库 > springmvc拦截器

springmvc拦截器

package com.itrus.msign.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.itrus.msign.entity.SysAdmin;
import com.itrus.msign.json.JSONObject;
import com.itrus.msign.web.controller.SysAdminController;
public class MyInterceptor extends HandlerInterceptorAdapter {
// @Autowired(required=false)
// private LicenseCheckService licenseCheckService;
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub
}
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
// TODO Auto-generated method stub
}
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object arg2) throws Exception {
/*
 * if(request.getRequestURI().endsWith("login.do")){ return true; }
 * HttpSession session = request.getSession();
 * if(session.getAttribute("admin")==null){
 * response.sendRedirect("../login.jsp"); return false; }
 */
// System.out.println("拦截地址PATH:"+request.getContextPath());
// System.out.println("拦截地址URI:"+request.getRequestURI());
String uripath = request.getRequestURI();
// uripath.contains("userList.do")
// || uripath.contains("projectList.do")
// || uripath.contains("contractList.do")
// || uripath.contains("toIndex.do")||
HandlerMethod handlerMethod = (HandlerMethod) arg2;
System.out.println("beanClass:" + handlerMethod.getBean().getClass());
Object object = handlerMethod.getBean();
//if (object instanceof SysAdminController) {
//((SysAdminController) object).changePassword("admin", "2", "3",
//"3", request.getSession(), response);
//}
if (!uripath.contains("login") && !uripath.contains("API")) {
SysAdmin sysAdmin = (SysAdmin) request.getSession().getAttribute(
"admin");
if (sysAdmin == null) {
String path = request.getContextPath() + "/login.jsp";
response.sendRedirect(path);
return false;
} else {
return true;
}
}
return true;
}
}


DispatcherServlet with name ‘springmvc‘ processing GET request for [/sealserver/contract/contractList.do]
Looking up handler method for path /contract/contractList.do
Returning handler method [public org.springframework.web.servlet.ModelAndView com.itrus.msign.web.controller.ContractController.contractList(com.itrus.msign.pagenation.ResultFilter<com.itrus.msign.entity.Contract>,java.lang.String,java.lang.String,javax.servlet.http.HttpServletRequest) throws java.io.IOException]
Returning cached instance of singleton bean ‘contractController‘
Last-Modified value for [/sealserver/contract/contractList.do] is: -1
beanClass:class com.itrus.msign.web.controller.ContractController
Creating a new SqlSession

拦截器可以拦截所有的请求,并且可以获取到该请求访问的controller对象并且可以做一些操作

springmvc拦截器