Skip to content

Commit 78d8aec

Browse files
committed
Fix CORS headers on error handling (#555)
1 parent 958bb06 commit 78d8aec

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ public WebFilter corsFilter() {
2222

2323
final ServerHttpResponse response = ctx.getResponse();
2424
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");
25+
fillCorsHeader(headers);
2926

3027
if (request.getMethod() == HttpMethod.OPTIONS) {
3128
response.setStatusCode(HttpStatus.OK);
@@ -36,4 +33,10 @@ public WebFilter corsFilter() {
3633
};
3734
}
3835

36+
public static void fillCorsHeader(HttpHeaders responseHeaders) {
37+
responseHeaders.add("Access-Control-Allow-Origin", "*");
38+
responseHeaders.add("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
39+
responseHeaders.add("Access-Control-Max-Age", "3600");
40+
responseHeaders.add("Access-Control-Allow-Headers", "Content-Type");
41+
}
3942
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.common.base.Throwables;
44
import com.google.common.collect.Sets;
5+
import io.kafbat.ui.config.CorsGlobalConfiguration;
56
import io.kafbat.ui.model.ErrorResponseDTO;
67
import java.math.BigDecimal;
78
import java.util.List;
@@ -78,6 +79,7 @@ private Mono<ServerResponse> renderDefault(Throwable throwable, ServerRequest re
7879
return ServerResponse
7980
.status(ErrorCode.UNEXPECTED.httpStatus())
8081
.contentType(MediaType.APPLICATION_JSON)
82+
.headers(CorsGlobalConfiguration::fillCorsHeader)
8183
.bodyValue(response);
8284
}
8385

@@ -92,6 +94,7 @@ private Mono<ServerResponse> render(CustomBaseException baseException, ServerReq
9294
return ServerResponse
9395
.status(errorCode.httpStatus())
9496
.contentType(MediaType.APPLICATION_JSON)
97+
.headers(CorsGlobalConfiguration::fillCorsHeader)
9598
.bodyValue(response);
9699
}
97100

@@ -122,6 +125,7 @@ private Mono<ServerResponse> render(WebExchangeBindException exception, ServerRe
122125
return ServerResponse
123126
.status(HttpStatus.BAD_REQUEST)
124127
.contentType(MediaType.APPLICATION_JSON)
128+
.headers(CorsGlobalConfiguration::fillCorsHeader)
125129
.bodyValue(response);
126130
}
127131

@@ -136,6 +140,7 @@ private Mono<ServerResponse> render(ResponseStatusException exception, ServerReq
136140
return ServerResponse
137141
.status(exception.getStatusCode())
138142
.contentType(MediaType.APPLICATION_JSON)
143+
.headers(CorsGlobalConfiguration::fillCorsHeader)
139144
.bodyValue(response);
140145
}
141146

0 commit comments

Comments
 (0)