2015-02-23 65 views
7

启动应用项目:春云 - Zuul代理是生产无“访问控制允许来源” Ajax响应

@SpringBootApplication 
@EnableZuulProxy 
public class ZuulServer { 

    public static void main(String[] args) { 
     new SpringApplicationBuilder(ZuulServer.class).web(true).run(args); 
    } 
} 

我YAML文件是这样的:

server: 
    port:8080 

spring: 
    application: 
     name: zuul 

eureka: 
client: 
    enabled: true 
    serviceUrl: 
     defaultZone: http://localhost:8761/eureka/ 



zuul: 
    proxy: 
     route: 
     springapp: /springapp 

我有一个微服务应用程序(在端口8081上)称为springapp并有一些休息服务。下面是我的客户端UI应用程序:

<html> 
    <head> 
     <title>TODO supply a title</title> 
     <meta charset="UTF-8"> 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
     <script type="text/javascript" src="js/libs/jquery/jquery.min.js" ></script> 
    </head> 
    <body> 
     <script type="text/javascript"> 
      $.ajax({ 
       url: 'http://localhost:8080/zuul/springapp/departments', 
       type: 'GET' 
      }).done(function (data) { 
       consoe.log(data); 
       document.write(data); 
      }); 
     </script>   

    </body> 
</html> 

但我得到一个

XMLHttpRequest cannot load http://localhost:8080/zuul/springapp/departments. No 
    'Access-Control-Allow-Origin' header is present on the requested 
    resource. Origin 'http://localhost:8383' is therefore not allowed access. 

这个UI HTML5应用是http://localhost:8383/SimpleAPp/index.html。 CORS,CORS,CORS ...请帮助。顺便说一句,http://localhost:8080/zuul/springapp/departments返回一个json列表,假设在浏览器地址栏上。 spring.io博客here表示不需要过滤器,因为zuulproxy处理这个问题,但我不知道为什么它不适合我。

回答

20

将这段代码添加到使用@EnableZuulProxy注释的类中应该可以做到。

@Bean 
public CorsFilter corsFilter() { 
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); 
    final CorsConfiguration config = new CorsConfiguration(); 
    config.setAllowCredentials(true); 
    config.addAllowedOrigin("*"); 
    config.addAllowedHeader("*"); 
    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 new CorsFilter(source); 
} 
1

这只是浏览器告诉你,你违反了它的共同来源政策(请参阅Wikipedia entry和互联网上的大量资料,其中没有一个与您添加的标签真正相关)。您可以教浏览器,通过服务CORS飞行前检查(例如,在Filter中)或通过代理加载HTML来从不同地址加载资源是正常的(提示:后者更容易,更少出错) )。

+0

感谢试图回答,但我加了标签的原因是因为来自Spring云的@EnableZuulProxy。在spring.io的这篇文章[博客](http:// spring。io/blog/2015/01/28/the-api-gateway-pattern-angular-js-and-spring-security-part-iv)他​​们说没有必要使用你提到的Filter,因为ZuulProxy做了一个消费客户端的反向代理。希望这是有道理的。 – 2015-02-23 14:00:03

+1

是的,但正如下面的@zeroflagL所说,这意味着您必须通过代理发送所有请求。 – 2015-02-23 15:21:24

5

当您的应用程序在http://localhost:8383上运行时,您只能进行AJAX调用http://localhost:8383。祖鲁没有也不能改变这种状况。

Zuul能做的是映射例如http://localhost:8383/zuul/http://localhost:8080/zuul/。但是您的浏览器必须调用http://localhost:8383/zuul/springapp/departments,您必须配置该映射。

+0

非常感谢。这对我有用:)现在我的另一个问题是:如果你有一个角度js应用程序包装在Cordova/Phonegap中,并在应用程序中使用静态文件而不是从代理服务器提供文件,你会怎么做?我问,因为我的解决方案截至目前仅适用,因为带有ajax请求的静态文件驻留在ZuulProxy服务器上。 – 2015-02-23 15:53:24

+0

我有一个针对上面的angular.js场景的解决方案。我添加了一个CORS过滤器,如[这里](https://spring.io/guides/gs/rest-service-cors/)给ZuulProxy所示。因此,任何从与代理相同的域或从其他源进入ZuulProxy的请求都将被允许:) – 2015-02-23 16:40:31

+0

如果我假设正确,[CORS Filter](https://spring.io/guides/gs/rest- service-cors /)只适用于应用程序托管在根(/)上下文中。如果应用程序托管在任何其他上下文中,则需要回退以启用CORS特定于该servlet容器。 – Stackee007 2015-03-06 15:18:35

0

我有一个类似的问题,Angular Web应用程序使用Spring Boot与Zuul和Spring Security实现的RESTful服务。

以上都不是解决方案。我意识到问题不在Zuul,而是在Spring Security。

由于官方文档(CORS with Spring Security)指出,在使用Spring Security时,必须在Spring Security之前配置CORS。

最后,我能够将Grinish尼泊尔的(见前答案)解决方案整合到一个可行的解决方案中。

事不宜迟,这里是使CORS使用Spring Security和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("*"); 
     config.addAllowedHeader("*"); 
     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 { 
     ... 
    } 
} 

```

+0

您是否在具有zuul的应用程序或具有微服务的应用程序中添加此类? – CuriousCoder 2017-12-01 14:25:00

相关问题