title: Spring how to add resource handlers for static files from other origin
body: <p>I'm trying to implement handling requests for static resourses in Spring + Angular project. Angular app is running on <code>http://localhost:4200</code> when Spring app is running on <code>http://localhost:8080</code>.</p>
  <p>So when I need to get an image, angular sends a request which looks like this <code>http://localhost:4200/screenShots/image-name.jpg</code>. To handle these kind of requests I've created <code>WebConfig</code> class:</p>
  <pre><code>@Configuration
  public class WebConfig implements WebMvcConfigurer {
  
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
          String path = &quot;file:///&quot; + *path to folder with images on my PC*
          registry.addResourceHandler(&quot;/screenShots/**&quot;)
                  .addResourceLocations(path);
      }
  }
  </code></pre>
  <p>And it doesn't work, I get <em>404 (Not Found)</em> error. I've experimented with addind and removing <code>@EnableWebMvc</code> and <code>@CrossOrigin</code> annotations, tried to add <code>addCorsMappings()</code> method in my class like that, nothing helped.</p>
  <pre><code>    @Override
      public void addCorsMappings(CorsRegistry registry) {
          registry.addMapping(&quot;/screenShots/**&quot;)
                  .allowedOrigins(&quot;http://localhost:4200&quot;);
      }
  </code></pre>
  <p>However when I put <code>http://localhost:8080/screenShots/image-name.jpg</code> <strong>(changed :4200 to :8080)</strong> string in Google address bar, it returns me my image as intended, so I wonder is there a way to add some kind of <code>@CrossOrigin</code> annotation alternative for resourse handlers cuz I haven't found any solution in the Internet yet. Thanks!</p>
  <p>Github repo for those, who are curious (<strong>only gameImagesRelease branch contains current piece of code</strong>): <a href="https://github.com/Vladyslav-Bahlai/OktenGames/tree/gameImagesRelease" rel="nofollow noreferrer">https://github.com/Vladyslav-Bahlai/OktenGames/tree/gameImagesRelease</a></p>