Spring Cloud - Zuul Proxy is producing a No 'Access-Control-Allow-Origin' ajax response

后端 未结 6 590
别跟我提以往
别跟我提以往 2021-02-04 05:28

Startup Appplication:

@SpringBootApplication
@EnableZuulProxy
public class ZuulServer {

     public static void main(String[] args) {
         new SpringApplica         


        
6条回答
  •  说谎
    说谎 (楼主)
    2021-02-04 06:05

    I had a similar problem, with Angular Web app consuming RESTful services implemented by Spring Boot with Zuul and Spring Security.

    None of the above solutions worked. I realized that the problem was NOT in Zuul, but in Spring Security.

    As the official documentation (CORS with Spring Security) states, when using Spring Security, CORS must be configured prior to Spring Security.

    Finally, I was able to integrate Grinish Nepal's (see prior answers) solution into a solution that works.

    Without further ado, here is the code that enables CORS with Spring Security and Zuul:

    
        @Configuration
        @EnableWebSecurity
        public class SecurityConfig extends WebSecurityConfigurerAdapter {
            //irrelevant for this problem
            @Autowired
            private MyBasicAuthenticationEntryPoint authenticationEntryPoint;
    
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http
                        //configure CORS -- uses a Bean by the name of     corsConfigurationSource (see method below)
                        //CORS must be configured prior to Spring Security
                        .cors().and()
                        //configuring security - irrelevant for this problem
                        .authorizeRequests()
                            .anyRequest().authenticated()
                            .and()
                        .httpBasic()
                        .authenticationEntryPoint(authenticationEntryPoint);
    
                //irrelevant for this problem
                http.addFilterAfter(new CustomFilter(),
                        BasicAuthenticationFilter.class);
            }
    
            //The CORS filter bean - Configures allowed CORS any (source) to any 
            //(api route and method) endpoint
            @Bean
            CorsConfigurationSource corsConfigurationSource() {
                final UrlBasedCorsConfigurationSource source = new     UrlBasedCorsConfigurationSource();
                final CorsConfiguration config = new CorsConfiguration();
                config.setAllowCredentials(true);
                config.addAllowedOrigin(CorsConfiguration.ALL);
                config.addAllowedHeaders(Collections.singletonList(CorsConfiguration.ALL));
                config.addAllowedMethod("OPTIONS");
                config.addAllowedMethod("HEAD");
                config.addAllowedMethod("GET");
                config.addAllowedMethod("PUT");
                config.addAllowedMethod("POST");
                config.addAllowedMethod("DELETE");
                config.addAllowedMethod("PATCH");
                source.registerCorsConfiguration("/**", config);
                return source;
            }
    
            //configuring BA usernames and passwords - irrelevant for this problem
            @Autowired
            public void configureGlobal(AuthenticationManagerBuilder auth) throws     Exception {
               ...
            }
        }
    

提交回复
热议问题