当前位置:   article > 正文

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

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

 # 定义过滤器

  1. package com.holen.filter;
  2. import jakarta.servlet.Filter;
  3. import jakarta.servlet.FilterChain;
  4. import jakarta.servlet.ServletException;
  5. import jakarta.servlet.ServletRequest;
  6. import jakarta.servlet.ServletResponse;
  7. import java.io.IOException;
  8. public class MyFilter implements Filter {
  9. @Override
  10. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  11. System.out.println("目标方法执行前的逻辑");
  12. chain.doFilter(request, response);
  13. System.out.println("目标方法执行完成后的逻辑");
  14. }
  15. @Override
  16. public void destroy() {
  17. System.out.println("销毁逻辑,可选");
  18. }
  19. }

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

  1. package com.holen.filter;
  2. import org.springframework.boot.web.servlet.FilterRegistrationBean;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. @Configuration
  6. public class FilterConfig {
  7. @Bean
  8. public FilterRegistrationBean<MyFilter> customFilterRegistration() {
  9. FilterRegistrationBean<MyFilter> registration = new FilterRegistrationBean<>();
  10. registration.setFilter(new MyFilter());
  11. registration.addUrlPatterns("/*");
  12. registration.setName("MyFilter");
  13. registration.setOrder(1);
  14. return registration;
  15. }
  16. }

# 定义拦截器

  1. package com.holen.Interceptor;
  2. import jakarta.servlet.http.HttpServletRequest;
  3. import jakarta.servlet.http.HttpServletResponse;
  4. import org.springframework.web.servlet.HandlerInterceptor;
  5. import org.springframework.web.servlet.ModelAndView;
  6. public class MyInterceptor implements HandlerInterceptor {
  7. @Override
  8. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  9. // 在请求到达目标方法之前执行的逻辑
  10. System.out.println("Executing custom interceptor before request is processed.");
  11. return true; // 返回 true 表示继续执行后续的拦截器或目标方法,返回 false 表示终止执行
  12. }
  13. @Override
  14. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  15. // 在请求被发送回客户端之前或在目标方法执行完成后执行的逻辑
  16. System.out.println("Executing custom interceptor after request is processed.");
  17. }
  18. @Override
  19. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  20. // 请求完成后的清理逻辑
  21. }
  22. }

# 注册拦截器

  1. package com.holen.Interceptor;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  4. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  5. @Configuration
  6. public class WebMvcConfig implements WebMvcConfigurer {
  7. @Override
  8. public void addInterceptors(InterceptorRegistry registry) {
  9. registry.addInterceptor(new MyInterceptor())
  10. .addPathPatterns("/**"); // 可以添加拦截的路径模式
  11. }
  12. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/83398
推荐阅读
相关标签
  

闽ICP备14008679号