springmvc 拦截器

一、Spring配置文件

在beans头部标签加入:

···
xmlns:mvc="http://www.springframework.org/schema/mvc"
···
xsi:schemaLocation="...
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
···"

然后加上mvc:interceptors拦截声明:

<mvc:interceptors>  
    <mvc:interceptor>  
        <mvc:mapping path="/**"/> <!--拦截所有controller-->  
        <mvc:exclude-mapping  path="*.png" /> <!--例外的-->
        <bean id="log4UpdateOrDeleteInterceptor"
class="com.XXX.testmvc.interceptor.Log4UpdateOrDeleteInterceptor"></bean>  
    </mvc:interceptor>  
</mvc:interceptors>  

二、实现拦截器

继承HandlerInterceptorAdapter,按需覆写preHandle和postHandle方法。

public class Log4UpdateOrDeleteInterceptor extends HandlerInterceptorAdapter {  
    protected static final Logger log = Logger.getLogger(Log4UpdateOrDeleteInterceptor.class);  
  
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,  
            Object handler) throws Exception {  
        HttpSession session = request.getSession();  
        String requestUri = request.getRequestURI();  
        log.debug("request uri:" + requestUri);  
        String contextPath = request.getContextPath();  
        String url = requestUri.substring(contextPath.length());  
        if (StringUtils.contains(url, "add") || StringUtils.contains(url, "edit")  
                || StringUtils.contains(url, "delete")) {  
            String user = session.getAttribute(SessionKey.USERNAME_SESSION_NAME) != null ? (String) session  
                    .getAttribute(SessionKey.USERNAME_SESSION_NAME) : null;  
            StringBuffer sb = new StringBuffer();  
            Enumeration<String> a = null;  
            a = request.getParameterNames();  
            while (a.hasMoreElements()) {  
                String key = a.nextElement();  
                sb.append(key + ":" + request.getParameter(key) + ", ");  
            }  
            log.warn(String.format("FBI request warning! user: %s, url: %s, params: {%s}", user,  
                    url, StringUtils.removeEnd(StringUtils.trim(sb.toString()), ",")));  
        }  
        return true;  
    }  
  
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o,  
            ModelAndView mav) throws Exception {  
        HttpSession session = request.getSession();  
        String requestUri = request.getRequestURI();  
        log.debug("request uri:" + requestUri);  
        String contextPath = request.getContextPath();  
        String url = requestUri.substring(contextPath.length());  
        if ((StringUtils.contains(url, "add") || StringUtils.contains(url, "edit") || StringUtils  
                .contains(url, "delete")) && mav != null) {  
            String user = session.getAttribute(SessionKey.USERNAME_SESSION_NAME) != null ? (String) session  
                    .getAttribute(SessionKey.USERNAME_SESSION_NAME) : null;  
            Map<String, Object> map = mav.getModel();  
            StringBuffer sb = new StringBuffer();  
            for (Map.Entry<String, Object> entry : map.entrySet()) {  
                sb.append(entry.getKey() + ":"  
                        + (entry.getValue() != null ? entry.getValue().toString() : null) + ", ");  
            }  
            log.warn(String.format("FBI response warning! user: %s, url: %s, models: {%s}", user,  
                    url, StringUtils.removeEnd(StringUtils.trim(sb.toString()), ",")));  
        }  
    }  
  
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o,  
            Exception excptn) throws Exception {  
        // System.out.println("afterCompletion");  
    }  
  
}
posted @ 2017-11-18 10:50  游园拾忆  阅读(44)  评论(0编辑  收藏  举报