title: trouble connecting Angular 10 with Spring security to use custom login page
body: <p>I have been working on a web application using Spring boot and spring security with frontend controlled by angular 10. I have implemented backend for security and created a login page also. But, on running on local host it is throwing an error</p>
  <p><strong>blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.</strong></p>
  <p>I have been banging my head all day long to resolve this error but could not find the solution.
  I have attached my code below for reference</p>
  <p><strong>Controller</strong></p>
  <pre><code>@RestController
  @RequestMapping(&quot;/user&quot;)
  public class UserController {
  
      @Autowired
      AuthenticationManager authenticationManager;
  
      @PostMapping(&quot;/login&quot;)
      public boolean login(@RequestBody loginDetails data) {
          try {
              String username = data.getUsername();
              System.out.println(&quot;Checking...&quot;);
              System.out.println(data.getUsername());
              System.out.println(data.getPassword());
              authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, data.getPassword()));
              // String token = jwtTokenProvider.createToken(username,
              // this.users.findByEmail(username).getRoles());
              System.out.println(&quot;abcdefg&quot;);
              Map&lt;Object, Object&gt; model = new HashMap&lt;&gt;();
              model.put(&quot;username&quot;, username);
              // model.put(&quot;token&quot;, token);
              /* return true; */
          } catch (AuthenticationException e) {
              /*
               * throw new BadCredentialsException(&quot;Invalid email/password supplied&quot;);
               */
              return false;
          }
          return true;
      }
  </code></pre>
  <p><strong>WebSecurityConfiguration</strong></p>
  <pre><code>@EnableWebSecurity
  @Configuration
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Qualifier(&quot;userDetailsService&quot;)
      @Autowired
      private UserDetailsService userDetailsService;
  
      @Bean
      public BCryptPasswordEncoder bCryptPasswordEncoder() {
  
          return new BCryptPasswordEncoder();
      }
  
      
      
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http.authorizeRequests().antMatchers(&quot;/user/save&quot;,&quot;/user/login&quot;,    
                  &quot;/admin/**&quot;).permitAll().anyRequest().authenticated().and().csrf()
                  .disable().formLogin().permitAll().and().logout().permitAll();
          http.cors();
      }
       
      @Bean
      public AuthenticationManager customAuthenticationManager() throws Exception {
          return authenticationManager();
      }
  
      @Autowired
      public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
          auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
      }
  
  }
  </code></pre>
  <p><strong>AngularRequestcode</strong></p>
  <pre><code>public doLogin(){
  
        this.userLogin.username=this.loginForm.get(&quot;username&quot;).value;
        this.userLogin.password=this.loginForm.get(&quot;password&quot;).value;
        console.log(this.userLogin);
      return this.http.post&lt;any&gt;(&quot;http://localhost:8080/user/login&quot;,this.userLogin).subscribe((response) =&gt; {
      
                if (response.status === 200) {
                    
                  console.log('login successfully');
      
                } else {
      
                  console.log('galat');
      
                }
    
              }
          );
    }
  </code></pre>