SpringBoot中使用拦截器、servlet、过滤器Filter

Spring Boot 使用拦截器步骤:

1. 创建类实现 HandlerInterceptor 接口
package com.dongmu.interceptor;

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 {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}

2:注册拦截器对象
package com.dongmu.config;

import com.dongmu.interceptor.MyInterceptor;
import org.omg.PortableInterceptor.Interceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyConfig implements WebMvcConfigurer {


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String path = "/user/**";
        String excludePath = "/user/login";

        //创建一个自定义的拦截器对象
        HandlerInterceptor interceptor = new MyInterceptor();


        //
        registry.addInterceptor(interceptor).addPathPatterns(path).excludePathPatterns(excludePath);
    }
}

下面我们进行测试

创建测试使用的 Controller
@RequestMapping("/user/list")
    @ResponseBody
    public String test01(){
        return "测试使用的list";
    }

    @RequestMapping("/user/login")
    @ResponseBody
    public String test02(){
        return "测试使用的login";
    }

    @RequestMapping("/user2/login")
    @ResponseBody
    public String test03(){
        return "测试使用的其他不被拦截的路径。";
    }

访问list
SpringBoot中使用拦截器、servlet、过滤器Filter
SpringBoot中使用拦截器、servlet、过滤器Filter
SpringBoot中使用拦截器、servlet、过滤器Filter
没变化说明没有被拦截
SpringBoot中使用拦截器、servlet、过滤器Filter
SpringBoot中使用拦截器、servlet、过滤器Filter
还是没有变化,说明也没有走拦截器。
SpringBoot中使用拦截器、servlet、过滤器Filter
上面的测试说明拦截器已经有效果了。

SpringBoot中使用Servlet

1:创建servlet类
package com.dongmu.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class MyServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.write("SpringBoot中使用Servlet");
        out.flush();
        out.close();
    }
}

2:把servlet类注册到@Configuraction配置类中

    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        return new ServletRegistrationBean(new MyServlet(),"/myservlet");
    }

结果:
SpringBoot中使用拦截器、servlet、过滤器Filter
SpringBoot中使用过滤器

1:创建filter类
package com.dongmu.filter;

import javax.servlet.*;
import java.io.IOException;

public class MyFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        System.out.println("执行了Filter方法。");
        filterChain.doFilter(servletRequest,servletResponse);

    }


}

2:将过滤器注册到配置类中
@Bean
    public FilterRegistrationBean filterRegistrationBean(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new MyFilter());

        filterRegistrationBean.addUrlPatterns("/filter/*");

        return filterRegistrationBean;
    }
创建测试类
@RequestMapping("/filter/filtertest")
    @ResponseBody
    public String test04(){
        return "测试使用过滤器。";
    }

SpringBoot中使用拦截器、servlet、过滤器Filter
SpringBoot中使用拦截器、servlet、过滤器Filter
字符集编码过滤器
首先演示一下乱码的问题:

package com.dongmu.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class MyServlet2 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.write("SpringBoot中使用Servlet---------冬木");
        out.flush();
        out.close();
    }
}

@Bean
    public ServletRegistrationBean noFilterServlet(){
        return new ServletRegistrationBean(new MyServlet2(),"/myservlet2");
    }

SpringBoot中使用拦截器、servlet、过滤器Filter
配置类中注册springboot写好的过滤器:

@Bean
    public FilterRegistrationBean filterRegistrationBean2(){

        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("utf-8");
        filter.setForceEncoding(true);

        filterRegistrationBean.setFilter(filter);
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }

然后我们还要改一下配置文件

server:
  port: 8084
  servlet:
    context-path: /myboot
    encoding:
      enabled: false
#enabled:springboot中默认开启了字符集过滤器,编码是ISO-8859-1的,我们把它设置为false的意思
就是不用它默认的这个编码。使用我们自己配置的编码过滤器。

SpringBoot中使用拦截器、servlet、过滤器Filter

使用配置文件的形式使用过滤器
我们也可以使用springboot的字符集过滤器进行配置使用即可
只是在使用之前配置一下它的编码方式即可,因为没有修改默认的是ISO-8859-1。

server:
  port: 8084
  servlet:
    context-path: /myboot
    encoding:
      enabled: true(这个默认就是true,可以不进行显式说明)
      charset: utf-8
      force: true

上一篇:分布式爬虫部署


下一篇:Filter --ResourceFilter --ActionFilter - 做aop