SpringBoot系列—2.SpringBoot拦截器篇

SpringBoot系列—1.IDEA搭建SpringBoot框架
SpringBoot系列—2.SpringBoot拦截器篇
SpringBoot系列—3.SpringBoot Redis篇
SpringBoot系列—4.SpringBoot 整合Mybatis、MP(MyBatis-Plus)
SpringBoot系列—5.SpringBoot 整合Mybatis-Plus分页

1.新建拦截器类,实现HandlerInterceptor类,重写preHandle、postHandle、afterCompletion三个方法

image.png

package com.example.demo.interceptor;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class MyInterceptor implements HandlerInterceptor {

    /**
     * 拦截器预处理 控制器方法未执行
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("----------拦截器预处理----------");
        String token = request.getParameter("token");
        if(StringUtils.isEmpty(token)){
            PrintWriter writer = null;
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html; charset=utf-8");
            try {
                writer = response.getWriter();
                writer.print("没有token参数");
            } catch (IOException e) {
            } finally {
                if (writer != null)
                    writer.close();
            }
            return false;
        }else{
            return true;
        }
    }

    /**
     * 控制器方法执行完毕
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("----------控制器方法执行完毕----------");
    }

    /**
     * 处理完成
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("----------finish----------");
    }
}

2.新建拦截器配置类,将上一步写的拦截器在这里配置,拦截路径暂时以全部为例

image.png

package com.example.demo.interceptor;

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

@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }
}

3.控制器打印一个记录

image.png

4.运行程序,浏览器测试拦截器是否有效

image.png

image.png

image.png

image.png

你可能感兴趣的:(SpringBoot系列—2.SpringBoot拦截器篇)