• Spring拦截器


    Spring的拦截器配置文件

    <mvc:interceptors>
        <mvc:interceptor>
             <mvc:mapping path="/**" />
              <!-- 不需要拦截的路径-->
              <mvc:exclude-mapping path="/login"/>           
             <bean class="com.springmvc.controller.interceptor.LoginInterceptor" />
        </mvc:interceptor>
       <!-- 可以配置多个 -->
    </mvc:interceptors>
    

    LoginInterceptor.java

    package com.springmvc.controller.interceptor;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
    
    public class LoginInterceptor extends HandlerInterceptorAdapter {
        
        private static final Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);
        @Override
        public boolean preHandle(HttpServletRequest request,
                HttpServletResponse response, Object handler) throws Exception {
            long startTime = System.currentTimeMillis();
            logger.info("请求URL::" + request.getRequestURL().toString()
                    + ":: Start Time=" + System.currentTimeMillis());
            request.setAttribute("startTime", startTime);
            //if returned false, we need to make sure 'response' is sent
            return true;
        }
     
        @Override
        public void postHandle(HttpServletRequest request,
                HttpServletResponse response, Object handler,
                ModelAndView modelAndView) throws Exception {
            System.out.println("请求URL::" + request.getRequestURL().toString()
                    + " Sent to Handler :: Current Time=" + System.currentTimeMillis());
             //这里可以做业务操作如判断session中user,不存在做跳转。   
            //we can add attributes in the modelAndView and use that in the view page
        }
     
        @Override
        public void afterCompletion(HttpServletRequest request,
                HttpServletResponse response, Object handler, Exception ex)
                throws Exception {
            long startTime = (Long) request.getAttribute("startTime");
            logger.info("请求URL::" + request.getRequestURL().toString()
                    + ":: End Time=" + System.currentTimeMillis());
            logger.info("请求URL::" + request.getRequestURL().toString()
                    + ":: Time Taken=" + (System.currentTimeMillis() - startTime));
        }
     
    }
    

    UserController.java

    package com.springmvc.controller;
    
    import java.util.Date;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/")
    public class UserController { 
    	private static final Logger logger = LoggerFactory.getLogger(UserController.class);	 
    	
    	/**
    	 * 测试拦截器	
    	 * @param model
    	 * @return
    	 */
    	 @RequestMapping(value = "/home", method = RequestMethod.GET)
    	    public String home(Model model) {
    		 	logger.info("Welcome home! ");
    	        try {
    	            Thread.sleep(1000);
    	        } catch (InterruptedException e) {
    	            e.printStackTrace();
    	        }
    	         
    	        model.addAttribute("serverTime", new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    	        logger.info("Before returning view page");
    	        return "home";
    	    }
    
    }
    
    

    浏览器URL测试 :http://localhost:8080/example/home

  • 相关阅读:
    《RocketMQ源码系列》心跳机制
    《RocketMQ源码系列》broker是如何注册到nameserver的
    使用redis客户端工具RedisClient连接windows和linux下的redis并解决无法连接redis的问题
    windows下安装Linux
    redis客户端工具RedisClient的使用
    redis哨兵机制配置
    redis数据的两种持久化方式rdb和aof对比(二)
    redis数据的两种持久化方式rdb和aof对比(一)
    windows下的redis主从复制
    redis持久化配置:rdb和aof
  • 原文地址:https://www.cnblogs.com/tonyY/p/4881007.html
Copyright © 2020-2023  润新知