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

本文介绍了如何在Java中使用JakartaServletAPI实现Filter,以及如何通过SpringMVC配置HandlerInterceptor进行请求拦截。MyFilter和MyInterceptor分别展示了过滤器和拦截器的使用示例,包括filter链的处理和预处理、后处理及请求完成后的逻辑。
摘要由CSDN通过智能技术生成

 # 定义过滤器

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<MyFilter> customFilterRegistration() {
        FilterRegistrationBean<MyFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new MyFilter());
        registration.addUrlPatterns("/*");
        registration.setName("MyFilter");
        registration.setOrder(1);
        return registration;
    }
}

# 定义拦截器

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值