gpt4 book ai didi

java - Spring MVC 应用程序中找不到静态资源

转载 作者:行者123 更新时间:2023-12-01 16:35:23 26 4
gpt4 key购买 nike

我正在开发 MVC Web 应用程序。将 Spring Boot 2.0.1 与 Spring Security 结合使用。当尝试访问静态资源时,我收到错误 404。

enter image description here

我尝试过不同的事情,我读过很多主题,但找不到任何解决方案。

配置类:

@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/");
}
}

项目结构:

enter image description here

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

}

这一切有什么问题吗?

最佳答案

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

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

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

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

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

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

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

关于java - Spring MVC 应用程序中找不到静态资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61960336/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com