From 30c373982c94092fd3363475b8611d9ad5aee66b Mon Sep 17 00:00:00 2001 From: Liosha Date: Sun, 13 Sep 2026 18:05:12 +0200 Subject: [PATCH] demo-decoupled docker-compose --- docker-compose.yml | 25 +++++++++++++++++++ dockerfile | 11 ++++++++ spring-angular-ui/Dockerfile | 13 ++++++++++ .../security/WebSecurityConfig.java | 19 +++++++++++++- src/main/resources/application.yaml | 8 +++--- 5 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 docker-compose.yml create mode 100644 dockerfile create mode 100644 spring-angular-ui/Dockerfile diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..5ee6959 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,25 @@ +services: + backend: + build: . + container_name: decoupled_backend + ports: + - "8080:8080" + environment: + - SPRING_DATASOURCE_URL=jdbc:postgresql://jambo-postgres:5432/springangulardb + - SPRING_DATASOURCE_USERNAME=springangular_user + - SPRING_DATASOURCE_PASSWORD=postgrespw + - SPRING_DATASOURCE_DRIVER-CLASS-NAME=org.postgresql.Driver + networks: + - jambotron-net + + frontend: + build: ./spring-angular-ui + container_name: decoupled_frontend + ports: + - "8081:80" + networks: + - jambotron-net + +networks: + jambotron-net: + external: true \ No newline at end of file diff --git a/dockerfile b/dockerfile new file mode 100644 index 0000000..ce20a13 --- /dev/null +++ b/dockerfile @@ -0,0 +1,11 @@ +FROM eclipse-temurin:26-jdk-alpine AS build +WORKDIR /app +COPY . . +RUN chmod +x gradlew +RUN ./gradlew bootJar -x test --no-daemon + +FROM eclipse-temurin:26-jre-alpine +WORKDIR /app +COPY --from=build /app/build/libs/*.jar app.jar +EXPOSE 8080 +ENTRYPOINT ["java", "-jar", "app.jar"] \ No newline at end of file diff --git a/spring-angular-ui/Dockerfile b/spring-angular-ui/Dockerfile new file mode 100644 index 0000000..f43df33 --- /dev/null +++ b/spring-angular-ui/Dockerfile @@ -0,0 +1,13 @@ +# Этап сборки Angular +FROM node:20-alpine AS build +WORKDIR /app +COPY package*.json ./ +RUN npm install --legacy-peer-deps +COPY . . +RUN npm run build + +# Этап раздачи через Nginx +FROM nginx:alpine +COPY --from=build /app/static/browser /usr/share/nginx/html +EXPOSE 80 +CMD ["nginx", "-g", "daemon off;"] diff --git a/src/main/java/com/example/demo_decoupled/security/WebSecurityConfig.java b/src/main/java/com/example/demo_decoupled/security/WebSecurityConfig.java index d5040fc..ff1a998 100644 --- a/src/main/java/com/example/demo_decoupled/security/WebSecurityConfig.java +++ b/src/main/java/com/example/demo_decoupled/security/WebSecurityConfig.java @@ -101,7 +101,9 @@ public class WebSecurityConfig {// extends WebSecurityConfigurerAdapter { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { - http.csrf(csrf -> csrf.disable()) + http + .cors(cors -> cors.configurationSource(corsConfigurationSource())) // Включаем CORS + .csrf(csrf -> csrf.disable()) .exceptionHandling(exception -> exception.authenticationEntryPoint(unauthorizedHandler)) .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) .authorizeHttpRequests(auth -> @@ -126,4 +128,19 @@ public class WebSecurityConfig {// extends WebSecurityConfigurerAdapter { return http.build(); } + + // Добавляем метод конфигурации CORS источников и разрешенных методов + @Bean + public org.springframework.web.cors.CorsConfigurationSource corsConfigurationSource() { + org.springframework.web.cors.CorsConfiguration configuration = new org.springframework.web.cors.CorsConfiguration(); + // Укажите порты, с которых разрешено ходить (например, ваш Angular на 8081 и дефолтный 4200) + configuration.setAllowedOrigins(java.util.List.of("http://localhost:8081", "http://localhost:4200", "http://localhost")); + configuration.setAllowedMethods(java.util.List.of("GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH")); + configuration.setAllowedHeaders(java.util.List.of("*")); + configuration.setAllowCredentials(true); + + org.springframework.web.cors.UrlBasedCorsConfigurationSource source = new org.springframework.web.cors.UrlBasedCorsConfigurationSource(); + source.registerCorsConfiguration("/**", configuration); + return source; + } } diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 5a37438..ad5d8d2 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -1,14 +1,12 @@ spring: application: - name: demo-deoupled + name: demo-decoupled datasource: - url: jdbc:postgresql://localhost:5432/springangulardb + url: jdbc:postgresql://jambo-postgres:5432/springangulardb username: springangular_user password: postgrespw flyway: - enabled: true - baseline-on-migrate: true - url: jdbc:postgresql://localhost:5432/springangulardb?options=-c%20timezone=UTC + url: jdbc:postgresql://jambo-postgres:5432/springangulardb?options=-c%20timezone=UTC user: springangular_user password: postgrespw jpa: