springboot成神之——拦截器

Posted 叶家伟的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot成神之——拦截器相关的知识,希望对你有一定的参考价值。

本文介绍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成神之——拦截器的主要内容,如果未能解决你的问题,请参考以下文章

springboot成神之——RestTemplate访问Rest

springboot成神之——websocket发送和请求消息

java成神之——enum枚举操作

java成神之——文件IO

java成神之——HttpURLConnection访问api

java成神之——安全和密码