title: CORS blocked Angular 7 and Spring 5
body: <p>I'm running a Spring 5 with Spring Security and Angular 7 project and trying to wire the frontend but keep getting this err message. I should note the projects are two different directories on my computer</p>
  
  <p>OS backend > spring</p>
  
  <p>OS frontend > angular</p>
  
  <pre><code>Access to XMLHttpRequest at 'http://localhost:8080/login' from origin 'http://localhost:4200' 
  has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the
  requested resource.
  </code></pre>
  
  <p>I've gone through a bunch of threads here on stackoverflow but none would help.</p>
  
  <p>From what i've gathered the issue is my spring security config</p>
  
  <p><strong>SPRING SECURITY FILE</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>package com.starter_kit.auth;
  
  import com.starter_kit.auth.Auth.CustomizeAuthenticationSuccessHandler;
  import com.starter_kit.auth.Company.CompanyRepo;
  import com.starter_kit.auth.Users.UserRepo;
  import com.starter_kit.auth.Users.UserService;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  import org.springframework.security.core.userdetails.UserDetailsService;
  import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
  import org.springframework.web.cors.CorsConfiguration;
  import org.springframework.web.cors.CorsConfigurationSource;
  import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
  
  import java.util.Arrays;
  
  @Configuration
  @EnableWebSecurity
  public class AppSecurityConfig extends WebSecurityConfigurerAdapter {
  
      // code
  
      @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          UserDetailsService userDetailsService = mongoUserDetails();
          auth
                  .userDetailsService(userDetailsService)
                  .passwordEncoder(bCryptPasswordEncoder);
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
                  .authorizeRequests()
                  .antMatchers("/").permitAll()
                  .antMatchers("/login").permitAll()
                  .antMatchers("/register").permitAll()
                  .antMatchers("/dashboard/").hasAuthority("ADMIN").anyRequest()
                  .authenticated().and().csrf().disable().formLogin().successHandler(customizeAuthenticationSuccessHandler)
                  .loginPage("/login").failureUrl("/login?error=true")
                  .usernameParameter("email")
                  .passwordParameter("password")
                  .and().logout()
                  .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                  .logoutSuccessUrl("/").and().exceptionHandling();
      }
  
      @Bean
      CorsConfigurationSource corsConfigurationSource() {
          CorsConfiguration configuration = new CorsConfiguration();
          configuration.setAllowedOrigins(Arrays.asList("/**"));
          configuration.setAllowedMethods(Arrays.asList("GET","POST","DELETE","PUT"));
          UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
          source.registerCorsConfiguration("/**", configuration);
          return source;
      }
  
  }
  
  </code></pre>
  
  <p>Spring Controller</p>
  
  <pre class="lang-java prettyprint-override"><code>@RestController
  @CrossOrigin(origins = "http://localhost:4200")
  @RequestMapping("/")
  public class LoginController {
      @Autowired
      private UserService userService;
  
      @PostMapping(path = "/login", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
      public UserDetails login(@RequestBody User user) {
          return userService.loadUserByUsername(user.getEmail());
      }
  }
  </code></pre>
  
  <p>and my ANGULAR TS HTTP CALL</p>
  
  <pre><code>private loginAuth: string = "http://localhost:8080/login";
    public headers = new HttpHeaders({ "Access-Control-Allow-Credentials": "true" })
  
    public loginUser(user: any) {
      return this.http.post(
        this.loginAuth,
        user,
        { headers: this.headers }
      );
    }
  </code></pre>
  
  <p>any help would be great</p>