Skip to content

Commit 1f9e6f3

Browse files
committed
Closes #535, allowing to configure cors through spring properties
1 parent c8a8759 commit 1f9e6f3

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

api/src/main/java/io/kafbat/ui/config/CorsGlobalConfiguration.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.kafbat.ui.config;
22

3+
import org.springframework.beans.factory.annotation.Autowired;
34
import org.springframework.context.annotation.Bean;
45
import org.springframework.context.annotation.Configuration;
56
import org.springframework.http.HttpHeaders;
@@ -15,17 +16,20 @@
1516
@Configuration
1617
public class CorsGlobalConfiguration {
1718

19+
@Autowired
20+
private CorsProperties corsProperties;
21+
1822
@Bean
1923
public WebFilter corsFilter() {
2024
return (final ServerWebExchange ctx, final WebFilterChain chain) -> {
2125
final ServerHttpRequest request = ctx.getRequest();
2226

2327
final ServerHttpResponse response = ctx.getResponse();
2428
final HttpHeaders headers = response.getHeaders();
25-
headers.add("Access-Control-Allow-Origin", "*");
26-
headers.add("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
27-
headers.add("Access-Control-Max-Age", "3600");
28-
headers.add("Access-Control-Allow-Headers", "Content-Type");
29+
headers.add("Access-Control-Allow-Origin", corsProperties.getAllowedOrigins());
30+
headers.add("Access-Control-Allow-Methods", corsProperties.getAllowedMethods());
31+
headers.add("Access-Control-Max-Age", corsProperties.getMaxAge());
32+
headers.add("Access-Control-Allow-Headers", corsProperties.getAllowedHeaders());
2933

3034
if (request.getMethod() == HttpMethod.OPTIONS) {
3135
response.setStatusCode(HttpStatus.OK);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.kafbat.ui.config;
2+
3+
import org.springframework.boot.context.properties.ConfigurationProperties;
4+
import org.springframework.stereotype.Component;
5+
import lombok.Data;
6+
7+
@Component
8+
@ConfigurationProperties(prefix = "cors")
9+
@Data
10+
11+
public class CorsProperties {
12+
13+
private String allowedOrigins;
14+
private String allowedMethods;
15+
private String allowedHeaders;
16+
private String maxAge;
17+
18+
}

api/src/main/resources/application.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ logging:
1919
reactor.netty.http.server.AccessLog: INFO
2020
org.hibernate.validator: WARN
2121

22+
cors:
23+
allowed-origins: "*"
24+
allowed-methods: "GET, PUT, POST, DELETE, OPTIONS"
25+
allowed-headers: "Content-Type"
26+
max-age: "3600"
27+

0 commit comments

Comments
 (0)