【问题标题】:Global CORS configuration using Java Config is not working in Spring MVC使用 Java Config 的全局 CORS 配置在 Spring MVC 中不起作用
【发布时间】:2021-04-16 09:08:39
【问题描述】:

我正在尝试为我在项目中开发的所有 Rest API 全局启用 CROS,同样我也尝试过这样做 使用 Java Config 进行全局 CORS 配置。 以下是带有@Configuration 注解的配置类。

package com.fs;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 *
 * @author 
 */
@Configuration
@ComponentScan("com.fs")
@EnableWebMvc
public class AppConfig extends WebMvcConfigurerAdapter 
{
    @Override
    public void addCorsMappings(CorsRegistry registry) {
           
      registry.addMapping("/**")
          .allowedOrigins("http://localhost:3000")
          .allowedMethods("POST", "GET",  "PUT", "OPTIONS", "DELETE")
          .allowedHeaders("X-Auth-Token", "Content-Type")        
          .maxAge(4800);
    }
}

现在,我正在尝试使用 AX 从运行在 https://localhost:3000 上的反应应用程序访问 API,但它没有提供响应并产生网络错误。

CORS 策略已阻止从源“http://localhost:3000”访问“http://localhost:8089/ewamftrxnsws/purchase/getSchemeList”处的 XMLHttpRequest:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。

let requestUrl = "http://localhost:8089/ewamftrxnsws/purchase/getSchemeList";
let promiseResponse = axios.post(requestUrl,{
        headers: {'Content-Type': 'application/json',}
       });
    promiseResponse.then(res=>{
        let isSuccess = res.data;
}).catch( error =>{
        console.log( "MFSchemeListApi catch => "+error);        
    });

有什么问题?谢谢。

【问题讨论】:

  • 如明确所述,您需要在配置中添加“Access-Control-Allow-Origin”。
  • 在配置中添加了相同的配置,但仍然无法正常工作 .allowedHeaders("X-Auth-Token", "Content-Type","Access-Control-Allow-Origin") @Umeshwaran跨度>
  • 试试allowedMethods("*")..allowedHeaders("*")而不是.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE").allowedHeaders("X-Auth-Token", "Content-Type")
  • 仍然无法正常工作,尽管控制器级别的配置正在工作@CrossOrigin(origins = "http://localhost:3000")@Umeshwaran
  • 请看我的回答。它可能有助于解决您的问题。试一试

标签: java reactjs spring-mvc axios cross-origin-read-blocking


【解决方案1】:

问题可能是由于不推荐使用的“WebMvcConfigurerAdapter”。尝试使用“WebMvcConfigurer”。

@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {

        registry.addMapping("/**")
                .allowedOrigins(
                        "http://localhost:3000",..)
                .allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD")
                .allowCredentials(true)
        ;
    }

}

这应该将“Access-Control-Allow-Credentials”标头带回请求中。

希望这能解决您的问题。试试看!!!

【讨论】:

    【解决方案2】:

    来自:https://spring.io/blog/2015/06/08/cors-support-in-spring-framework

    如果您使用 Spring Security,请确保在 Spring Security 级别启用 CORS,以允许它利用在 Spring MVC 级别定义的配置。

    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.cors().and()...
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-08
      • 1970-01-01
      • 2019-11-13
      • 2015-04-15
      • 2017-01-14
      • 2015-02-12
      • 1970-01-01
      • 2011-09-04
      • 2018-07-23
      相关资源
      最近更新 更多