配置简单的拦截器java中
springMVC.xml文件中====
<!-- 拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**/*"/>
<mvc:exclude-mapping path="/**/fonts/*"/>
<mvc:exclude-mapping path="/**/*.css"/>
<mvc:exclude-mapping path="/**/*.js"/>
<mvc:exclude-mapping path="/**/*.png"/>
<mvc:exclude-mapping path="/**/*.gif"/>
<mvc:exclude-mapping path="/**/*.jpg"/>
<mvc:exclude-mapping path="/**/*.jpeg"/>
<!-- 登录前放行的请求 -->
<mvc:exclude-mapping path="/**/user/login.do"/>
<mvc:exclude-mapping path="/**/user/toRegister.do"/>
<mvc:exclude-mapping path="/**/code/getVerifyCode"/>
<mvc:exclude-mapping path="/**/user/findUserByPhoneNo.do"/>
<mvc:exclude-mapping path="/**/user/findAllotControlByUserNo.do"/>
<mvc:exclude-mapping path="/**/user/userLogin.do"/>
<mvc:exclude-mapping path="/**/user/toUserMainjsp.do"/>
<bean class="interceptor.InterceptorDemo"></bean>
</mvc:interceptor>
</mvc:interceptors>
java代码
package interceptor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class InterceptorDemo implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
System.out.println("进入preHandle方法");
String user_no=httpServletRequest.getSession().getAttribute("user_no").toString();
System.out.println("进入preHandle方法user_no=="+user_no);
if(!StringUtils.isAllBlank(user_no)) {
System.out.println("session有值");
return true;
}
return false;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
String url = httpServletRequest.getServletPath();//获取当前url
String contextPath = httpServletRequest.getContextPath();//获取当前项目的根目录
System.out.println("url:"+url);
System.out.println("contextPath:"+contextPath);
System.out.println("进入postHandler 方法");
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
System.out.println("进入afterCompletion 方法");
}
}
方法二
springMVC.xml文件======
<mvc:interceptors>
<bean class="com.xy.cc.interceptor.SpringLoginInterceptor">
<property name="excludedUrls">
<list>
<value>/user/getrsakey.do</value>
<value>/</value>
<value>/user/getMsgCode.do</value>
<value>/user/Login.do</value>
<value>/user/msgLogin.do</value>
<value>/user/getCompanyByCompanyidAndUserNo.do</value>
<value>/user/regist.do</value>
<value>/user/toDownLoadJsp.do</value>
<value>/user/resetPassword.do</value>
</list>
</property>
</bean>
</mvc:interceptors>
后台java代码====
package com.xy.cc.interceptor;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.xy.cc.controller.BadSurveyController;
import com.xy.cc.service.UserService;
import com.xy.cc.util.CUtil;
public class SpringLoginInterceptor implements HandlerInterceptor{
private static Logger logger = Logger.getLogger( HandlerInterceptor.class);
private static UserService u= null;
private List<String> excludedUrls;
// private static SimpleDateFormat sdf =new SimpleDateFormat("yy-MM-dd HH:mm:ss");
public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object arg2) throws Exception {
String requestUri = req.getRequestURI();
if(!requestUri.endsWith(".do")){//对非.do不拦截
return true;
}
for (String url : excludedUrls) {
if (requestUri.endsWith(url)) {
return true;
}
}
if(null==u) {
getDao("userService",req);
}
String oUsrNo=req.getHeader("oUsrNo");
if(null==u.findBaseInfoByUsrNo(oUsrNo)) {
logger.info("当前请求oUsrNo='"+oUsrNo+"'");
logger.info("当前请求url="+requestUri+"未获取到用户信息");
return false;
}
return true;
}
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
}
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
}
public List<String> getExcludedUrls() {
return excludedUrls;
}
public void setExcludedUrls(List<String> excludedUrls) {
this.excludedUrls = excludedUrls;
}
private void getDao(String t,HttpServletRequest request){
BeanFactory beanFactory = WebApplicationContextUtils.getRequiredWebApplicationContext((request.getSession()).getServletContext());
u=(UserService) beanFactory.getBean(t);
}
}