title: CORS issue - No 'Access-Control-Allow-Origin' header is present on the requested resource
body: <p>I have created two web applications - client and service apps.<br>The interaction between client and service apps goes fine when they are deployed in same Tomcat instance.<br>
  But when the apps are deployed into seperate Tomcat instances (different machines), I get the below error when request to sent service app.</p>
  
  <pre class="lang-none prettyprint-override"><code>Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 
  Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 401
  </code></pre>
  
  <p>My Client application uses JQuery, HTML5 and Bootstrap.</p>
  
  <p>AJAX call is made to service as shown below:</p>
  
  <pre><code>var auth = "Basic " + btoa({usname} + ":" + {password});
  var service_url = {serviceAppDomainName}/services;
  
  if($("#registrationForm").valid()){
      var formData = JSON.stringify(getFormData(registrationForm));
      $.ajax({
          url: service_url+action,
          dataType: 'json',
          async: false,
          type: 'POST',
          headers:{
              "Authorization":auth
          },
          contentType: 'application/json',
          data: formData,
          success: function(data){
              //success code
          },
          error: function( jqXhr, textStatus, errorThrown ){
              alert( errorThrown );
          });
  }
  </code></pre>
  
  <p>My service application uses Spring MVC, Spring Data JPA and Spring Security.</p>
  
  <p>I have included <code>CorsConfiguration</code> class as shown below:</p>
  
  <p><code>CORSConfig.java</code>:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Configuration
  @EnableWebMvc
  public class CORSConfig extends WebMvcConfigurerAdapter  {
      @Override
      public void addCorsMappings(CorsRegistry registry) {
          registry.addMapping("*");
      }
  }
  </code></pre>
  
  <p><code>SecurityConfig.java</code>:</p>
  
  <pre><code>@Configuration
  @EnableGlobalMethodSecurity(prePostEnabled = true)
  @EnableWebSecurity
  @ComponentScan(basePackages = "com.services", scopedProxy = ScopedProxyMode.INTERFACES)
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      @Qualifier("authenticationService")
      private UserDetailsService userDetailsService;
  
      @Bean
      @Override
      public AuthenticationManager authenticationManagerBean() throws Exception {
          return super.authenticationManagerBean();
      }
  
      @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth.userDetailsService(userDetailsService);
          auth.authenticationProvider(authenticationProvider());
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
         http
                  .authorizeRequests()
                  .antMatchers("/login").permitAll()
                  .anyRequest().fullyAuthenticated();
          http.httpBasic();
          http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
          http.csrf().disable();
      }
  
      @Bean
      public PasswordEncoder passwordEncoder() {
          return new BCryptPasswordEncoder();
      }
  
      @Bean
      public DaoAuthenticationProvider authenticationProvider() {
          DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
          authenticationProvider.setUserDetailsService(userDetailsService);
          authenticationProvider.setPasswordEncoder(passwordEncoder());
          return authenticationProvider;
      }
  }
  </code></pre>
  
  <p>Spring Security dependencies:</p>
  
  <pre class="lang-xml prettyprint-override"><code> &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-config&lt;/artifactId&gt;
              &lt;version&gt;3.2.3.RELEASE&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-web&lt;/artifactId&gt;
              &lt;version&gt;3.2.3.RELEASE&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>I am using <strong>Apache Tomcat</strong> server for deployment.</p>