记录一个mvc拦截器

/**
 * @author 龙小虬
 * @since 2020-07-30 19:41
 */
public class MyFilter implements HandlerInterceptor {
    
    


    /**
     * preHandle在目标Controller方法执行之前
     * 返回值 boolean类型  true表示放行该请求、false表示拦截
     * 作用:做权限验证、日志
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
    
    
        //获取请求行
//        System.out.println("请求方式:"+httpServletRequest.getMethod());
//        System.out.println("URI:"+httpServletRequest.getRequestURI());
//        System.out.println("发出请求客户端IP地址:"+httpServletRequest.getRemoteAddr());
//        System.out.println("服务点接收请求的IP地址:"+httpServletRequest.getLocalAddr());
//        System.out.println("访问客户端的端口号:"+httpServletRequest.getRemotePort());
//        System.out.println("web应用路径:"+httpServletRequest.getContextPath());
//        System.out.println("http协议和版本:"+httpServletRequest.getProtocol());
        //获取请求头
        //Enumeration枚举类型
//        Enumeration<String> headerNames = httpServletRequest.getHeaderNames();
//        //自动补全变量名称 : Ctrl + Alt + v
//        while (headerNames.hasMoreElements()){
    
    
//            String element = headerNames.nextElement();
//            System.out.println(element+":"+httpServletRequest.getHeader(element));
//        }
//        System.out.println("自定义拦截器");

        return true;
    }

    /**
     * postHandler在目标Controller方法执行之后执行,在 视图渲染(render)之前执行
     * 视图渲染:就是把值填充到页面中的这个过程
     * 作用:可以在数据到达页面之前对域中的数据进行修改
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    
    

    }

    /**执行之后,而且在Controller视图渲染执行之后执行。
     * 作用:做资源释放
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @param e
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    
    

    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43911969/article/details/107898026
今日推荐