springboot拦截器放行静态资源

/ 教程 / 6 条评论 / 1718 浏览
springboot拦截器放行静态资源

编写一个类

 实现 WebMvcConfigurer
package com.my.blog.website.interceptor;
import com.my.blog.website.utils.TaleUtils;
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.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * 向mvc中添加自定义组件
 * Created by BlueT on 2017/3/9.
 * 更换MVC配置类
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Resource
    private BaseInterceptor baseInterceptor;
    @Resource
    private IpInterceptor ipInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(ipInterceptor);
        registry.addInterceptor(baseInterceptor);
    }

    /**
     * 添加静态资源文件,外部可以直接访问地址
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/upload/**").addResourceLocations("file:" + TaleUtils.getUplodFilePath() + "upload/");
    }
}

  1. :meigui::meigui::meigui::shenli::shenli::yinxian::yinxian::yinxian::yinxian::damuzhi::damuzhi::damuzhi::damuzhi::haha2:

    回复
  2. 🇭🇺:han::caihong::caihong::caihong::caihong::caihong::caihong::caihong::caihong::caihong::caihong::caihong::caihong::yinxian::weiqu:

    回复
  3. :mianqiang::mianqiang::taikaixin::neng::huaxin::yi::tu:

    回复