1. 程式人生 > >springboot成神之——攔截器

springboot成神之——攔截器

ref type pri quest exceptio ack min 路由 urn

  • 本文介紹spring boot攔截器
    • 創建攔截器類LogInterceptor.java
    • 創建攔截器類OldLoginInterceptor.java
    • 攔截器配置類WebMvcConfig.java
    • 路由InterceptorController.java

本文介紹spring boot攔截器

創建攔截器類LogInterceptor.java

package com.springlearn.learn.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class LogInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)  throws Exception {
        long startTime = System.currentTimeMillis();
        System.out.println("攔截前"+"Start Time: " + startTime);
        request.setAttribute("startTime", startTime);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response,Object handler, ModelAndView modelAndView) throws Exception{
        System.out.println("攔截中"+"Request URL: " + request.getRequestURL());
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("攔截後"+(System.currentTimeMillis()-(Long) request.getAttribute("startTime"))+"");
        
    }
}

創建攔截器類OldLoginInterceptor.java

package com.springlearn.learn.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class OldLoginInterceptor extends HandlerInterceptorAdapter {
 
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
 
        System.out.println("攔截前"+"重定向到 /admin/login");
        response.sendRedirect(request.getContextPath() + "/admin/login");
        return false;
    }
 
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("攔截中");
    }
 
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("攔截後");
    }
 
}

攔截器配置類WebMvcConfig.java

package com.springlearn.learn.config;

import com.springlearn.learn.interceptor.LogInterceptor;
import com.springlearn.learn.interceptor.OldLoginInterceptor;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer  {
 
   @Override
   public void addInterceptors(InterceptorRegistry registry) {
      registry.addInterceptor(new LogInterceptor());

      registry.addInterceptor(new LogInterceptor()).addPathPatterns("/admin/*").excludePathPatterns("/admin/oldLogin");

      registry.addInterceptor(new OldLoginInterceptor()).addPathPatterns("/admin/oldLogin");  
   }
 
}

路由InterceptorController.java

package com.springlearn.learn.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class InterceptorController {
    
    @ResponseBody
    @RequestMapping(value = { "/", "/test" })
    public String test(Model model) {
        return "test";
    }
 
    @ResponseBody
    @Deprecated
    @RequestMapping(value = { "/admin/oldLogin" })
    public String oldLogin(Model model) {
        return "oldLogin";
    }
    
    @ResponseBody
    @RequestMapping(value = { "/admin/login" })
    public String login(Model model) {
        return "login";
    }
 
}

// 運行即可

springboot成神之——攔截器