【问题标题】:spring boot static contentspring boot 静态内容
【发布时间】:2018-02-10 19:24:53
【问题描述】:

我在使用 Spring Boot 提供静态内容时遇到问题。

我正在使用默认配置位置:src/main/resources/static/css 和 src/main/resources/static/js。

当页面加载时,我会为所有静态内容获得一个404。我什至在我的安全设置中添加了permitAll。

@Override
protected void configure(HttpSecurity http) throws Exception {
  http
    .authorizeRequests()    
      .antMatchers("/css/**","/js/**").permitAll()
      .antMatchers("/services/**").hasRole("PREAUTH_USER")
      .antMatchers("/", "/dashboard").permitAll()
      .anyRequest().authenticated()
    .and()
      .formLogin().loginPage("/login")
      .permitAll()
    .and()
      .logout()
      .permitAll();
}

我的资源处理程序:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) { 
  registry.addResourceHandler("/**").addResourceLocations("/");
}

...当我点击 login 我的 CSS 正在返回 404: http://localhost:8080/login

这里是响应头:

Request URL:http://localhost:8080/css/preauth.css
Request Method:GET
Status Code:404 
Remote Address:[::1]:8080
Referrer Policy:no-referrer-when-downgrade
Response Headers
view source
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Type:application/json;charset=UTF-8
Date:Fri, 01 Sep 2017 17:04:59 GMT
Expires:0
Pragma:no-cache
Transfer-Encoding:chunked
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block
Request Headers
view source
Accept:text/css,*/*;q=0.1
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Cookie:JSESSIONID=D1399529A7AD61D0FA67ECF0343D3A03
Host:localhost:8080
Referer:http://localhost:8080/login
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36

【问题讨论】:

  • 你是把它当作 jar 来运行吗?
  • 如果您使用默认资源文件夹,则无需配置 addresource 处理程序。您可以将其删除并重试。 SpringBoot 将配置静态文件夹并设置资源准备消耗
  • 也可以访问spring.io/blog/2013/12/19/…查看如何配置资源处理程序。
  • 应该不需要手动配置资源处理程序。 Spring Boot 将为您自动配置它。我想知道您是否无意中使用@EnableWebMvc 关闭了它?
  • 我同意不需要资源处理程序。我的原始配置中没有它。我添加它是因为 404.. 我再次删除它并继续为我的静态资源获取 404。我还删除了@EnableWebMvc

标签: css spring spring-boot


【解决方案1】:

问题似乎是当我在我的 appConfig 类中扩展 WebMvcConfigurationSupport(我需要配置消息转换器)时,WebMvcAutoConfiguration 被禁用。所以我不得不添加

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
        "classpath:/META-INF/resources/", "classpath:/resources/",
        "classpath:/static/", "classpath:/public/" };

和

   @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!registry.hasMappingForPattern("/webjars/**")) {
    registry.addResourceHandler("/webjars/**").addResourceLocations(
            "classpath:/META-INF/resources/webjars/");
}
if (!registry.hasMappingForPattern("/**")) {
    registry.addResourceHandler("/**").addResourceLocations(
            CLASSPATH_RESOURCE_LOCATIONS);
}

编辑: 上述代码不是必需的。我更改了我的配置类以扩展 WebMvcConfigurerAdapter 并且不需要上述更改。请参阅@AndyWilkinson 的以下评论

【讨论】:

  • 你为什么要扩展WebMvcConfigurationSupport?这在 Spring Boot 应用程序中是一件相当不寻常的事情。推荐的方法是扩展WebMvcConfigurerAdapter。这允许您自定义 Spring MVC 的自动配置,同时保持 Spring Boot 的默认值。
  • @AndyWilkinson,谢谢安迪,你是对的。我修改了我的代码,一切都按我的预期工作
  • WebMvcConfigurerAdapter 现已弃用
猜你喜欢
  • 1970-01-01
  • 2018-06-26
  • 2017-07-19
  • 2017-04-05
  • 2017-03-25
  • 1970-01-01
  • 1970-01-01
  • 2014-05-08
  • 2018-02-14
相关资源
最近更新 更多