【问题标题】:Static resources not found in Spring MVC app在 Spring MVC 应用程序中找不到静态资源
【发布时间】:2020-05-22 17:08:48
【问题描述】:

我正在开发 MVC 网络应用程序。将 Spring Boot 2.0.1 与 Spring Security 一起使用。 尝试访问静态资源时出现错误 404。

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

配置类:

@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);

}

这一切有什么问题?

【问题讨论】:

标签: java spring-mvc spring-security staticresource


【解决方案1】:

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

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

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

为了更安全,您可以像这样对多个位置查找进行分类

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

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

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

【讨论】:

  • 我已经尝试在此方法中指定 {registry.addResourceHandler("/static/**") .addResourceLocations("/static/");} 和一堆其他选项。我也试过没有任何 ResourceHandler 。不工作。我认为问题出在安全性的某个地方,但找不到...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-24
  • 2017-01-09
  • 1970-01-01
  • 2015-04-12
  • 2015-11-13
  • 2023-04-01
相关资源
最近更新 更多