在 Spring MVC 应用中找不到静态资源

Static resources not found in Spring MVC app

我正在开发 MVC 网络应用程序。使用具有 Spring 安全性的 Spring Boot 2.0.1。 尝试访问静态资源时出现错误 404。

我尝试了不同的方法,阅读了很多主题,但找不到任何解决方案。

配置class:

@SpringBootApplication
@EnableWebMvc
public class FriendlyFireChessApplication extends SpringBootServletInitializer implements WebMvcConfigurer {

    @Autowired
    private SpringApplicationContext springApplicationContext;

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(FriendlyFireChessApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(FriendlyFireChessApplication.class, args);
    }

    /*
    some beans here
    */

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

项目结构:

index.html:

<DOCTYPE html>
    <html lang="en">

    <head>
        <title>Friendly fire chess</title>

        <link rel="stylesheet" type="text/css" href='/static/css/style.css'/>
    </head>

    <body>
        <header>
            <div class="main_header">
                <div>
                    <a id="icon"><img src='/static/img/logo_1.png' width="40" height="70" border="0" /></a>
                </div>

                <div id="main_title">
                    <span>Friendly Fire Chess</span>
                </div>

                <div class="authentication_bar">
                    <div>
                        <span><a id="log_in_button" href='http://www.ffchess.org/login'>Login</a></span>
                    </div>

                    <div>
                        <span><a id="sign_in_button" href="http://www.ffchess.org/signin">Sign In</a></span>
                    </div>
                </div>

            </div>

        </header>

    </html>

安全设置:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable().authorizeRequests()
            .antMatchers(HttpMethod.POST, SecurityConstants.SIGN_UP_URL)
            .permitAll()
            .antMatchers(HttpMethod.GET, SecurityConstants.VERIFICATION_EMAIL_URL)
            .permitAll()
            .antMatchers(HttpMethod.POST, SecurityConstants.PASSWORD_RESET_REQUEST_URL)
            .permitAll()
            .antMatchers(HttpMethod.POST, SecurityConstants.PASSWORD_RESET_URL)
            .permitAll()
            .antMatchers(SecurityConstants.H2_CONSOLE)
            .permitAll()
            .antMatchers(SecurityConstants.HOME_PAGE)
            .permitAll()
            .antMatchers("/resources/**", "/static/css/**", "/static/img/**")
            .permitAll()
            .anyRequest().authenticated().and()
            .addFilter(getAuthenticationFilter())
            .addFilter(new AuthorizationFilter(authenticationManager()))
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

}

这一切有什么问题?

根据项目结构,您的所有资源都将被复制到您的 class 路径下的 static 目录中,并且不会有任何像 resources 这样的位置。因此,它无法解决。

资源位置应与 class路径一起指定

registry.addResourceHandler("/static/**")
   .addResourceLocations("classpath:/static/");

为了安全起见,您也可以像这样class多位置查找

.addResourceLocations(new String[]{"classpath:/static/", "/"});

Spring 默认包含这些,除非被覆盖

["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]