【springboot】静态资源过滤问题处理

思考:静态资源过滤这个问题 在maven规定的是 约定大于配置 ,所以src目录下的配置文件扫描不到,需要在pom.xml中添加过滤,那么springboot 需要不呢?

静态资源

在 SpringBoot项目中,默认是没有webapp文件夹 webapp文件夹

如果你在创建项目时有添加 web的依赖,那它本身就是一个 web项目。

那问题也就接踵而至,没有web文件夹 我们的静态资源应该放哪里呢。

我们将项目结构展开,扫一眼 猜测的话应该是放到这两个文件夹中。
在这里插入图片描述
思考:我们在ssm项目中要配置pom过滤 那这里呢?就要看源码啦!
在WebMvcAutoConfiguration 自动配置类中找到这个
在这里插入图片描述
拿到源码


@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    if (!this.resourceProperties.isAddMappings()) {
        // 已禁用默认资源处理
        logger.debug("Default resource handling disabled");
        return;
    }
    // 缓存控制
    Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
    CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
    // webjars 配置
    if (!registry.hasMappingForPattern("/webjars/**")) {
        customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
                                             .addResourceLocations("classpath:/META-INF/resources/webjars/")
                                             .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
    }
    // 静态资源配置
    String staticPathPattern = this.mvcProperties.getStaticPathPattern();
    if (!registry.hasMappingForPattern(staticPathPattern)) {
        customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
                                             .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
                                             .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
    }
}

大概意思就是:读一下源代码:比如所有的 /webjars/** , 都需要去 classpath:/META-INF/resources/webjars/ 找对应的资源; springboot自动帮我们做了资源过滤处理

这是自动配置的静态资源 我们还可以自己定制

自定义路径

自定义路径很好理解,就是通过配置文件的形式 对静态资源加载路径进行自定义操作

	/**
	 * Locations of static resources. Defaults to classpath:[/META-INF/resources/,
	 * /resources/, /static/, /public/].
	 */
	private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;

这是存在于自动配置类中的属性(也就是我们可以配置的配置项)。

我们可以通过的 spring.resources.static-locations=来对静态资源加载路径进行配置,一旦配置后 默认的静态资源加载路径就会失效。SpringBoot会以我们配置的路径为准。(默认的路径下面会说)
在这里插入图片描述
一般和spring.mvc.static-Path-Pattern=配合使用,下面的代码为yaml格式,实际上二者并无太多差别。

默认路径

关于默认路径有哪些,我们可以通过在配置文件中点进 static-locations一探究竟

  • /META-INF/resources/
  • /resources/
  • /static/
  • /public/
  • /**(假装有五个)

静态资源的默认访问优先级:
/META-INF/resources/>/resources/>/static/>/public/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot中,可以通过配置过滤器来过滤静态资源。以下是实现步骤: 1. 创建过滤器 创建一个实现javax.servlet.Filter接口的过滤器类,例如: ```java import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; @WebFilter(urlPatterns = "/*") public class StaticResourceFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { // 初始化方法 } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // 过滤逻辑 chain.doFilter(request, response); } @Override public void destroy() { // 销毁方法 } } ``` 2. 注册过滤器 在启动类上添加`@ServletComponentScan`注解,以扫描并注册过滤器。 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication @ServletComponentScan public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` 3. 配置静态资源路径 在application.properties文件中配置静态资源路径,例如: ``` spring.resources.static-locations=classpath:/static/ ``` 4. 配置过滤器排除静态资源过滤器的`doFilter`方法中,可以通过HttpServletRequest的getRequestURI()方法获取请求的URI,然后判断是否需要过滤静态资源。例如: ```java @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; String requestUri = httpRequest.getRequestURI(); // 判断是否需要过滤静态资源 if (!requestUri.startsWith("/static/")) { chain.doFilter(request, response); } } ``` 通过以上步骤,你可以配置一个过滤器来过滤静态资源。注意,如果你使用了Spring Security等安全框架,可能需要在配置中排除静态资源的路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值