springBoot add custom interceptor

1. My project environment: springboot2.0.0+thymeleaf

2. The purpose of adding a custom interceptor: the project runs normally locally, and a large number of errors are reported in the front-end page js and css files after being published to the cloud server. In order to make the imported static file path correct, an interceptor is added and the project path is placed in the request domain for the front-end page to use.

3. The project directory is as follows, the files in the box are the files needed by the custom interceptor:

4. Customize the interceptor, mark the @Component annotation, and put the interceptor into the spring container:

package com.admin.mall.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 作用:拦截所有请求,设置rootPath给前端页面使用
 */
@Component
public class MyInterceptor implements HandlerInterceptor {

    /**
     * 在整个请求结束之后被调用,DispatcherServlet 渲染视图之后执行(进行资源清理工作)
     */
    @Override
    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception {

    }

    /**
     * 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)
     */
    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception {

    }


    /**
     * 在请求处理之前进行调用(Controller方法调用之前)
     *
     * @return 返回true才会继续向下执行,返回false取消当前请求     
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {

        String rootPath = request.getContextPath();
        //设置前端的全局地址,可以是request.getContextPath(),也可以是包含ip、端口号、项目名等等的全路径
        request.setAttribute("rootPath", rootPath);
        return true;
    }

}

5. Add a custom interceptor (registered interceptor) to the project. Mark @Configuration in this class to indicate that it is a configuration class:

package com.admin.mall;

import com.admin.mall.interceptor.MyInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    /**
     * 注入自定义拦截器到该配置类中
     */
    @Autowired
    private MyInterceptor myInterceptor;    

    /**
     * 添加自定义拦截器
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor)
                .addPathPatterns("/**")//拦截的访问路径,拦截所有
                .excludePathPatterns("/static/*");//排除的请求路径,排除静态资源路径
        super.addInterceptors(registry);
    }

    /**
     * 添加静态资源映射路径,css、js等都放在classpath下的static中
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
        super.addResourceHandlers(registry);
    }
}

Because when deploying the project on the cloud server, there are a lot of static resource files that cannot be found in the front end. The shiro is integrated in the project. At first, it was considered to be the problem. The springboot prompt was relatively weak, so I stepped on the pit. It must be noted that if there are multiple interceptors, it is best to pay attention to the order of execution and whether it is executed.

Guess you like

Origin blog.csdn.net/qq_29644709/article/details/92732535