Skip to content

Commit

Permalink
Fix CORS config for AOT
Browse files Browse the repository at this point in the history
  • Loading branch information
ystxn committed Aug 11, 2024
1 parent 8a39f30 commit 0db346a
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ out
.vscode/

### Spring ###
application-*.yml
src/main/resources/application-*.yml

### Web ###
**/node_modules
Expand Down
45 changes: 22 additions & 23 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
plugins {
id 'com.github.ben-manes.versions' version '0.51.0'
id 'org.springframework.boot' version '3.3.2'
id 'io.spring.dependency-management' version '1.1.6'
id 'io.freefair.lombok' version '8.6'
id 'org.graalvm.buildtools.native' version '0.10.2'
id 'java'
id 'jacoco'
id "com.github.ben-manes.versions" version "0.51.0"
id "org.springframework.boot" version "3.3.2"
id "io.spring.dependency-management" version "1.1.6"
id "io.freefair.lombok" version "8.7.1"
id "org.graalvm.buildtools.native" version "0.10.2"
id "java"
id "jacoco"
}

group = 'tech.sledger'
group = "tech.sledger"

java {
toolchain {
Expand All @@ -22,7 +22,7 @@ tasks.withType(JavaCompile).configureEach {
options.compilerArgs.add("-parameters")
}

def testContainersVersion = '1.20.0'
def testContainersVersion = "1.20.1"

dependencies {
implementation "org.springframework.boot:spring-boot-starter-web"
Expand All @@ -34,11 +34,11 @@ dependencies {
implementation "org.bouncycastle:bcprov-jdk18on:1.78.1"
implementation "com.auth0:java-jwt:4.4.0"
implementation ("com.opencsv:opencsv:5.9") {
exclude group: 'commons-logging', module: 'commons-logging'
exclude group: "commons-logging", module: "commons-logging"
}
implementation ("org.jxls:jxls-jexcel:1.0.9") {
exclude group: 'commons-logging', module: 'commons-logging'
exclude group: 'log4j', module: 'log4j'
exclude group: "commons-logging", module: "commons-logging"
exclude group: "log4j", module: "log4j"
}
developmentOnly "org.springframework.boot:spring-boot-devtools"

Expand All @@ -51,7 +51,7 @@ dependencies {
testImplementation "org.testcontainers:mongodb:$testContainersVersion"
}

tasks.named("jar") {enabled = false }
tasks.named("jar") { enabled = false }

tasks.withType(Test).configureEach { useJUnitPlatform() }
test {
Expand All @@ -64,38 +64,37 @@ test {
html.required = false
}
finalizedBy jacocoTestReport
jacoco { excludes = [ 'tech.sledger.model.*' ] }
}
jacocoTestReport {
dependsOn test
afterEvaluate {
getClassDirectories().setFrom(classDirectories.files.collect {
fileTree(dir: it, exclude: [
'tech/sledger/*.class',
'tech/sledger/model/**',
'tech/sledger/service/ResendService.class',
'tech/sledger/config/AotHints.class'
"tech/sledger/*.class",
"tech/sledger/model/**",
"tech/sledger/service/ResendService.class",
"tech/sledger/config/AotHints.class"
])
})
}
reports {
xml.required = true
html.outputLocation = layout.buildDirectory.dir('jacocoHtml')
html.outputLocation = layout.buildDirectory.dir("jacocoHtml")
}
}

bootBuildImage {
imageName = "$System.env.IMAGE_NAME"
publish = true
imageName = System.getenv("IMAGE_NAME") ?: "sledger-backend"
publish = System.getenv("DOCKER_PASS") != null
createdDate = "now"
environment = [
"BP_JVM_VERSION": "22",
"BP_SPRING_CLOUD_BINDINGS_DISABLED": "true"
]
docker {
publishRegistry {
username = "$System.env.DOCKER_USER"
password = "$System.env.DOCKER_PASS"
username = System.getenv("DOCKER_USER")
password = System.getenv("DOCKER_PASS")
}
}
}
31 changes: 31 additions & 0 deletions src/main/java/tech/sledger/config/CorsConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package tech.sledger.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import java.util.List;

@Slf4j
@Configuration
public class CorsConfig {
@Value("${sledger.cors:false}")
private String cors;

@Bean
public CorsFilter corsFilter() {
var source = new UrlBasedCorsConfigurationSource();
if ("true".equalsIgnoreCase(cors)) {
log.info("CorsFilter Activated");
var config = new CorsConfiguration();
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE"));
source.registerCorsConfiguration("/**", config);
}
return new CorsFilter(source);
}
}
17 changes: 0 additions & 17 deletions src/main/java/tech/sledger/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.MongoTransactionManager;
import org.springframework.security.authentication.AuthenticationManager;
Expand All @@ -19,10 +18,6 @@
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import java.util.List;

@Configuration
@EnableWebSecurity
Expand Down Expand Up @@ -86,16 +81,4 @@ public AuthenticationManager authenticationManager(
MongoTransactionManager transactionManager(MongoDatabaseFactory dbFactory) {
return new MongoTransactionManager(dbFactory);
}

@Profile("dev")
@Bean
public CorsFilter corsFilter() {
var source = new UrlBasedCorsConfigurationSource();
var config = new CorsConfiguration();
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE"));
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}
1 change: 1 addition & 0 deletions src/test/resources/application-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sledger.cors: true

0 comments on commit 0db346a

Please sign in to comment.