繁体   English   中英

Thymeleaf 错误地加载静态文件

[英]Thymeleaf incorrectly loads static files

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

在此处输入图片说明

在 index.html 中导入:

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

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

最好的部分: 在此处输入图片说明 检查网站时styles.csstemplates文件夹中作为index.html加载。

做什么?

在 spring security 4.x 中 - spring security 中的资源是permitAll

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

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

请尝试检查以下几点:
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/\**",
      ...
    );
  }

  ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM