title: Spring Boot Oauth2, "Access-Control-Allow-Origin" Error (Server returns 401)
body: <p>i have a rest server with oauth2 implemented. POSTMAN works good but our angular2 client wont work because of the following reason on console:</p>
  
  <blockquote>
    <p>Response to preflight request doesn't pass access control check: No
    'Access-Control-Allow-Origin' header is present on the requested
    resource. Origin '<a href="http://localhost:4200" rel="nofollow noreferrer">http://localhost:4200</a>' is therefore not allowed
    access. The response had HTTP status code 401.</p>
  </blockquote>
  
  <p>Have been reading and trying on this problem for some hours long. We have our filter implemented as below:</p>
  
  <pre><code>@Component
  public class CORSFilter implements Filter {
  
      public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
          HttpServletResponse response = (HttpServletResponse) res;
  
          response.setHeader("Access-Control-Allow-Origin", "*");
          response.setHeader("Access-Control-Allow-Credentials", "true");
          response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
          response.setHeader("Access-Control-Max-Age", "3600");
          response.setHeader("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Authorization, Origin, Accept, Access-Control-Request-Method, Access-Control-Request-Headers");
  
  
          chain.doFilter(req, res);
      }
  
      public void init(FilterConfig filterConfig) {}
  
      public void destroy() {}
  
  }
  </code></pre>
  
  <p>And in the Initializer class here how we start it (also tried the commented override method)</p>
  
  <pre><code>public class ZiftInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  
      @Override
      protected Class&lt;?&gt;[] getRootConfigClasses() {
          return new Class[] { ZiftConfiguration.class };
      }
  
      @Override
      protected Class&lt;?&gt;[] getServletConfigClasses() {
          return null;
      }
  
      @Override
      protected String[] getServletMappings() {
          return new String[] { "/" };
      }
  
      /*
      @Override
      public void onStartup(ServletContext servletContext) throws ServletException {
          registerServletFilter(servletContext, new CORSFilter());
          super.onStartup(servletContext);
      }*/
  
  
      @Override
      protected Filter[] getServletFilters() {
          Filter [] singleton = { new CORSFilter()};
          return singleton;
      }
  }
  </code></pre>
  
  <p>And lastly, here is our class that extends WebSecurityConfigurerAdapter</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  public class OAuth2SecurityConfiguration extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private DataSource dataSource;
      @Autowired
      private ClientDetailsService clientDetailsService;
  
      @Autowired
      public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
  
          auth.jdbcAuthentication().dataSource(dataSource).usersByUsernameQuery("select username,password, enabled from User where username=?")
          .authoritiesByUsernameQuery("select username, role from User_Roles where username=?");
  
  
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http.csrf().disable().anonymous().disable().authorizeRequests().antMatchers("/oauth/token").permitAll();
          http.authorizeRequests().antMatchers("/oauth/token/").permitAll();
      }
  
      @Override
      @Bean
      public AuthenticationManager authenticationManagerBean() throws Exception {
          return super.authenticationManagerBean();
      }
  
      @Bean
      public TokenStore tokenStore() {
          return new JdbcTokenStore(dataSource);
      }
  
      @Bean
      @Autowired
      public TokenStoreUserApprovalHandler userApprovalHandler(TokenStore tokenStore) {
          TokenStoreUserApprovalHandler handler = new TokenStoreUserApprovalHandler();
          handler.setTokenStore(tokenStore);
          handler.setRequestFactory(new DefaultOAuth2RequestFactory(clientDetailsService));
          handler.setClientDetailsService(clientDetailsService);
  
          return handler;
      }
  
      @Bean
      @Autowired
      public ApprovalStore approvalStore(TokenStore tokenStore) throws Exception {
          TokenApprovalStore store = new TokenApprovalStore();
          store.setTokenStore(tokenStore);
  
          return store; 
      }
  
  }
  </code></pre>
  
  <p>What might be the problem? Thank you all even for reading.</p>