访问父springboot项目中的JAR中存在的javascript文件

时间:2018-12-21 07:44:37

标签: java spring maven spring-boot jar

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

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

现在,我将此JAR添加到了父级spring-boot项目中,并添加了JSP

之一
<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处理的,但是对于一个简单的用例来说,这是多余的。

1 个答案:

答案 0 :(得分:0)

您的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();
    }
}
相关问题