Spring 4/5 global CORS configuration doesn't work giving `No 'Access-Control-Allow-Origin' header is present on the requested resource`

前端 未结 1 1387
陌清茗
陌清茗 2021-01-03 15:57

I am trying to login through http://localhost:3000 using HTTP `POST.

I am getting the following:

XMLHttpRequest cannot load http

相关标签:
1条回答
  • 2021-01-03 16:24

    With help from dur . I spotted that http://github.com/svlada/springboot-security-jwt had added a WebSecurityConfig that was overriding my spring boot application configuration.

    In the configure section of WebSecurityConfig I added cors() before anything else as following:

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.cors()
            .and()
            .csrf().disable() // We don't need CSRF for JWT based authentication
            .exceptionHandling()
            .authenticationEntryPoint(this.authenticationEntryPoint)
    
            .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
    
            .and()
                .authorizeRequests()
                    .antMatchers(FORM_BASED_LOGIN_ENTRY_POINT).permitAll() // Login end-point
                    .antMatchers(TOKEN_REFRESH_ENTRY_POINT).permitAll() // Token refresh end-point
                    .antMatchers("/console").permitAll() // H2 Console Dash-board - only for testing
            .and()
                .authorizeRequests()
                    .antMatchers(TOKEN_BASED_AUTH_ENTRY_POINT).authenticated() // Protected API End-points
            .and()
                .addFilterBefore(buildAjaxLoginProcessingFilter(), UsernamePasswordAuthenticationFilter.class)
                .addFilterBefore(buildJwtTokenAuthenticationProcessingFilter(), UsernamePasswordAuthenticationFilter.class);
        }
    

    Then I was getting an error for the corsFilter. The Bean needs to be of type CorsFilter but for me it was FilterRegistrationBean.

    So I converted the Bean in my @SpringBootApplication class from

        @Bean
        public FilterRegistrationBean corsFilter() {
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            CorsConfiguration config = new CorsConfiguration();
            config.setAllowCredentials(true);
            config.addAllowedOrigin("http://localhost:3000");
            config.addAllowedHeader("*");
            config.addAllowedMethod("*");
            source.registerCorsConfiguration("/**", config);
            FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
            bean.setOrder(0);
            return bean;
        }
    

    to:

        @Bean
        public CorsFilter corsFilter() {
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            CorsConfiguration config = new CorsConfiguration();
            config.setAllowCredentials(true);
            config.addAllowedOrigin("http://localhost:3000");
            config.addAllowedHeader("*");
            config.addAllowedMethod("*");
            source.registerCorsConfiguration("/**", config);
            return new CorsFilter(source);
        }
    

    I also removed

      @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    .allowedOrigins("http://localhost:3000")
                    .allowedMethods("GET", "PUT", "POST", "DELETE","OPTIONS");
        }
    

    I might have to move those configurations to a single place - favorably removing WebSecurityConfig altogether

    0 讨论(0)
提交回复
热议问题