Spring boot configuration interceptor and filter

tags: Springboot

Interceptor

Customize a normal interceptor

Here is the same as before to create a normal 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("My Interceptor");
        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 {

    }
}

Create a configuration class

Create a configuration class, add the relevant interceptor in the configuration class, here you can configure multiple interceptors, add @Configuration class on this class, this will be managed by the spring container.

/**
   * Interceptor configuration class
 */
 @Configuration// indicates that the class will be created by the spring container
public class InterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration r1 = registry.addInterceptor(new PermissionInterceptor());
                 / / Add an intercept request
        r1.addPathPatterns("/*");
                 / / Add unblocked requests
        r1.excludePathPatterns("/login");

                 / / The above is the same as the following
        //registry.addInterceptor(new PermissionInterceptor()).addPathPatterns("/*").excludePathPatterns("/login");
    }
}

Then enter the appropriate url in the browser to test.

filter

method one:

Create a normal 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("Enter filter filter");
        chain.doFilter(request, response);
    }
    @Override
    public void destroy() {
    }
}

Add the annotation configuration filter to the entry method. @ServletComponentScan will scan the servlet-related annotations, such as @WebServlet, @WebFilter, @WebListener.

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

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

}

Method Two:

Create a configuration class configuration filter, where @Bean is equivalent to the bean tag in the previous spring configuration file

@Configuration
public class FilterConfig {


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

Configuring the servlet

method one:

Create a normal 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);
    }
}

Add annotations to the main class and configure the package where the servlet is located

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

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

}

Method 2:

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

Intelligent Recommendation

Spring boot filter, interceptor, listener

Filter Intercept the client's HttpServletRequest before the HttpServletRequest reaches the servlet. Check the HttpServletRequest as needed, or modify the HttpServletRequest header and data. Intercept ...

Configure the interceptor (Filter) in Spring-boot

The advantages of Spring-boot compared to Spring4: 1. Simplify the configuration process of Spring4, perform automatic configuration, and reduce the complexity of project construction (Spring4 configu...

Use of Filter and Interceptor in Spring Boot

Execution order Filter = "Interceptor =" Before = "Method =" After = "After filter Interceptor Registered interceptor Controller method Request URL http: // localhost: 8081 / ...

Spring boot filter and interceptor detailed

Original address:https://www.tsanyang.top/share-detail/836252049946443776.html Recently implemented a permission control function, you want to implement by interceptors, when the business is done as e...

spring boot filter, interceptor related

Interceptors and filters Some reprinted fromSpring Boot uses filter Filter - Jianshu Filters are filtering and preprocessing processes for data. When we visit the website, we sometimes publish some se...

More Recommendation

[Spring Boot] - interceptor configuration

Implement the HandlerInterceptor interface Implement the WebMvcConfigurer interface Test Results Reference blog Spring Boot interceptor is invalid and does not work Spring-boot How to add an intercept...

Spring boot configuration interceptor

Write an interceptor to implement the HandlerInterceptor. Configuration interceptor...

Spring Boot interceptor configuration

Interceptor Spring Boot Configuration ps. (probably not the best step, but this is probably a simple configuration, you can own the Internet to query) Step great view 1. Create an interceptor class an...

Spring-boot configuration interceptor

The function of HandlerInterceptor is similar to the filter, but it provides finer control capabilities: before the request is responded, after the request is responded, before the view is rendered, a...

Spring boot interceptor and shiro filter Filter

Spring boot interceptor interceptor is executed in the servlet, Shiro should be Filter, execute before servlet. Springmvc interceptor is a priority below Shiro, Shiro is a Filter that is customized fo...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top