springboot拦截器放行静态资源

warning: 这篇文章距离上次修改已过315天,其中的内容可能已经有所变动。

springboot拦截器放行静态资源
# 编写一个类
实现 WebMvcConfigurer

```java
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/");
}
}

```

版权声明 ▶ 本网站名称:我的学习笔记
▶ 本文链接:https://ooolo.net/article/116.html
▶ 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行核实删除。
▶ 转载本站文章需要遵守:商业转载请联系站长,非商业转载请注明出处!!!

最后修改于:2023年04月24日 17:36

已有 7 条评论

  1. qgtwhetynetynne

  2. :meigui::meigui::meigui::shenli::shenli::yinxian::yinxian::yinxian::yinxian::damuzhi::damuzhi::damuzhi::damuzhi::haha2:

  3. :hu::han::caihong::caihong::caihong::caihong::caihong::caihong::caihong::caihong::caihong::caihong::caihong::caihong::yinxian::weiqu:

  4. :mianqiang::mianqiang::taikaixin::neng::huaxin::yi::tu:

添加新评论

icon_mrgreen.pngicon_neutral.pngicon_twisted.pngicon_arrow.pngicon_eek.pngicon_smile.pngicon_confused.pngicon_cool.pngicon_evil.pngicon_biggrin.pngicon_idea.pngicon_redface.pngicon_razz.pngicon_rolleyes.pngicon_wink.pngicon_cry.pngicon_surprised.pngicon_lol.pngicon_mad.pngicon_sad.pngicon_exclaim.pngicon_question.png