Skip to content

Commit

Permalink
enable CORS for all interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Knorrke committed May 22, 2024
1 parent da13c7d commit 0ccf7f1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<embedded-postgres.version>2.0.7</embedded-postgres.version>
<jbcrypt.version>0.4</jbcrypt.version>
<sl4j.version>2.0.7</sl4j.version>
<git-code-format-maven-plugin.version>3.4</git-code-format-maven-plugin.version>
<git-code-format-maven-plugin.version>5.3</git-code-format-maven-plugin.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -290,6 +290,13 @@
<configuration>
<skip>${skipTests}</skip>
</configuration>
<dependencies>
<dependency>
<groupId>com.cosium.code</groupId>
<artifactId>google-java-format</artifactId>
<version>5.3</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>com.github.eirslett</groupId>
Expand Down
42 changes: 20 additions & 22 deletions src/main/java/config/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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(
"/*",
Expand Down

0 comments on commit 0ccf7f1

Please sign in to comment.