【问题标题】:Global CORS configuration using Java Config is not working使用 Java Config 的全局 CORS 配置不起作用
【发布时间】:2017-10-08 06:43:33
【问题描述】:

我很难使用 Java 配置来获得全局 CORS 配置。我不确定这是否是一个已知问题,因为其他人也有同样的问题: Spring Global CORS configuration not working but Controller level config does

无论如何,让我进一步详细说明我正在尝试做的事情:

我正在使用 Spring 4.2.3.RELEASE(没有弹簧靴)。我正在尝试使用 Java config 配置全局 CORS 配置,以便我可以使用属性文件中的 @Value 注入 CORS 域(允许的来源)。因为据我所知,我无法在 mvc xml 命名空间中为 allowed-origin 注入值,如下图所示(如果您知道解决此问题的任何其他方法,请告诉我):

<mvc:cors>
        <mvc:mapping path="/**" allowed-origins="${vrm.cors.domain}"
                     allowed-methods="*"
                     allowed-headers="*"
                     allow-credentials="false" max-age="3600"/>
</mvc:cors>

来自 Spring 参考文档here,我正在尝试使用 java config 配置 Global CORS 来解决问题。但是,我无法让 CORS 工作。代码在应用程序启动期间确实调用,但来自客户端的调用总是返回

请求中没有“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点“http://localhost:3000” 访问

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
    @Value("${vrm.cors.domain}")
    //vrm.cors.domain=/**
    private String corsDomain;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").
        .allowedOrigins(vrm.cors.domain);
    }
}

【问题讨论】:

  • 为什么vrm.cors.domain 设置为/**?如果你想允许一切,不应该是*吗?
  • 对不起,这是我的示例代码中的错误。我已经更新了。

标签: java spring spring-mvc cors property-files


【解决方案1】:

根据这个回答https://stackoverflow.com/a/33823253

您可以像这样启用 cors:

@EnableWebSecurity
@Configuration
class SecurityConfig extends WebSecurityConfigurerAdapter {

  private final AppProperties appProperties;

  @Autowired
  public SecurityConfig(AppProperties appProperties) {
    this.appProperties = appProperties;
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
        .addFilterBefore(corsFilter(), SessionManagementFilter.class) //adds your custom CorsFilter
        .authorizeRequests()
        .antMatchers("/**")
        .permitAll()
        .and()
        .csrf()
        .disable();
  }

  private CorsFilter corsFilter() {
    return new CorsFilterAdapter(
        appProperties.getClientUrls(),
        appProperties.getHeaders(),
        appProperties.getMethods())
        .corsFilter();
  }
}

CrosFilterAdapter 是这样的:

public class CorsFilterAdapter {

  private final String[] clientUrls;
  private final String[] headers;
  private final String[] methods;

  public CorsFilterAdapter(String clientUrls, String headers, String methods) {
    this.clientUrls = clientUrls.split(",");
    this.headers = headers.split(",");
    this.methods = methods.split(",");
  }

  public CorsFilter corsFilter() {
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(false);
    for (String clientUrl : clientUrls) {
      config.addAllowedOrigin(clientUrl);
    }
    for (String header: headers) {
      config.addAllowedHeader(header);
    }
    for (String method : methods) {
      config.addAllowedMethod(method);
    }
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", config);
    return new CorsFilter(source);
  }
}

【讨论】:

    猜你喜欢
    • 2021-04-16
    • 1970-01-01
    • 2017-01-14
    • 2019-11-13
    • 2018-07-23
    • 1970-01-01
    • 2020-06-23
    • 2022-01-15
    • 1970-01-01
    相关资源
    最近更新 更多