From 0ccf7f14f790b66d170b90d4e00eea0bd1fcdb2c Mon Sep 17 00:00:00 2001 From: Knorrke Date: Wed, 22 May 2024 21:56:27 +0200 Subject: [PATCH] enable CORS for all interfaces --- pom.xml | 9 ++++++- src/main/java/config/Router.java | 42 +++++++++++++++----------------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/pom.xml b/pom.xml index 4a965d7..03740e6 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ 2.0.7 0.4 2.0.7 - 3.4 + 5.3 @@ -290,6 +290,13 @@ ${skipTests} + + + com.cosium.code + google-java-format + 5.3 + + com.github.eirslett diff --git a/src/main/java/config/Router.java b/src/main/java/config/Router.java index 3ae71b7..2858292 100644 --- a/src/main/java/config/Router.java +++ b/src/main/java/config/Router.java @@ -46,6 +46,26 @@ public Router(Javalin app, PostService postService, UserService userService) { public void setupRoutes() { app.routes( () -> { + // Activate CORS + app.options( + "/*", + ctx -> { + String accessControlRequestHeaders = ctx.header("Access-Control-Request-Headers"); + if (accessControlRequestHeaders != null) { + ctx.header("Access-Control-Allow-Headers", accessControlRequestHeaders); + } + + String accessControlRequestMethod = ctx.header("Access-Control-Request-Method"); + if (accessControlRequestMethod != null) { + ctx.header("Access-Control-Allow-Methods", accessControlRequestMethod); + } + }); + before( + "/*", + ctx -> { + ctx.header("Access-Control-Allow-Origin", "*"); + ctx.header("Access-Control-Allow-Headers", "*"); + }); get("/registrieren", userController::register); post("/registrieren", userController::register); @@ -78,28 +98,6 @@ public void setupRoutes() { get("", ctx -> ctx.redirect("/docs/index.html")); before("/*", ctx -> logger.debug("Received api call to path {}", ctx.path())); - // Activate CORS - app.options( - "/*", - ctx -> { - String accessControlRequestHeaders = - ctx.header("Access-Control-Request-Headers"); - if (accessControlRequestHeaders != null) { - ctx.header("Access-Control-Allow-Headers", accessControlRequestHeaders); - } - - String accessControlRequestMethod = - ctx.header("Access-Control-Request-Method"); - if (accessControlRequestMethod != null) { - ctx.header("Access-Control-Allow-Methods", accessControlRequestMethod); - } - }); - before( - "/*", - ctx -> { - ctx.header("Access-Control-Allow-Origin", "*"); - ctx.header("Access-Control-Allow-Headers", "*"); - }); before("/*", this::checkCorrectRequestType); before( "/*",