saw303/gradle-java-rest-api

Add CORS support

Opened this issue · 0 comments

Introduce a way to support CORS requests.

The Spring way

Spring supports CORS out of the box.

Usually Spring uses the org.springframework.web.filter.CorsFilter to handle the CORS request. We can configure this filter by providing a org.springframework.web.cors.CorsConfigurationSource.

The generator could create such a CorsConfigurationSource for the Spring application. E.g.

@Bean
  CorsConfigurationSource corsConfigurationSource() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration corsConfiguration = new CorsConfiguration();
    corsConfiguration.addAllowedOrigin("*");
    corsConfiguration.addAllowedMethod(HttpMethod.GET);
    corsConfiguration.addAllowedMethod(HttpMethod.PUT);
    corsConfiguration.addAllowedMethod(HttpMethod.POST);
    corsConfiguration.addAllowedMethod(HttpMethod.DELETE);
    corsConfiguration.addAllowedMethod(OPTIONS);
    source.registerCorsConfiguration("/**", corsConfiguration);
    return source;
  }

Java EE way

Currently not scope of this issue since the Java EE way has no priority at time.