【问题标题】:Access javascript files present inside JAR in parent springboot project访问父 springboot 项目中 JAR 中存在的 javascript 文件
【发布时间】:2019-05-21 16:47:18
【问题描述】:

我必须通过 JAR 项目提供静态 .js 文件。 为此,我创建了一个 JAR(使用 MAVEN),如下所示:

parent
|--com
|--META-INF
|--webapp
   |--resources
      |--js
         |--myjs.js

现在,我将此 JAR 添加到我的父 spring-boot 项目中,并添加到 JSPs 之一中

<script src="resources/js/myjs.js"></script>

这给了我一个 404 错误。

我的结论:要么启动项目没有将 JAR 中的 webapp 文件夹合并到它自己的 webapp 中,要么我访问文件不正确。

这些问题没有帮助(无法更改WebMvcConfigurerAdapter 并且严格不允许使用 java 代码):

SpringBoot - accessing a file inside resources folder
Serve static resources within jar files by Spring boot

通常,此类事情是使用 overlays 处理的,但对于简单的用例来说,这将是多余的。

【问题讨论】:

    标签: java spring maven spring-boot jar


    【解决方案1】:

    您的 webapp 文件夹是安全文件夹。您无法直接访问其中的文件。 您需要允许所有传入的 js 和其他静态文件请求,如下所示:-

    @Configuration
    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
             http
                .authorizeRequests()
                .antMatchers("/", "/js/**", "/css/**").permitAll();
            http.csrf().disable();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-18
      • 1970-01-01
      • 2013-10-02
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      • 2016-11-21
      • 1970-01-01
      • 2019-07-27
      相关资源
      最近更新 更多