Skip to content

Commit

Permalink
[1.2.x] Allow to enable cors with configuration (#49) (#52)
Browse files Browse the repository at this point in the history
* feat: allow to enable cors with configuration

* feat: set list of string instead

(cherry picked from commit ac7e108)

Co-authored-by: f-necas <[email protected]>
  • Loading branch information
github-actions[bot] and f-necas authored Oct 31, 2024
1 parent 39bb97b commit 772c425
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import java.util.Enumeration;
import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
Expand Down Expand Up @@ -55,6 +58,7 @@
*/
@AutoConfiguration
@Import(SpringDocConfiguration.class)
@Slf4j
public class ApiAutoConfiguration implements WebMvcConfigurer {

@Bean
Expand Down Expand Up @@ -188,8 +192,21 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
}
}

@Value("${cors.allowed-origins:}")
private String[] allowedOrigins;

@Value("${cors.allowed-methods:*}")
private String[] allowedMethods;

@ConditionalOnProperty(name = "cors", havingValue = "true")
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("*");
if (allowedOrigins.length == 0) {
log.warn("CORS disabled");
return;
}
log.warn("CORS enabled for origins: " + Arrays.toString(allowedOrigins) + " methods: "
+ Arrays.toString(allowedMethods));
registry.addMapping("/**").allowedMethods(allowedMethods).allowedOrigins(allowedOrigins);
}
}
4 changes: 4 additions & 0 deletions src/services/ogc-features/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ springdoc:
#path: ${openapi.geoServerACL.base-path}/swagger-ui.html
try-it-out-enabled: true

#cors:
# allowed-origins: "*, https://localhost:8080"
# allowed-methods: GET, POST, PUT, DELETE, OPTIONS

logging:
level:
root: info
Expand Down

0 comments on commit 772c425

Please sign in to comment.