I'm working on MVC web app. Using Spring Boot 2.0.1 with Spring Security. And I get error 404 when try reaching static resources.
I've tried diefferent things, I've read many topics, but can't find any solution.
Configuretion 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/");
}
}
Project strucutre:
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>
Security settings:
@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);
}
What's wrong with all of this?
As per project structure, all your resources will be copied to static
directory under your classpath and there won't be any location like resources
. Hence, it would not be able to resolve.
resource location should be specified along with classpath
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/");
for safer side, you can class multiple location lookup as well like this
.addResourceLocations(new String[]{"classpath:/static/", "/"});
Spring includes these by default unless overridden
["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]