SpringBoot2.0拦截器访问静态资源的坑

SpringBoot 2.0 对静态资源进行了拦截,需额外配置允许访问 '/static/' 和 '/public/' 路径。默认配置变更导致2.0以前Thymeleaf模板中@{/images/1.png}无法直接访问,现需改为@{/static/images/1.png}。这为前端访问带来变化,需注意调整。
摘要由CSDN通过智能技术生成

SpringBoot2.0以下,访问静态资源是默认放行的,2.0静态资源一并拦截。
需要加入"/static/", "/public/"

/**
     * 拦截器配置
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/",  "/webjars/**", "/static/**", "/public/**");
    }

并且还要添更改默认配置路径,将系统默认"classpath:/static/“改为”/static/**"

@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/public/**").addResourceLocations("classpath:/public/");
    }

但这个产生一个问题,2.0以下前端thymeleaf可以@{/images/1.png}直接访问static里的images

<img th:src="@{/images/1.png}"
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值