V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
fangch
V2EX  ›  问与答

江湖救急, springboot 配置拦截器静态资源的配置老是不生效,弄了一天了,哎!

  •  
  •   fangch · 2019-04-17 10:04:02 +08:00 · 5357 次点击
    这是一个创建于 1850 天前的主题,其中的信息可能已经有所发展或是发生改变。

    springboot 的版本是
    2.1.4

    application.yml 中的配置是:
    spring:mvc:static-path-pattern:/static/**

    mvc 的配置是:
    @Configuration
    public class MvcConfig implements WebMvcConfigurer {

    @Autowired
    private SessionInterceptor sessionInterceptor;
    
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }
    
    @Override
    public void addInterceptors(InterceptorRegistry registry){
    
        registry.addInterceptor(sessionInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/static/**","/login","/js/**");
    
    }
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**");
    }
    

    }

    前台访问的的静态资源的链接是;
    http://localhost:8081/js/jquery-3.3.1.min.js ,这个是已经配置了排除了,但是为什么还是被拦截!

    说明:
    配置中 /login 这个请求已经拦截已经生效了,但是为什么静态资源没有生效,希望大佬帮忙看看有哪些地方错了!

    第 1 条附言  ·  2019-04-17 11:12:59 +08:00
    已经解决
    原因:
    springboot 添加拦截器之后,不认 springboot 之前默认访问的 static 文件夹,访问应该使用 http://localhost:8081/static/js/jquery.min.js,加上 static 文件夹即可
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1553 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 17:05 · PVG 01:05 · LAX 10:05 · JFK 13:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.