springBoot添加自定义拦截器

1.我的项目环境:springboot2.0.0+thymeleaf

2.添加自定义拦截器的目的:项目在本地运行正常,发布到云服务器后前端页面js、css文件大量报错。为了使引入的静态文件路径正确,所以添加拦截器,将项目路径放到request域中供前端页面使用。

3.工程目录如下,其中框出来的文件是自定义拦截器需要的文件:

4.自定义拦截器,标注@Component注解,将该拦截器放入到spring容器中:

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.为项目添加自定义的拦截器(注册拦截器)。在该类标注@Configuration,表明它是一个配置类:

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);
    }
}

因为在云服务器部署项目时,前端出现大量找不到静态资源文件,项目中本身集成shiro,起初考虑是这块的问题,而springboot提示比较弱,所以踩了坑。一定注意,如果有多个拦截器,最好关注一下执行顺序和是否执行。

猜你喜欢

转载自blog.csdn.net/qq_29644709/article/details/92732535