【问题标题】:CORS issue: 'Access-Control-Allow-Origin' header is not presentCORS 问题:“Access-Control-Allow-Origin”标头不存在
【发布时间】:2017-12-11 07:19:24
【问题描述】:

我正在使用 jhipster V4.5.6 开发一个 Spring Boot 应用程序。但无法配置 CORS。

这是我的 application-dev.yml 文件:

 # CORS is only enabled by default with the "dev" profile, so BrowserSync can access the API
cors:
    allowed-origins: "http://localhost:9000"
    allowed-methods: GET, PUT, POST, DELETE, OPTIONS
    allowed-headers: "*"
    exposed-headers:
    allow-credentials: true
    max-age: 1800

WebConfigurer.java 如下:

@Bean
public CorsFilter corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = jHipsterProperties.getCors();
    if (config.getAllowedOrigins() != null && !config.getAllowedOrigins().isEmpty()) {
        log.debug("Registering CORS filter");
        source.registerCorsConfiguration("/api/**", config);
        source.registerCorsConfiguration("/v2/api-docs", config);
    }
    return new CorsFilter(source);
}

而SecurityConfiguration.java文件如下:

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring()
        .antMatchers(HttpMethod.OPTIONS, "/**")
        .antMatchers("/test/**");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .csrf()
        .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
    .and()
        .addFilterBefore(corsFilter, UsernamePasswordAuthenticationFilter.class)
        .exceptionHandling()
        .authenticationEntryPoint(http401UnauthorizedEntryPoint())
    .and()
        .authorizeRequests()
        ... //Some project specific configuration
}

现在,我可以处理 GET 请求了。但是当我如下使用 POST 时:

private demoCors(restUrl: string, input: any): Observable<Result> {
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });
    return this.http.post(restUrl, JSON.stringify(input), options)
    .map(this.extractData)
    .catch(this.handleError);
}

我收到以下错误:

POST http://localhost:8080/api/dth 403 (Forbidden)

XMLHttpRequest 无法加载 http://localhost:8080/api/dth。不 'Access-Control-Allow-Origin' 标头出现在请求的 资源。因此不允许使用原点“http://localhost:9000” 使用权。响应的 HTTP 状态代码为 403。

谁能建议如何解决它?

【问题讨论】:

  • 你需要设置一个 CORS 过滤器来允许你的本地主机连接,请检查这个。 link
  • 已经设置好了。 @Bean public CorsFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); CorsConfiguration config = jHipsterProperties.getCors(); if (config.getAllowedOrigins() != null &amp;&amp; !config.getAllowedOrigins().isEmpty()) { log.debug("Registering CORS filter"); source.registerCorsConfiguration("/api/**", config); source.registerCorsConfiguration("/v2/api-docs", config); } return new CorsFilter(source); }
  • 您没有 Access-Control-Allow-Origin 和 Access-Control-Allow-Methods 标头
  • @jorrin、Access-Control-Allow-Origin 和 Access-Control-Allow-Methods 标头在响应中。我收到 OPTION 请求的响应如下:Access-Control-Allow-Credentials:true Access-Control-Allow-Headers:content-type, x-xsrf-token Access-Control-Allow-Methods:GET,PUT,POST,DELETE,OPTIONS Access-Control-Allow-Origin:http://localhost:9000 Access-Control-Max-Age:1800 Connection:keep-alive Content-Length:0 Date:Fri, 07 Jul 2017 08:13:57 GMT Vary:Origin
  • 抱歉,我看不到 Access-Control-Allow-Origin 标头在哪里

标签: spring spring-mvc spring-boot cors jhipster


【解决方案1】:

你能试试这个吗?

# CORS is only enabled by default with the "dev" profile, so BrowserSync can access the API
cors:
    allowed-origins: "*"
    allowed-methods: GET, PUT, POST, DELETE, OPTIONS
    allowed-headers: "*"
    exposed-headers:
    allow-credentials: true
    max-age: 1800

【讨论】:

  • 是的,我解决了这个问题。实际上,CSRF 是这里的罪魁祸首。所以,我暂时禁用了 CSRF,它工作正常。
猜你喜欢
  • 2021-10-20
  • 2018-03-04
  • 2016-10-30
  • 1970-01-01
  • 2016-05-07
  • 2016-10-30
  • 2019-06-19
  • 2021-05-23
相关资源
最近更新 更多