spring boot配置拦截器和过滤器

拦截器

自定义一个普通的拦截器

这里就跟以前一样创建一个普通的拦截器

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("我的拦截器");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}

创建配置类

创建一个配置类,在该配置类中添加相关的拦截器,这里可以配置多个拦截器,该类上添加@Configuration类,这样会被spring容器创建管理。

/**
 * 拦截器配置类
 */
@Configuration//表示该类会被spring容器创建
public class InterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration r1 = registry.addInterceptor(new MyInterceptor());
        //添加拦截请求
        r1.addPathPatterns("/*");
        //添加不拦截的请求
        r1.excludePathPatterns("/login");

        //上面跟下面的写法是一样的
        //registry.addInterceptor(new PermissionInterceptor()).addPathPatterns("/*").excludePathPatterns("/login");
    }
}

之后在浏览器中输入相应的url进行测试即可。

过滤器

方式一:

创建普通的filter

@WebFilter(urlPatterns="/*")
public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        System.out.println("进入filter过滤器");
        chain.doFilter(request, response);
    }
    @Override
    public void destroy() {
    }
}

在入口方法上添加注解配置filter所在的包,@ServletComponentScan会扫描servlet相关的注解,比如@WebServlet、@WebFilter、@WebListener。

@SpringBootApplication
@ServletComponentScan(basePackages={"com.monkey1024.filter"})
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

方法二:

创建配置类配置过滤器,这里的@Bean相当于以前spring配置文件中的bean标签

@Configuration
public class FilterConfig {


    @Bean
    public FilterRegistrationBean myFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean(new MyFilter());
        registration.addUrlPatterns("/*");
        return registration;
    }
}

配置servlet

方式一:

创建普通的servlet

@WebServlet("/myServlet")
public class MyServlet extends HttpServlet {
    private static final long serialVersionUID = -4134217146900871026L;
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().print("hello word");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

在主类上加入注解并配置servlet所在的包

@SpringBootApplication
@ServletComponentScan(basePackages="com.monkey1024.servlet")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

方式二:

@Configuration
public class ServletConfig {
    @Bean
    public ServletRegistrationBean myServletRegistrationBean(){
        ServletRegistrationBean registration = new ServletRegistrationBean(new MyServlet(), "/servlet/myServlet");
        return registration;
    }
}