【Spring】定义过滤器Filter和拦截器Interceptor

 # 定义过滤器

package com.holen.filter;

import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import java.io.IOException;

public class MyFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("目标方法执行前的逻辑");
        chain.doFilter(request, response);
        System.out.println("目标方法执行完成后的逻辑");
    }

    @Override
    public void destroy() {
        System.out.println("销毁逻辑,可选");
    }
}

# 注册过滤器(servlet版本:jakarta.servlet-api:6.0.0) 

package com.holen.filter;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FilterConfig {
    @Bean
    public FilterRegistrationBean customFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean<>();
        registration.setFilter(new MyFilter());
        registration.addUrlPatterns("/*");
        registration.setName("MyFilter");
        registration.setOrder(1);
        return registration;
    }
}

【Spring】定义过滤器Filter和拦截器Interceptor_第1张图片

# 定义拦截器

package com.holen.Interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 在请求到达目标方法之前执行的逻辑
        System.out.println("Executing custom interceptor before request is processed.");
        return true; // 返回 true 表示继续执行后续的拦截器或目标方法,返回 false 表示终止执行
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        // 在请求被发送回客户端之前或在目标方法执行完成后执行的逻辑
        System.out.println("Executing custom interceptor after request is processed.");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 请求完成后的清理逻辑
    }
}

# 注册拦截器

package com.holen.Interceptor;

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 MyInterceptor())
                .addPathPatterns("/**"); // 可以添加拦截的路径模式
    }
}

你可能感兴趣的:(spring,java,后端)