1

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.

enter image description here

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:

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>

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?

Svidic
  • 13
  • 1
  • 4
  • Did you see this already ? - https://stackoverflow.com/questions/24916894/serving-static-web-resources-in-spring-boot-spring-security-application – Mukesh Keshu May 22 '20 at 17:32

1 Answers1

0

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/", "/"]
lucid
  • 2,722
  • 1
  • 12
  • 24
  • I've already tried specifying {registry.addResourceHandler("/static/**") .addResourceLocations("/static/");} and bunch of other options in this method. I tried without any ResourceHandler as well. Does't work. I think the issue is somewhere in Security, but can't find it... – Svidic May 22 '20 at 18:15