Java Spring引导应用程序找不到webjar文件
我在Spring Boot中有一个应用程序。我给gradle增加了依赖性Java Spring引导应用程序找不到webjar文件,java,spring,spring-mvc,spring-boot,gradle,Java,Spring,Spring Mvc,Spring Boot,Gradle,我在Spring Boot中有一个应用程序。我给gradle增加了依赖性 compile 'org.webjars:bootstrap:4.0.0' 然后我将其添加到HTML文件中 <link rel="stylesheet" href="/webjars/bootstrap/4.0.0/css/bootstrap.min.css"/> HTML页面代码如下所示 但是CSS文件的地址不起作用 为什么会这样?显然,在Spring Boot中,只需添加依赖项,就可
compile 'org.webjars:bootstrap:4.0.0'
然后我将其添加到HTML文件中
<link rel="stylesheet" href="/webjars/bootstrap/4.0.0/css/bootstrap.min.css"/>
HTML页面代码如下所示
但是CSS文件的地址不起作用
为什么会这样?显然,在Spring Boot中,只需添加依赖项,就可以自动设置所有内容。假设您使用的是maven。在pom.xml中添加
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
<scope>runtime</scope>
</dependency>
org.webjars
独自创立
3.3.7
运行时
然后在html文件中,这是您应该如何引用webjar的
<link rel="stylesheet" href="/webjars/bootstrap/3.3.7/css/bootstrap.css">
确保您的dependency groupId来自“org.webjars”我和您面临同样的问题。因此,经过一些研究,我发现WebJAR与Spring无关,因此我们必须使用SpringMVC为这些客户机依赖项定义一些映射 假设pom.xml中存在此依赖项:
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.3.1</version>
</dependency>
然后在HTML中引用代码:
<link rel="stylesheet" href="/webjars/bootstrap/4.3.1/css/bootstrap.css">
或者在您的胸腺中更好:
<link th:href="@{/webjars/bootstrap/4.3.1/css/bootstrap.min.css}" rel="stylesheet" />
如果其他使用Spring Boot 2的人看到了这一点-使用Spring Security,我面临着同样的问题,不得不做一些更改:
webmvcconfiguer
的我的配置类中删除了@EnableWebMvc
,以便用@springbootplication
注释的主类可以自动配置addResourceHandlers()
方法:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/").resourceChain(false);
}
@Override
public void configure(WebSecurity webSecurity) throws Exception {
webSecurity.ignoring().antMatchers("/webjars/**");
}
websecurityConfigureAdapter
的Spring安全配置类中,我必须做一些事情:
a。让Spring通过删除@EnableWebSecurity
b。重写configure(HttpSecurity http)
方法,就像您可能已经在做的那样,添加ant matcher以允许/webjars/**
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/webjars/**").permitAll()
.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
c。覆盖configure(WebSecurity-WebSecurity)
方法:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/").resourceChain(false);
}
@Override
public void configure(WebSecurity webSecurity) throws Exception {
webSecurity.ignoring().antMatchers("/webjars/**");
}