攔截器聲明
實現HandlerInterceptor接口:
Public class HandlerInterceptor1 implements HandlerInterceptor{
/**
* controller執行前調用此方法
* 返回true表示繼續執行,返回false中止執行
* 這裏可以加入登錄校驗、權限攔截等
*/
@Override
Public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
// TODO Auto-generated method stub
Return false;
}
/**
* controller執行後但未返回視圖前調用此方法
* 這裏可在返回用戶前對模型數據進行加工處理,比如這裏加入公用信息以便頁面顯示
*/
@Override
Public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
}
/**
* controller執行後且視圖返回後調用此方法
* 這裏可得到執行controller時的異常信息
* 這裏可記錄操作日誌,資源清理等
*/
@Override
Public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
}
}
或者繼承HandlerInterceptorAdapter抽象類實現部分方法:
public class LoginInterceptor extends HandlerInterceptorAdapter{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
//1、請求到登錄頁面 放行
if(request.getServletPath().startsWith("loginUrl")) {
return true;
}
//2、比如退出、首頁等頁面無需登錄
//3、如果用戶已經登錄 放行
if(request.getSession().getAttribute("username") != null) {
//更好的實現方式的使用cookie
return true;
}
//4、如果頁面上某個按鈕進行ajax請求後臺數據。用戶請求前,服務器突然關掉,之後服務器啓動,用戶正好點擊按鈕進行ajax後臺請求
//此時應該進行重定向到登錄頁面,需要加上如下代碼,否則頁面不發聲任何改變
if("XMLHttpRequest".equals(request.getHeader("X-Requested-With")) && request.getSession().getAttribute("username") == null){//判斷是否爲ajax請求
PrintWriter out = response.getWriter();//如果是ajax請求,返回一個標識,告訴ajax請求無效,需要重新登錄
out.print("login");
return false;
}
//5、非法請求 即這些請求需要登錄後才能訪問
//重定向到登錄頁面
response.sendRedirect(request.getContextPath() + "loginUrl");
return false;
}
}
攔截器配置
貼一下springmvc的整體配置吧,此配置只對BeanNameUrlHandlerMapping映射器起作用
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!--映射器-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="myInterceptorOne"/>
<ref bean="myInterceptorTwo"/>
</list>
</property>
</bean>
<!-- 適配器 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/><!-- 要求編寫的處理器action實現controller接口 -->
<!-- 試圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 攔截器 -->
<bean id="myInterceptorOne" class="lee.springmvc.interceptor.MyInterceptorOne"></bean>
<bean id="myInterceptorTwo" class="lee.springmvc.interceptor.MyInterceptorTwo"></bean>
</beans>
或者針對所有mapping配置全局攔截器
<!--攔截器 -->
<mvc:interceptors>
<!--多個攔截器,順序執行 -->
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="lee.springmvc.interceptor.MyInterceptorOne"></bean>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="lee.springmvc.interceptor.MyInterceptorTwo"></bean>
</mvc:interceptor>
</mvc:interceptors>