另一个 spring 拦截器

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importorg.apache.log4j.Logger;

importorg.springframework.web.servlet.handler.HandlerInterceptorAdapter;

importcom.test.entity.Employee;

/**

*Spring拦截器

*

*@authorjhwen

*

*/

publicclassSessionInterceptorextendsHandlerInterceptorAdapter{

privatefinalLoggerlogger=Logger.getLogger(SessionInterceptor.class);

/**

*在业务处理器处理请求之前被调用

*/

publicbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,

Objecthandler)throwsException{

//要排除拦截的请求

String[]ignoreRegexs=newString[]{"login.do","checkCode.do"};

//获取客户端请求URI

StringrequestURI=request.getRequestURI();

//获取客户端请求

if(null!=requestURI&&!"".equals(requestURI)){

//不同的工程,substring(intparamInt1,intparamInt2)的paramInt1不同

requestURI=requestURI.substring(8,requestURI.length());

}

for(inti=0;i<ignoreRegexs.length;i++){

StringignoreRegex=ignoreRegexs[i];

/**

*如果是login.do和checkCode.do,则不做拦截

*/

if(ignoreRegex.equals(requestURI)){

returntrue;

}

}

/**

*如果不是login.do(登录)和checkCode.do(检验验证码),则做拦截,

*并且,判断是否已经登录

*/

Employeeemp=(Employee)request.getSession().getAttribute("emp");

/**

*如果未登录,则不允许操作;

*如果已经登录,则允许做其他的操作.

*/

if(null==emp){

returnfalse;

}else{

returntrue;

}

}

}

Spring的核心配置文件applicationContext.xml:

<beanclass="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">

<propertyname="interceptors">

<list>

<beanclass="com.test.common.core.interceptor.SessionInterceptor"/>

</list>

</property>

</bean>

相关推荐