简体   繁体   English

Spring Data REST (2.4.4.RELEASE) 和 CORS

[英]Spring Data REST (2.4.4.RELEASE) and CORS

I'm trying to add CORS support to Access JPA Data with REST gs-accessing-data-rest-complete by following Sebastien Deleuze recommendations in Spring Data Rest and Cors :我正在尝试按照Spring Data Rest 和 Cors 中的Sebastien Deleuze 建议添加 CORS 支持以使用 REST gs-accessing-data-rest-complete访问 JPA 数据

@Configuration
public class ApplicationConfiguration {
  @Bean
  public CorsFilter corsFilter() {

    UrlBasedCorsConfigurationSource source = 
      new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true); // you USUALLY want this
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");
    config.addAllowedMethod("GET");
    config.addAllowedMethod("PUT");
    source.registerCorsConfiguration("/**", config);
    return new CorsFilter(source);
  }
}

or, alternatively:或者,或者:

@Configuration
public class ApplicationConfiguration {
  @Bean
  public FilterRegistrationBean corsFilter() {
    UrlBasedCorsConfigurationSource source = 
        new UrlBasedCorsConfigurationSource();
    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);
    final FilterRegistrationBean bean = new FilterRegistrationBean(
        new CorsFilter(source)
    );
    bean.setOrder(0);
    return bean;
  }
}

but both configurations result in the same response that does not include the Access-Control-Allow-Origin header:但两种配置都会产生相同的响应,但不包含Access-Control-Allow-Origin标头:

~> curl -v http://localhost:8080/people
* Connected to localhost (::1) port 8080 (#0)
> GET /people HTTP/1.1
> Host: localhost:8080
> Accept: */*
< HTTP/1.1 200 OK
< Server: Apache-Coyote/1.1
< Content-Type: application/hal+json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Fri, 22 Apr 2016 21:29:26 GMT
{
  "_embedded" : {
    "people" : [ {
      "firstName" : "Frodo",
      "lastName" : "Baggins",
      "_links" : { "self" : {
          "href" : "http://localhost:8080/people/1"
        }, "person" : {
          "href" : "http://localhost:8080/people/1"
        } } } ]     // for readability
  }, "_links" : {
    "self" : {
      "href" : "http://localhost:8080/people"
    }, "profile" : {
      "href" : "http://localhost:8080/profile/people"
    }, "search" : {
      "href" : "http://localhost:8080/people/search"
    }
  }, "page" : {
    "size" : 20,
    "totalElements" : 1,
    "totalPages" : 1,
    "number" : 0
  }
* Connection #0 to host localhost left intact

The versions of the dependencies are:依赖项的版本是:

~/gs-accessing-data-rest-complete> mvn dependency:tree
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building gs-accessing-data-rest 0.1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ gs-accessing-data-rest ---
[INFO] org.springframework:gs-accessing-data-rest:jar:0.1.0
[INFO] +- org.springframework.boot:spring-boot-starter-data-rest:jar:1.3.3.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:1.3.3.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot:jar:1.3.3.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:1.3.3.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:1.3.3.RELEASE:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.1.5:compile
[INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.1.5:compile
[INFO] |  |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.16:compile
[INFO] |  |  |  \- org.slf4j:log4j-over-slf4j:jar:1.7.16:compile
[INFO] |  |  +- org.springframework:spring-core:jar:4.2.5.RELEASE:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.16:runtime
[INFO] |  +- org.springframework.boot:spring-boot-starter-web:jar:1.3.3.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.3.3.RELEASE:compile
[INFO] |  |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.0.32:compile
[INFO] |  |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.0.32:compile
[INFO] |  |  |  +- org.apache.tomcat.embed:tomcat-embed-logging-juli:jar:8.0.32:compile
[INFO] |  |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.0.32:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-validation:jar:1.3.3.RELEASE:compile
[INFO] |  |  |  \- org.hibernate:hibernate-validator:jar:5.2.4.Final:compile
[INFO] |  |  |     +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] |  |  |     \- com.fasterxml:classmate:jar:1.1.0:compile
[INFO] |  |  +- org.springframework:spring-web:jar:4.2.5.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-webmvc:jar:4.2.5.RELEASE:compile
[INFO] |  |     \- org.springframework:spring-expression:jar:4.2.5.RELEASE:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.6.5:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.6.5:compile
[INFO] |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.6.5:compile
[INFO] |  \- org.springframework.data:spring-data-rest-webmvc:jar:2.4.4.RELEASE:compile
[INFO] |     +- org.springframework.data:spring-data-rest-core:jar:2.4.4.RELEASE:compile
[INFO] |     |  +- org.springframework.hateoas:spring-hateoas:jar:0.19.0.RELEASE:compile
[INFO] |     |  +- org.springframework.plugin:spring-plugin-core:jar:1.2.0.RELEASE:compile
[INFO] |     |  \- org.atteo:evo-inflector:jar:1.2.1:compile
[INFO] |     +- com.github.fge:json-patch:jar:1.7:compile
[INFO] |     |  +- com.github.fge:jackson-coreutils:jar:1.6:compile
[INFO] |     |  |  +- com.github.fge:msg-simple:jar:1.1:compile
[INFO] |     |  |  |  \- com.github.fge:btf:jar:1.2:compile
[INFO] |     |  |  \- com.google.guava:guava:jar:16.0.1:compile
[INFO] |     |  \- com.google.code.findbugs:jsr305:jar:2.0.1:compile
[INFO] |     +- org.slf4j:slf4j-api:jar:1.7.16:compile
[INFO] |     \- org.slf4j:jcl-over-slf4j:jar:1.7.16:compile
[INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:1.3.3.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-aop:jar:1.3.3.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-aop:jar:4.2.5.RELEASE:compile
[INFO] |  |  |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  |  \- org.aspectj:aspectjweaver:jar:1.8.8:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.3.3.RELEASE:compile
[INFO] |  |  +- org.apache.tomcat:tomcat-jdbc:jar:8.0.32:compile
[INFO] |  |  |  \- org.apache.tomcat:tomcat-juli:jar:8.0.32:compile
[INFO] |  |  \- org.springframework:spring-jdbc:jar:4.2.5.RELEASE:compile
[INFO] |  +- org.hibernate:hibernate-entitymanager:jar:4.3.11.Final:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile
[INFO] |  |  +- org.hibernate:hibernate-core:jar:4.3.11.Final:compile
[INFO] |  |  |  +- antlr:antlr:jar:2.7.7:compile
[INFO] |  |  |  \- org.jboss:jandex:jar:1.1.0.Final:compile
[INFO] |  |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  |  |  \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] |  |  +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.5.Final:compile
[INFO] |  |  +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[INFO] |  |  \- org.javassist:javassist:jar:3.18.1-GA:compile
[INFO] |  +- javax.transaction:javax.transaction-api:jar:1.2:compile
[INFO] |  +- org.springframework.data:spring-data-jpa:jar:1.9.4.RELEASE:compile
[INFO] |  |  +- org.springframework.data:spring-data-commons:jar:1.11.4.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-orm:jar:4.2.5.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-context:jar:4.2.5.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-tx:jar:4.2.5.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-beans:jar:4.2.5.RELEASE:compile
[INFO] |  \- org.springframework:spring-aspects:jar:4.2.5.RELEASE:compile
[INFO] \- com.h2database:h2:jar:1.4.191:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.525 s
[INFO] Finished at: 2016-04-22T15:42:32-06:00
[INFO] Final Memory: 21M/609M
[INFO] ------------------------------------------------------------------------

Any ideas what I'm doing wrong?任何想法我做错了什么?

Both configurations are correct.两种配置都是正确的。 You will see the Access-Control* headers in the response only if it is a CORS request;只有当它是 CORS 请求时,您才会在响应中看到Access-Control*标头; try this using curl :使用curl试试这个:

curl -H "Origin: http://someotherorigin.com" -v http://localhost:8080/people

Here's the output:这是输出:

~> curl -H "Origin: http://someotherorigin.com" -v http://localhost:8080/people/1
*   Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> GET /people/1 HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.42.1
> Accept: */*
> Origin: http://someotherorigin.com
> 
< HTTP/1.1 404 Not Found
< Server: Apache-Coyote/1.1
< Access-Control-Allow-Origin: http://localhost:9001
< Vary: Origin
< Access-Control-Allow-Credentials: true
< Content-Length: 0
< Date: Sat, 23 Apr 2016 13:52:03 GMT
< 
* Connection #0 to host localhost left intact

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Data Rest 和 Cors - Spring Data Rest and Cors Spring 引导中的 CORS 问题 + Spring 数据 Z55276C10D84E1DF7713B441E76E14199 重新存储库 - CORS issue in Spring Boot + Spring Data Rest Repositories annotated with @CrossOrigin 未为 OPTIONS/DELETE 正确启用 Spring Boot Data Rest + CORS - Spring Boot Data Rest + CORS not being enabled properly for OPTIONS/DELETE Spring 启动 (2.2.0.RELEASE&lt;) 与 spring 数据 rest 端点未暴露 - Spring boot (2.2.0.RELEASE<) with spring data rest endpoint not exposed 多部分表单数据在 spring 4.3.5 RELEASE rest api 中不起作用 - Multipart form data is not working in spring 4.3.5 RELEASE rest api Spring API REST 和 Cors 以及 AngularJS - Spring API REST and Cors and AngularJS 是否可以在 spring boot 2.4.4 rest java 11 应用程序中在 rest 控制器和控制器建议之间进行映射? - Is it possible to have mapping between rest controller and controller advice in spring boot 2.4.4 rest java 11 application? CORS 使用 Spring boot REST API 的问题 - Problem with CORS consuming Spring boot REST API 如何使用Spring REST在跨源资源共享(CORS)中接收JSON数据 - How To Receive JSON Data In Cross Origin Resource Sharing (CORS) with Spring REST Spring Data Rest 2.0.0.RELEASE以前使用RC1打破代码工作 - Spring Data Rest 2.0.0.RELEASE Breaks Code Working Previously With RC1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM