Skip to content

Commit 0228b5b

Browse files
committed
Closes #535, allowing to configure cors through spring properties
1 parent 4cf17a0 commit 0228b5b

File tree

5 files changed

+76
-10
lines changed

5 files changed

+76
-10
lines changed

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

Lines changed: 11 additions & 7 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,14 +16,17 @@
1516
@Configuration
1617
public class CorsGlobalConfiguration {
1718

19+
@Autowired
20+
private static 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-
fillCorsHeader(headers, request);
29+
fillCorsHeader(headers);
2630

2731
if (request.getMethod() == HttpMethod.OPTIONS) {
2832
response.setStatusCode(HttpStatus.OK);
@@ -33,11 +37,11 @@ public WebFilter corsFilter() {
3337
};
3438
}
3539

36-
public static void fillCorsHeader(HttpHeaders responseHeaders, ServerHttpRequest request) {
37-
responseHeaders.add("Access-Control-Allow-Origin", request.getHeaders().getOrigin());
38-
responseHeaders.add("Access-Control-Allow-Credentials", "true");
39-
responseHeaders.add("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
40-
responseHeaders.add("Access-Control-Max-Age", "3600");
41-
responseHeaders.add("Access-Control-Allow-Headers", "Content-Type");
40+
public static void fillCorsHeader(HttpHeaders responseHeaders) {
41+
responseHeaders.add("Access-Control-Allow-Origin", corsProperties.getAllowedOrigins());
42+
responseHeaders.add("Access-Control-Allow-Credentials", corsProperties.getAllowCredentials());
43+
responseHeaders.add("Access-Control-Allow-Methods", corsProperties.getAllowedMethods());
44+
responseHeaders.add("Access-Control-Max-Age", corsProperties.getMaxAge());
45+
responseHeaders.add("Access-Control-Allow-Headers", corsProperties.getAllowedHeaders());
4246
}
4347
}
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+
public class CorsProperties {
11+
12+
private String allowedOrigins;
13+
private String allowedMethods;
14+
private String allowedHeaders;
15+
private String allowCredentials;
16+
private String maxAge;
17+
18+
}

api/src/main/java/io/kafbat/ui/exception/GlobalErrorWebExceptionHandler.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,7 @@ private String requestId(ServerRequest request) {
151151
}
152152

153153
private Consumer<HttpHeaders> headers(ServerRequest request) {
154-
return (HttpHeaders headers) -> {
155-
CorsGlobalConfiguration.fillCorsHeader(headers, request.exchange().getRequest());
156-
};
154+
return CorsGlobalConfiguration::fillCorsHeader;
157155
}
158156

159157
private BigDecimal currentTimestamp() {

api/src/main/resources/application.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,10 @@ 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+
allow-credentials: "true"
27+
max-age: "3600"
28+

contract/src/main/resources/swagger/kafbat-ui-api.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4391,3 +4391,42 @@ components:
43914391
type: object
43924392
additionalProperties:
43934393
type: string
4394+
cors:
4395+
type: object
4396+
properties:
4397+
allowedOrigins:
4398+
type: array
4399+
items:
4400+
type: string
4401+
description: >-
4402+
List of allowed origins for CORS.
4403+
If not provided, defaults to allowing all origins (`*`)
4404+
default: ["*"]
4405+
allowedMethods:
4406+
type: array
4407+
items:
4408+
type: string
4409+
description: >-
4410+
List of allowed HTTP methods for CORS
4411+
If not provided, defaults to `GET, POST, PUT, DELETE, OPTIONS`.
4412+
default: ["GET", "POST", "PUT", "DELETE", "OPTIONS"]
4413+
allowedHeaders:
4414+
type: array
4415+
items:
4416+
type: string
4417+
description: >-
4418+
List of allowed HTTP headers for CORS.
4419+
If not provided, defaults to allowing all headers (`*`).
4420+
default: ["*"]
4421+
allowCredentials:
4422+
type: boolean
4423+
description: >-
4424+
Whether to allow credentials in CORS requests.
4425+
If not provided, defaults to `true`
4426+
default: true
4427+
maxAge:
4428+
type: integer
4429+
description: >-
4430+
Maximum age (in seconds) for CORS preflight requests.
4431+
If not provided, defaults to `3600` seconds.
4432+
default: 3600

0 commit comments

Comments
 (0)