Thymeleaf错误地加载静态文件

问题描述 投票:2回答:2

所以我正确加载了静态css文件,然后无论出于何种原因,都说不出原因,他们停止加载正确。这是我的项目结构:

enter image description here

在index.html中导入:

<head>
  <link rel="stylesheet" th:href="@{/css/styles.css}"/>
</head>

我甚至试图在spring.resources.static-locations=classpath:/static/设置application.properties无济于事。

最好的部分:enter image description here在检查网站时 - styles.cssindex.html文件夹中加载为templates

做什么?

java spring spring-mvc thymeleaf
2个回答
2
投票

在春季安全4.x - 春天安全的资源是permitAll

在spring security 5.x中 - 你应该手动配置它。

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().antMatchers("/css/**", "/js/**").permitAll()
}

0
投票

请尝试检查以下几点: 1. ResourceHandler有css位置

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

  ...
}

2.在spring-security规则中排除* .css

class SecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers(
      "/css/\**",
      "/js/\**",
      "/img/\**",
      ...
    );
  }

  ...
}
© www.soinside.com 2019 - 2024. All rights reserved.