【发布时间】: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);
}
这一切有什么问题?
【问题讨论】:
-
你已经看到了吗? - stackoverflow.com/questions/24916894/…
标签: java spring-mvc spring-security staticresource