WebMvcConfigurationSupport 静态资源失效问题

使用SpringBoot做项目时,用拦截器判断用户登录状态。继承WebMvcConfigurationSupport后,推荐使用WebMvcConfigurer,但会出现静态资源失效问题。文中提供两种解决方法,一是重写方法加入静态文件路径,二是实现接口,可解决该问题。

在使用SpringBoot做项目时,需要使用拦截器判断用户是否登陆,来决定用户是否能访问某些页面。
起初,通过继承来WebMvcConfigurerAdapter

package com.donglei.myblog.interceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.web.servlet.config.annotation.*;
@SpringBootConfiguration
public class AdminLoginAdapter extends WebMvcConfigurerAdapter {
    @Autowired
    AdminLoginInterceptor adminLoginInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(adminLoginInterceptor).addPathPatterns("/admin").excludePathPatterns("/login/**");
        registry.addInterceptor(adminLoginInterceptor).addPathPatterns("/index");
        super.addInterceptors(registry);
    }

但是由于SpringBoot 2.0之后,就将WebMvcConfigurerAdapter废弃使用了,推荐使用WebMvcConfigurationSupport,但是使用WebMvcConfigurationSupport后发现网页不能获取到静态资源。这里提供两种解决方法:

一、重写 addResourceHandlers()方法,加入静态文件路径

package com.donglei.myblog.interceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.web.servlet.config.annotation.*;

@SpringBootConfiguration
public class AdminLoginAdapter extends WebMvcConfigurationSupport {
    @Autowired
    AdminLoginInterceptor adminLoginInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(adminLoginInterceptor).addPathPatterns("/admin").excludePathPatterns("/login/**");
        registry.addInterceptor(adminLoginInterceptor).addPathPatterns("/index");
        super.addInterceptors(registry);
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    //重写这个方法,映射静态资源文件
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/resources/")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/public/");
        super.addResourceHandlers(registry);
    }
}

实践证明,这种方法行之有效,但是有的朋友可能表示,这种方法还是有问题,那就第二种方法,实现WebMvcConfigurer接口

二、实现WebMvcConfigurer接口


package com.donglei.myblog.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.web.servlet.config.annotation.*;
@SpringBootConfiguration
public class AdminLoginAdapter implements WebMvcConfigurer {

    @Autowired
    AdminLoginInterceptor adminLoginInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(adminLoginInterceptor).addPathPatterns("/admin").excludePathPatterns("/login/**");
        registry.addInterceptor(adminLoginInterceptor).addPathPatterns("/index");
        //super.addInterceptors(registry);
    }
}

这样,就解决了WebMvcConfigurationSupport 静态资源失效问题

<think>好的,我现在需要帮助用户解决在Spring Boot项目中添加外部静态资源目录的问题。首先,我应该回顾用户提供的引用内容,看看有没有相关的配置方法。引用[1]、[2]和[5]都提到了使用`spring.web.resources.static-locations`这个配置项来指定静态资源的位置。用户之前的问题是关于如何添加外部目录,所以可能需要使用文件系统路径。 根据引用[3]和[4],除了配置文件,还可以通过继承`WebMvcConfigurationSupport`并重写`addResourceHandlers`方法来添加资源处理器。不过用户现在想添加的是外部目录,也就是位于项目之外的路径,比如`D:/static/`这样的位置。这时候需要注意路径的格式,应该使用`file:`前缀来指定绝对路径。 在配置文件中,用户需要指定`spring.web.resources.static-locations`,默认情况下这个属性包括了classpath下的几个目录。如果用户添加外部目录,需要覆盖默认值还是追加呢?根据引用[2]和[5],如果直接设置这个属性,会覆盖默认的路径,所以最好把默认的路径也包含进去,或者明确列出所有需要的目录。例如,配置为`classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:D:/static/`,这样既保留了原有的classpath路径,又添加了外部目录。 另外,引用[3]提到,如果同时使用配置文件和自定义的`WebMvcConfigurationSupport`,后者会覆盖前者,导致配置文件中的设置失效。所以用户需要选择其中一种方式,或者确保在自定义配置类中也包含所有需要的路径。 还需要考虑操作系统的问题,比如在Linux系统中,路径的写法可能不同,需要使用`file:/mnt/static/`这样的格式。权限问题也可能存在,确保应用有权限读取外部目录。 用户可能还想知道如何测试配置是否生效,可以建议他们创建一个测试HTML文件放在外部目录,然后通过浏览器访问该文件,检查是否能够正确加载。 总结下来,有两种主要方法:通过配置文件设置`spring.web.resources.static-locations`,或者通过Java配置类重写`addResourceHandlers`。用户需要根据具体情况选择,并注意路径格式和权限问题。</think>在Spring Boot项目中添加外部静态资源目录可以通过以下两种方式实现,均支持文件系统绝对路径: ### 一、配置文件方式(推荐) 在`application.yml`或`application.properties`中配置: ```yaml spring: web: resources: static-locations: - classpath:/META-INF/resources/ - classpath:/resources/ - classpath:/static/ - classpath:/public/ - file:D:/static/ # Windows系统 - file:/mnt/static/ # Linux系统 ``` *说明*: 1. `file:`前缀表示文件系统路径,需使用绝对路径 2. 路径分隔符Windows用`/`或`\\`,Linux用`/` 3. 需要保留默认的classpath路径时,必须显式列出所有路径[^2][^5] ### 二、Java配置类方式 创建配置类实现资源映射: ```java @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**") .addResourceLocations( "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "file:D:/static/"); } } ``` *注意事项*: 1. 文件路径需要应用运行权限 2. 热部署时修改外部目录内容需要重启应用 3. 两种方式同时存在时,Java配置类方式优先级更高[^3][^4] ### 验证方式 访问`http://localhost:8080/测试文件.txt`,若文件存放在`D:/static/`目录下能正常访问即配置成功。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值