zoukankan      html  css  js  c++  java
  • springboot的拦截器和过滤器实现

    • springboot拦截器
    说明:和springmvc一样去实现HandlerInterceptor接口

    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * 〈一句话功能简述〉<br>
     * TODO(参数拦截器1)
     *
     * @author sunhan
     * @version 1.0.0
     * @since 2020/3/7 10:06 PM
     */
    @Component
    public class ParameterInterceptor1 implements HandlerInterceptor {
    
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                                 Object handler) throws Exception {
            System.out.println("进入了拦截器-----11111");
            return true;
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response,
                               Object handler, ModelAndView modelAndView) {
    
        }
    
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
                                    Object handler, Exception ex) {
    
        }
    }
    View Code

    把自定义拦截器注册到WebMvcConfigurer配置接口中:

    重写addInterceptors方法,添加自定义拦截器的order,pathPatterns

    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    /**
     * 〈一句话功能简述〉<br>
     * TODO()
     *
     * @author sunhan
     * @version 1.0.0
     * @since 2020/3/7 10:25 PM
     */
    @Configuration
    public class InterceptConfiguration implements WebMvcConfigurer {
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(new ParameterInterceptor1()).addPathPatterns("/get*");
            registry.addInterceptor(new ParameterInterceptor2()).addPathPatterns("/save*");
        }
    }
    View Code
    • springboot过滤器

    说明:注解方式和注册方式

      1、注解方式:

    import org.springframework.core.annotation.Order;
    
    import javax.servlet.*;
    import javax.servlet.annotation.WebFilter;
    import java.io.IOException;
    
    /**
     * 〈一句话功能简述〉<br>
     * TODO()
     *
     * @author sunhan
     * @version 1.0.0
     * @since 2020/3/6 5:28 PM
     */
    @WebFilter(filterName = "requestParamFilter",urlPatterns = "/*")
    @Order(3)
    public class RequestParamFilter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
    
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                             FilterChain filterChain) throws IOException, ServletException {
            System.out.println("sunhan---请求参数过滤器!---test1");
            filterChain.doFilter(servletRequest,servletResponse);
        }
    
        @Override
        public void destroy() {
    
        }
    }
    View Code

      注意:

      1.1、注解方式需要@WebFilter和@Order放在Filter实现类上。

      1.2、注解方式需要@ServletComponentScan放在SpringBootApplication启动类上

      

      2、注册方式:

    import javax.servlet.*;
    import javax.servlet.http.HttpServletRequest;
    import java.io.IOException;
    
    public class MyFilter implements Filter {
    
        @Override
        public void init(FilterConfig arg0) throws ServletException {
        }
    
        @Override
        public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain filterChain)
                throws IOException, ServletException {
            HttpServletRequest request = (HttpServletRequest) srequest;
            System.out.println("this is MyFilter,url :"+request.getRequestURI());
            filterChain.doFilter(srequest, sresponse);
        }
    
        @Override
        public void destroy() {
        }
    
    }
    View Code

      注意:@Bean放在一个方法中,然后通过springboot的FilterRegistrationBean注册到过滤链中

    import org.springframework.boot.web.servlet.FilterRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class WebConfiguration {
    
        @Bean
        public FilterRegistrationBean testFilterRegistration() {
            FilterRegistrationBean registration = new FilterRegistrationBean();
            registration.setFilter(new MyFilter());
            registration.addUrlPatterns("/*");
            registration.setName("MyFilter");
            registration.setOrder(6);
            return registration;
        }
    
        @Bean
        public FilterRegistrationBean test2FilterRegistration() {
            FilterRegistrationBean registration = new FilterRegistrationBean();
            registration.setFilter(new MyFilter2());
            registration.addUrlPatterns("/*");
            registration.setName("MyFilter2");
            registration.setOrder(1);
            return registration;
        }
    }
    View Code

     

  • 相关阅读:
    Max History CodeForces
    Buy a Ticket CodeForces
    AC日记——字符串的展开 openjudge 1.7 35
    AC日记——回文子串 openjudge 1.7 34
    AC日记——判断字符串是否为回文 openjudge 1.7 33
    AC日记——行程长度编码 openjudge 1.7 32
    AC日记——字符串P型编码 openjudge 1.7 31
    AC日记——字符环 openjudge 1.7 30
    AC日记——ISBN号码 openjudge 1.7 29
    AC日记——单词倒排 1.7 28
  • 原文地址:https://www.cnblogs.com/han-sun/p/12459042.html
Copyright © 2011-2022 走看看