title: cors issue when throwing a runtime exception with spring and docker
body: <p>I' building a web app using spring, angular and docker. I also have for the moment two environments. A dev environment where angular is served using <code>npm start</code> and spring using <code>mvn spring-boot:run</code>. And a test environment where I use digitalocean to host my app and containerize the angular and spring apps using docker. </p>
  
  <p>In this application I have a registration page where user can register and a spring controller to handle registration request. Here is the code for my controller :</p>
  
  <pre><code>@PostMapping("/register")
  public SaveUserDto register(@RequestBody @Valid SaveUserDto saveUserDto, WebRequest request) {
  
          User userToSave = this.modelMapper.map(saveUserDto, User.class);
          User registeredUser = null;
          try {
              registeredUser = this.registrationService.register(userToSave);
          } catch (DataIntegrityViolationException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
              if(e.getRootCause().toString().contains("uk_pfki8mu6sefesty9h30d4n3yv")) {
                  throw new EmailAlreadyExistException();
              }
          }
          try {
              eventPublisher.publishEvent(new OnRegistrationCompleteEvent(registeredUser, request.getLocale(), request.getHeader("Origin")));
          } catch (Exception me) {
              System.out.println(me.getMessage());
          }
  
          if(registeredUser != null) {
              return this.modelMapper.map(registeredUser, SaveUserDto.class);
          } else {
              return null;
          }
  }
  </code></pre>
  
  <p>Since my angular is served from host:4200 (where host is either localhsot in my dev environment, or 165.22.72.253 in my test environment) and my api is served from host:8761 I have a cors configuration to enable cors request. Here is the configuration :</p>
  
  <pre><code>@Bean
  public CorsFilter corsFilter() {
          UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
          CorsConfiguration config = new CorsConfiguration();
          config.setAllowCredentials(true);
          config.addAllowedOrigin("http://" + clientHost + ":4200");
          config.addAllowedOrigin("http://" + clientHost + ":80");
          config.addAllowedHeader("*");
          config.addAllowedMethod("OPTIONS");
          config.addAllowedMethod("GET");
          config.addAllowedMethod("POST");
          config.addAllowedMethod("PUT");
          config.addAllowedMethod("DELETE");
          source.registerCorsConfiguration("/**", config);
          return new CorsFilter(source);
  }
  </code></pre>
  
  <p>Here clientHost is a parameter with either value is localhost when running in dev environment or 165.22.72.253 for my test environment. For most use case this setting is working but it fails in one particular occasion.</p>
  
  <p>The problem arise when a user try to register whith a existing email adress. Then as you can see from the controller method I throw an EmailAlreadyExistException. In dev env everything is working great that is the request return an error code 500 with a message "Email already exist". But in the test environment I get <code>Access to XMLHttpRequest at 'http://165.22.72.253:8762/register' from origin 'http://165.22.72.253:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.</code>.</p>
  
  <p>If useful  here is my docker-compose file :</p>
  
  <pre><code>version: "3.5"
  
  services:
  
    tomcat-file:
      container_name: tomcat-file-server
      image: tomcat-file-server
      build:
        context: .
        dockerfile: tomcat-file.prod.dockerfile
      volumes:
        - /home/vetter_leo/medias:/usr/local/tomcat/src
      ports:
        - "8205:8205"
  
  networks:
    default:
      external:
        name: helenos-network
  </code></pre>
  
  <p>At this point I don't understand why it's working in dev env and not in test env. Any help will be greatly appreciated. Thanks in advance.</p>