1

I couldn't access the image under the resources/static folder. I'm using Spring Boot version 2.112.

I also tried adding spring.resources.static-locations in the properties file but still can't access the .jpg file from the resources/static/image folder. Any reason why?

@Configuration
public class WebConfiguration extends WebMvcConfigurationSupport {
    
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
        .addResourceLocations("classpath:/static/", "classpath:/image/");
    }
İsmail Y.
  • 3,579
  • 5
  • 21
  • 29
Sam
  • 43
  • 5

1 Answers1

0

Well, you may try https://stackoverflow.com/a/66361120/15215155. Most of time is about spring boot default security configuration.

Edit: Little edit here since it would be too much for comments.

Tackling the problem from another angle. Is there any specific feature you need from WebMvcConfigurationSupport?? If not, why not trying WebSecurityConfigurer for the resources handlers and WebSecurityConfigurerAdapter for the security config?.

@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**", "/js/**")
                .addResourceLocations("classpath:/static/", "classpath:/resources/static/");
        registry.addResourceHandler("/images/**").addResourceLocations("classpath:/static/images/");
    }
}


@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
   
 @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
        .antMatchers("/resources/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
        .antMatchers("/css/**", "/js/**", "/image/**"").permitAll()
    }
}
  • I added this line based on the spring.io blog http.authorizeRequests() .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll(); to allow access to common static resource locations but I still can't access it. – Sam Mar 10 '21 at 20:31