Skip to content

Commit

Permalink
Fix CORS headers on error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hadisfr committed Sep 21, 2024
1 parent 1e4dff2 commit b8d46e2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ public WebFilter corsFilter() {

final ServerHttpResponse response = ctx.getResponse();
final HttpHeaders headers = response.getHeaders();
headers.add("Access-Control-Allow-Origin", "*");
headers.add("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
headers.add("Access-Control-Max-Age", "3600");
headers.add("Access-Control-Allow-Headers", "Content-Type");
fillCorsHeader(headers);

if (request.getMethod() == HttpMethod.OPTIONS) {
response.setStatusCode(HttpStatus.OK);
Expand All @@ -36,4 +33,10 @@ public WebFilter corsFilter() {
};
}

public static void fillCorsHeader(HttpHeaders responseHeaders) {
responseHeaders.add("Access-Control-Allow-Origin", "*");
responseHeaders.add("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
responseHeaders.add("Access-Control-Max-Age", "3600");
responseHeaders.add("Access-Control-Allow-Headers", "Content-Type");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.base.Throwables;
import com.google.common.collect.Sets;
import io.kafbat.ui.config.CorsGlobalConfiguration;
import io.kafbat.ui.model.ErrorResponseDTO;
import java.math.BigDecimal;
import java.util.List;
Expand Down Expand Up @@ -78,6 +79,7 @@ private Mono<ServerResponse> renderDefault(Throwable throwable, ServerRequest re
return ServerResponse
.status(ErrorCode.UNEXPECTED.httpStatus())
.contentType(MediaType.APPLICATION_JSON)
.headers(CorsGlobalConfiguration::fillCorsHeader)
.bodyValue(response);
}

Expand All @@ -92,6 +94,7 @@ private Mono<ServerResponse> render(CustomBaseException baseException, ServerReq
return ServerResponse
.status(errorCode.httpStatus())
.contentType(MediaType.APPLICATION_JSON)
.headers(CorsGlobalConfiguration::fillCorsHeader)
.bodyValue(response);
}

Expand Down Expand Up @@ -122,6 +125,7 @@ private Mono<ServerResponse> render(WebExchangeBindException exception, ServerRe
return ServerResponse
.status(HttpStatus.BAD_REQUEST)
.contentType(MediaType.APPLICATION_JSON)
.headers(CorsGlobalConfiguration::fillCorsHeader)
.bodyValue(response);
}

Expand All @@ -136,6 +140,7 @@ private Mono<ServerResponse> render(ResponseStatusException exception, ServerReq
return ServerResponse
.status(exception.getStatusCode())
.contentType(MediaType.APPLICATION_JSON)
.headers(CorsGlobalConfiguration::fillCorsHeader)
.bodyValue(response);
}

Expand Down

0 comments on commit b8d46e2

Please sign in to comment.