Spring how to add resource handlers for static files from other origin

Viewed 1964

I'm trying to implement handling requests for static resourses in Spring + Angular project. Angular app is running on http://localhost:4200 when Spring app is running on http://localhost:8080.

So when I need to get an image, angular sends a request which looks like this http://localhost:4200/screenShots/image-name.jpg. To handle these kind of requests I've created WebConfig class:

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String path = "file:///" + *path to folder with images on my PC*
        registry.addResourceHandler("/screenShots/**")
                .addResourceLocations(path);
    }
}

And it doesn't work, I get 404 (Not Found) error. I've experimented with addind and removing @EnableWebMvc and @CrossOrigin annotations, tried to add addCorsMappings() method in my class like that, nothing helped.

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/screenShots/**")
                .allowedOrigins("http://localhost:4200");
    }

However when I put http://localhost:8080/screenShots/image-name.jpg (changed :4200 to :8080) string in Google address bar, it returns me my image as intended, so I wonder is there a way to add some kind of @CrossOrigin annotation alternative for resourse handlers cuz I haven't found any solution in the Internet yet. Thanks!

Github repo for those, who are curious (only gameImagesRelease branch contains current piece of code): https://github.com/Vladyslav-Bahlai/OktenGames/tree/gameImagesRelease

1 Answers

For example, I will show my MVC-configs. I had the same problem. Spring did not see HTML files. I draw your attention to the templates folder.

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    /**
     * Making our pages visible
     *
     * @param registry
     */
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("login");
        registry.addViewController("/coffees").setViewName("coffees");
        registry.addViewController("/teas").setViewName("teas");
        registry.addViewController("/contacts").setViewName("contacts");
        registry.addViewController("/home").setViewName("home");
        registry.addViewController("/about").setViewName("about");
        registry.addViewController("/registration").setViewName("registration");
        registry.addViewController("/card").setViewName("card");
    }


    /**
     * This method helps Spring discover file paths
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {

        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/templates/");
    }
}
Related