From 5d34c39421d10ccf096201a5057bb224d58a94ff Mon Sep 17 00:00:00 2001 From: Anton Babych Date: Tue, 19 Dec 2023 21:25:56 +0200 Subject: [PATCH 1/4] Add docker support to project and fix some problem with security --- Dockerfile | 16 ++++++++ docker-compose.yml | 41 +++++++++++++++++++ ...reAppApplication.java => Application.java} | 4 +- .../bookstoreapp/config/SecurityConfig.java | 10 +++-- src/main/resources/application.properties | 11 ++--- 5 files changed, 72 insertions(+), 10 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml rename src/main/java/store/bookstoreapp/{BookStoreAppApplication.java => Application.java} (68%) diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6fa3d00 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +# Builder stage +FROM openjdk:17-jdk-alpine as builder +WORKDIR application +ARG JAR_FILE=target/*.jar +COPY ${JAR_FILE} application.jar +RUN java -Djarmode=layertools -jar application.jar extract + +# Final stage +FROM openjdk:17-jdk-alpine +WORKDIR application +COPY --from=builder application/dependencies/ ./ +COPY --from=builder application/spring-boot-loader/ ./ +COPY --from=builder application/snapshot-dependencies/ ./ +COPY --from=builder application/application/ ./ +ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"] +EXPOSE 8080 \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..9d597f9 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,41 @@ +version: "3.8" + +services: + db: + image: mysql:latest + restart: unless-stopped + env_file: ./.env + ports: + - $MYSQL_LOCAL_PORT:$MYSQL_DOCKER_PORT + environment: + MYSQL_ROOT_PASSWORD: $MYSQL_ROOT_PASSWORD + MYSQL_DATABASE: $MYSQL_DATABASE + MYSQL_USER: $MYSQL_USER + MYSQL_PASSWORD: $MYSQL_PASSWORD + healthcheck: + test: [ "CMD", "mysql_isready -U mysql" ] + interval: 30s + timeout: 30s + retries: 3 + app: + depends_on: + - db + restart: on-failure + image: book-store-app + build: . + env_file: ./.env + ports: + - $SPRING_LOCAL_PORT:$SPRING_DOCKER_PORT + - $DEBUG_PORT:$DEBUG_PORT + environment: + SPRING_APPLICATION_JSON: '{ + "spring.datasource.url" : "jdbc:mysql://db:$MYSQL_DOCKER_PORT/$MYSQL_DATABASE", + "spring.datasource.username" : "$MYSQL_USER", + "spring.datasource.password" : "$MYSQL_PASSWORD", + "spring.datasource.driver-class-name" : "com.mysql.cj.jdbc.Driver", + "spring.jpa.hibernate.ddl-aut" : "validate", + "spring.jpa.show-sql" : "true", + "jwt.expiration" : "$JWT_EXPIRATION_TIME", + "jwt_secret" : "$JWT_SECRET" + }' + JAVA_TOOL_OPTIONS: "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005" diff --git a/src/main/java/store/bookstoreapp/BookStoreAppApplication.java b/src/main/java/store/bookstoreapp/Application.java similarity index 68% rename from src/main/java/store/bookstoreapp/BookStoreAppApplication.java rename to src/main/java/store/bookstoreapp/Application.java index 9125cd0..e2ec917 100644 --- a/src/main/java/store/bookstoreapp/BookStoreAppApplication.java +++ b/src/main/java/store/bookstoreapp/Application.java @@ -4,10 +4,10 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication -public class BookStoreAppApplication { +public class Application { public static void main(String[] args) { - SpringApplication.run(BookStoreAppApplication.class, args); + SpringApplication.run(Application.class, args); } } diff --git a/src/main/java/store/bookstoreapp/config/SecurityConfig.java b/src/main/java/store/bookstoreapp/config/SecurityConfig.java index ed8e7a7..4098ac3 100644 --- a/src/main/java/store/bookstoreapp/config/SecurityConfig.java +++ b/src/main/java/store/bookstoreapp/config/SecurityConfig.java @@ -4,7 +4,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.AuthenticationManager; -import org.springframework.security.config.Customizer; import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration; import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity; @@ -36,12 +35,17 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti .csrf(AbstractHttpConfigurer::disable) .authorizeHttpRequests( auth -> auth - .requestMatchers("api/auth/**", "api/error", "api/swagger-ui/**") + .requestMatchers( + "/api/auth/**", + "/api/error", + "/swagger-ui/**", + "/v3/api-docs/**", + "/swagger-resources/**" + ) .permitAll() .anyRequest() .authenticated() ) - .httpBasic(Customizer.withDefaults()) .sessionManagement( session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) .addFilterBefore( diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 676e3e5..56d4505 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,12 +1,13 @@ -spring.datasource.url=jdbc:mysql://localhost:3306/book_store -spring.datasource.username=root -spring.datasource.password=password +spring.datasource.url=jdbc:mysql://localhost:${MYSQL_DOCKER_PORT}\ + /${MYSQL_DATABASE}?serverTimeZone=UTC +spring.datasource.username=${MYSQL_USER} +spring.datasource.password=${MYSQL_ROOT_PASSWORD} spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=validate spring.jpa.show-sql=true -jwt.expiration=1000000 -jwt.secret=p1tbookstoreappsomerandomsymholstokey1231488p1t +jwt.expiration=${JWT_EXPIRATION_TIME} +jwt.secret=${JWT_SECRET} From 48401931fba34dc5b55e25b0c79006df566c6d85 Mon Sep 17 00:00:00 2001 From: Anton Babych Date: Tue, 19 Dec 2023 21:55:41 +0200 Subject: [PATCH 2/4] Add docker support to project and fix some problem with security --- .env | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 .env diff --git a/.env b/.env new file mode 100644 index 0000000..c554ae7 --- /dev/null +++ b/.env @@ -0,0 +1,13 @@ +MYSQL_USER=myuser +MYSQL_PASSWORD=password +MYSQL_DATABASE=book_store +MYSQL_ROOT_PASSWORD=password +MYSQL_LOCAL_PORT=3307 +MYSQL_DOCKER_PORT=3306 + +SPRING_LOCAL_PORT=8088 +SPRING_DOCKER_PORT=8080 +DEBUG_PORT=5005 + +JWT_EXPIRATION_TIME=5000000 +JWT_SECRET=p1tbookstoreappsomerandomsymholstokey1231488p1t \ No newline at end of file From 981550ab490333222e98710adbe7d18e67dd2e80 Mon Sep 17 00:00:00 2001 From: Anton Babych Date: Wed, 20 Dec 2023 18:14:20 +0200 Subject: [PATCH 3/4] Some changes to docker --- .env | 2 +- Dockerfile | 2 +- docker-compose.yml | 30 ++++++++----------- pom.xml | 4 +++ src/main/resources/application.properties | 11 ++----- .../BookStoreAppApplicationTests.java | 5 ---- 6 files changed, 21 insertions(+), 33 deletions(-) diff --git a/.env b/.env index c554ae7..338dbbc 100644 --- a/.env +++ b/.env @@ -1,4 +1,4 @@ -MYSQL_USER=myuser +MYSQL_USER=admin123 MYSQL_PASSWORD=password MYSQL_DATABASE=book_store MYSQL_ROOT_PASSWORD=password diff --git a/Dockerfile b/Dockerfile index 6fa3d00..1a1418f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,5 +12,5 @@ COPY --from=builder application/dependencies/ ./ COPY --from=builder application/spring-boot-loader/ ./ COPY --from=builder application/snapshot-dependencies/ ./ COPY --from=builder application/application/ ./ -ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"] +ENTRYPOINT ["java", "org.springframework.boot.loader.launch.JarLauncher"] EXPOSE 8080 \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 9d597f9..d37304e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,8 +1,8 @@ version: "3.8" services: - db: - image: mysql:latest + mysqldb: + image: mysql restart: unless-stopped env_file: ./.env ports: @@ -12,14 +12,10 @@ services: MYSQL_DATABASE: $MYSQL_DATABASE MYSQL_USER: $MYSQL_USER MYSQL_PASSWORD: $MYSQL_PASSWORD - healthcheck: - test: [ "CMD", "mysql_isready -U mysql" ] - interval: 30s - timeout: 30s - retries: 3 + app: depends_on: - - db + - mysqldb restart: on-failure image: book-store-app build: . @@ -29,13 +25,13 @@ services: - $DEBUG_PORT:$DEBUG_PORT environment: SPRING_APPLICATION_JSON: '{ - "spring.datasource.url" : "jdbc:mysql://db:$MYSQL_DOCKER_PORT/$MYSQL_DATABASE", - "spring.datasource.username" : "$MYSQL_USER", - "spring.datasource.password" : "$MYSQL_PASSWORD", - "spring.datasource.driver-class-name" : "com.mysql.cj.jdbc.Driver", - "spring.jpa.hibernate.ddl-aut" : "validate", - "spring.jpa.show-sql" : "true", - "jwt.expiration" : "$JWT_EXPIRATION_TIME", - "jwt_secret" : "$JWT_SECRET" - }' + "spring.datasource.url" : "jdbc:mysql://mysqldb:$MYSQL_DOCKER_PORT/$MYSQL_DATABASE?serverTimezone=UTC", + "spring.datasource.username" : "$MYSQL_USER", + "spring.datasource.password" : "$MYSQL_PASSWORD", + "spring.datasource.driver-class-name" : "com.mysql.cj.jdbc.Driver", + "spring.jpa.hibernate.ddl-aut" : "validate", + "spring.jpa.show-sql" : "true", + "jwt.expiration" : "$JWT_EXPIRATION_TIME", + "jwt_secret" : "$JWT_SECRET" + }' JAVA_TOOL_OPTIONS: "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005" diff --git a/pom.xml b/pom.xml index 73e7df5..6ce059a 100644 --- a/pom.xml +++ b/pom.xml @@ -100,6 +100,10 @@ jjwt-jackson ${jjwt.version} + + org.springframework.boot + spring-boot-docker-compose + diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 56d4505..435be3d 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,13 +1,6 @@ -spring.datasource.url=jdbc:mysql://localhost:${MYSQL_DOCKER_PORT}\ - /${MYSQL_DATABASE}?serverTimeZone=UTC -spring.datasource.username=${MYSQL_USER} -spring.datasource.password=${MYSQL_ROOT_PASSWORD} -spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=validate spring.jpa.show-sql=true -jwt.expiration=${JWT_EXPIRATION_TIME} -jwt.secret=${JWT_SECRET} - - +jwt.expiration=1000000 +jwt.secret=p1tbookstoreappsomerandomsymholstokey1231488p1t diff --git a/src/test/java/store/bookstoreapp/BookStoreAppApplicationTests.java b/src/test/java/store/bookstoreapp/BookStoreAppApplicationTests.java index 2f5cb0f..8352eb4 100644 --- a/src/test/java/store/bookstoreapp/BookStoreAppApplicationTests.java +++ b/src/test/java/store/bookstoreapp/BookStoreAppApplicationTests.java @@ -1,13 +1,8 @@ package store.bookstoreapp; -import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class BookStoreAppApplicationTests { - @Test - void contextLoads() { - } - } From 5454d9b579a981edaaa080c9a4d6ba60a1d79163 Mon Sep 17 00:00:00 2001 From: Anton Babych Date: Thu, 21 Dec 2023 14:36:15 +0200 Subject: [PATCH 4/4] Some changes to docker and set shopping carts to existing user --- docker-compose.yml | 2 +- ...14-set-shopping-cart-to-existing-user.yaml | 27 +++++++++++++++++++ .../db/changelog/db.changelog-master.yaml | 4 ++- .../BookStoreAppApplicationTests.java | 4 +++ 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 src/main/resources/db/changelog/changes/14-set-shopping-cart-to-existing-user.yaml diff --git a/docker-compose.yml b/docker-compose.yml index d37304e..4e84afc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -25,7 +25,7 @@ services: - $DEBUG_PORT:$DEBUG_PORT environment: SPRING_APPLICATION_JSON: '{ - "spring.datasource.url" : "jdbc:mysql://mysqldb:$MYSQL_DOCKER_PORT/$MYSQL_DATABASE?serverTimezone=UTC", + "spring.datasource.url" : "jdbc:mysql://mysqldb:3306/$MYSQL_DATABASE?createDatabaseIfNotExist=true&allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC", "spring.datasource.username" : "$MYSQL_USER", "spring.datasource.password" : "$MYSQL_PASSWORD", "spring.datasource.driver-class-name" : "com.mysql.cj.jdbc.Driver", diff --git a/src/main/resources/db/changelog/changes/14-set-shopping-cart-to-existing-user.yaml b/src/main/resources/db/changelog/changes/14-set-shopping-cart-to-existing-user.yaml new file mode 100644 index 0000000..cbc3479 --- /dev/null +++ b/src/main/resources/db/changelog/changes/14-set-shopping-cart-to-existing-user.yaml @@ -0,0 +1,27 @@ +databaseChangeLog: + - changeSet: + id: 1-add-shopping-cart-to-default-user + author: antonbabych + changes: + - insert: + tableName: shopping_cards + columns: + - column: + name: user_id + valueNumeric: 1 + - column: + name: is_deleted + value: 0 + - changeSet: + id: 2-add-shopping-cart-to-default-admin + author: antonbabych + changes: + - insert: + tableName: shopping_cards + columns: + - column: + name: user_id + valueNumeric: 2 + - column: + name: is_deleted + value: 0 diff --git a/src/main/resources/db/changelog/db.changelog-master.yaml b/src/main/resources/db/changelog/db.changelog-master.yaml index 9f3a66a..6d7d530 100644 --- a/src/main/resources/db/changelog/db.changelog-master.yaml +++ b/src/main/resources/db/changelog/db.changelog-master.yaml @@ -24,4 +24,6 @@ databaseChangeLog: - include: file: db/changelog/changes/12-create-order-table.yaml - include: - file: db/changelog/changes/13-create-order-item-table.yaml \ No newline at end of file + file: db/changelog/changes/13-create-order-item-table.yaml + - include: + file: db/changelog/changes/14-set-shopping-cart-to-existing-user.yaml \ No newline at end of file diff --git a/src/test/java/store/bookstoreapp/BookStoreAppApplicationTests.java b/src/test/java/store/bookstoreapp/BookStoreAppApplicationTests.java index 8352eb4..c948d3e 100644 --- a/src/test/java/store/bookstoreapp/BookStoreAppApplicationTests.java +++ b/src/test/java/store/bookstoreapp/BookStoreAppApplicationTests.java @@ -1,8 +1,12 @@ package store.bookstoreapp; +import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class BookStoreAppApplicationTests { + @Test + void contextLoads(){ + } }