From 2f4846e20d8e12304761897671f90296b3ce9468 Mon Sep 17 00:00:00 2001 From: roma-dxunvrs Date: Sat, 11 Apr 2026 16:24:39 +0300 Subject: [PATCH] auth-service done, it's work --- auth-service/.gitignore | 3 +- auth-service/Dockerfile | 16 +++++++++ auth-service/build.gradle | 2 +- .../controller/AuthController.java | 34 +++++++++++++++++++ .../dxunvrs/auth_service/dto/AuthRequest.java | 9 +++++ .../auth_service/dto/AuthResponse.java | 10 ++++++ .../exception/GlobalExceptionHandler.java | 33 ++++++++++++++++++ .../auth_service/service/AuthService.java | 1 - .../src/main/resources/application.properties | 7 +++- docker-compose.yml | 18 ++++++++-- 10 files changed, 127 insertions(+), 6 deletions(-) create mode 100644 auth-service/Dockerfile create mode 100644 auth-service/src/main/java/com/dxunvrs/auth_service/controller/AuthController.java create mode 100644 auth-service/src/main/java/com/dxunvrs/auth_service/dto/AuthRequest.java create mode 100644 auth-service/src/main/java/com/dxunvrs/auth_service/dto/AuthResponse.java create mode 100644 auth-service/src/main/java/com/dxunvrs/auth_service/exception/GlobalExceptionHandler.java diff --git a/auth-service/.gitignore b/auth-service/.gitignore index c2065bc..35504a0 100644 --- a/auth-service/.gitignore +++ b/auth-service/.gitignore @@ -1,4 +1,5 @@ -HELP.md +.postman/ + .gradle build/ !gradle/wrapper/gradle-wrapper.jar diff --git a/auth-service/Dockerfile b/auth-service/Dockerfile new file mode 100644 index 0000000..65fdf63 --- /dev/null +++ b/auth-service/Dockerfile @@ -0,0 +1,16 @@ +FROM eclipse-temurin:17-jdk-alpine AS builder +WORKDIR /build + +COPY . . +RUN ./gradlew bootJar + +FROM eclipse-temurin:17-jre-alpine +WORKDIR /app + +COPY --from=builder /build/build/libs/auth-service-1.0.jar app.jar + +ENV SERVER_PORT=5252 + +EXPOSE 5252 + +ENTRYPOINT ["java", "-jar", "app.jar"] \ No newline at end of file diff --git a/auth-service/build.gradle b/auth-service/build.gradle index 200707b..74fdaae 100644 --- a/auth-service/build.gradle +++ b/auth-service/build.gradle @@ -18,7 +18,7 @@ repositories { } dependencies { - implementation 'org.springframework.boot:spring-boot-starter-session-jdbc' + implementation 'org.springframework.boot:spring-boot-starter-jdbc' implementation 'org.springframework.boot:spring-boot-starter-webmvc' compileOnly 'org.projectlombok:lombok' runtimeOnly 'org.postgresql:postgresql' diff --git a/auth-service/src/main/java/com/dxunvrs/auth_service/controller/AuthController.java b/auth-service/src/main/java/com/dxunvrs/auth_service/controller/AuthController.java new file mode 100644 index 0000000..bbd5c7d --- /dev/null +++ b/auth-service/src/main/java/com/dxunvrs/auth_service/controller/AuthController.java @@ -0,0 +1,34 @@ +package com.dxunvrs.auth_service.controller; + +import com.dxunvrs.auth_service.dto.AuthRequest; +import com.dxunvrs.auth_service.dto.AuthResponse; +import com.dxunvrs.auth_service.service.AuthService; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/auth") +public class AuthController { + private final AuthService authService; + + public AuthController(AuthService authService) { + this.authService = authService; + } + + @PostMapping("/register") + public ResponseEntity register(@RequestBody AuthRequest authRequest) { + String token = authService.register(authRequest.getUsername(), authRequest.getPassword()); + + return ResponseEntity.ok(new AuthResponse(token)); + } + + @PostMapping("/login") + public ResponseEntity login(@RequestBody AuthRequest authRequest) { + String token = authService.login(authRequest.getUsername(), authRequest.getPassword()); + + return ResponseEntity.ok(new AuthResponse(token)); + } +} diff --git a/auth-service/src/main/java/com/dxunvrs/auth_service/dto/AuthRequest.java b/auth-service/src/main/java/com/dxunvrs/auth_service/dto/AuthRequest.java new file mode 100644 index 0000000..bb87dc2 --- /dev/null +++ b/auth-service/src/main/java/com/dxunvrs/auth_service/dto/AuthRequest.java @@ -0,0 +1,9 @@ +package com.dxunvrs.auth_service.dto; + +import lombok.Data; + +@Data +public class AuthRequest { + private String username; + private String password; +} diff --git a/auth-service/src/main/java/com/dxunvrs/auth_service/dto/AuthResponse.java b/auth-service/src/main/java/com/dxunvrs/auth_service/dto/AuthResponse.java new file mode 100644 index 0000000..8312080 --- /dev/null +++ b/auth-service/src/main/java/com/dxunvrs/auth_service/dto/AuthResponse.java @@ -0,0 +1,10 @@ +package com.dxunvrs.auth_service.dto; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class AuthResponse { + private String token; +} diff --git a/auth-service/src/main/java/com/dxunvrs/auth_service/exception/GlobalExceptionHandler.java b/auth-service/src/main/java/com/dxunvrs/auth_service/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..cf5f7d2 --- /dev/null +++ b/auth-service/src/main/java/com/dxunvrs/auth_service/exception/GlobalExceptionHandler.java @@ -0,0 +1,33 @@ +package com.dxunvrs.auth_service.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import java.util.Map; + +@RestControllerAdvice +public class GlobalExceptionHandler { + @ExceptionHandler(InvalidAuthorizeException.class) + public ResponseEntity handleInvalidAuth(InvalidAuthorizeException e) { + return ResponseEntity + .status(HttpStatus.BAD_REQUEST) + .body(Map.of("error", e.getMessage())); + } + + @ExceptionHandler(HttpMessageNotReadableException.class) + public ResponseEntity handleJsonError(HttpMessageNotReadableException e) { + return ResponseEntity + .status(HttpStatus.BAD_REQUEST) + .body(Map.of("error", "Invalid JSON format")); + } + + @ExceptionHandler(Exception.class) + public ResponseEntity handleAll(Exception e) { + return ResponseEntity + .status(HttpStatus.INTERNAL_SERVER_ERROR) + .body(Map.of("error", "Internal server error")); + } +} diff --git a/auth-service/src/main/java/com/dxunvrs/auth_service/service/AuthService.java b/auth-service/src/main/java/com/dxunvrs/auth_service/service/AuthService.java index 7cf76f8..400fbd3 100644 --- a/auth-service/src/main/java/com/dxunvrs/auth_service/service/AuthService.java +++ b/auth-service/src/main/java/com/dxunvrs/auth_service/service/AuthService.java @@ -2,7 +2,6 @@ package com.dxunvrs.auth_service.service; import com.dxunvrs.auth_service.exception.InvalidAuthorizeException; import com.dxunvrs.auth_service.repository.UserDao; -import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; diff --git a/auth-service/src/main/resources/application.properties b/auth-service/src/main/resources/application.properties index c7b12ea..765ede3 100644 --- a/auth-service/src/main/resources/application.properties +++ b/auth-service/src/main/resources/application.properties @@ -1,2 +1,7 @@ spring.application.name=auth-service -auth.jwt.private-key=${JWT_PRIVATE_KEY:MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQClrdBRAtRwvJmfS5DtUZOlp+pY9nWRho5Xgzx61y+/7qm+LtlrZA8iApCJEr9V8ZwkFTywpcxcI2vT3U37QX38zKPL7C/9CILsCd0cdbipdwbnHnB6Ta6uiXNdqDcQTPeg4P5N8zuvyZXYzgro6auEsP5PaZ14FFIX20iu+/71wSyJYFpdB6y0Mvef5wZ13n4tL5mNRYM47WtiURKW3iDXdVAcjQSZ4b1O+sr1KQ2ax4VuAYGGrcH7xHRgxeNJW+w3ZW2Y4Yt4Con2t8Qwi2nvez/YwWOVpS+fFAscxL/XW+HLm9OW12GZossQD+MItBw0NHvggul79/jr0h0amdInAgMBAAECggEAOZnZgCDMvTlE67X4+BlRjNl5R17Ad0zhthlBcK/ZwQ/34dAl9Qk/naJon89tyDKNB5p+lCfAihQii/E4CG+3nqmDFQXTlVRdODUhs6BWCE0CkF3r2wmg/rt46YnvQvKPcVZn86/qA27BczQHZPzAjgx/Dhr5DWN7OyIY/gVMmiH0cys59gWSH3jailOYPIi3k0sSwxkYW3QsKRaC85bCG8V2GA8/vRldJSbgcDOXpwDkNDjUNr1JU7Mb1jyZ/LlV3ec6IaQ+z8+oGyAZCD/A6nYrdVhnme4lCIt1bqjlTI9MQhlsc+V8oyHa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f0Mi23B1fN95mgYZrwORgSl2rC5780G0pC1GM7Zr7Hk3fXkdHTr/mA0pRFZLc7N5/ahAoGAGGpDfYN3buBf0ENh79WuI+p6HMDz2ZSwebHWIvKfjMu/9LfevB4tVKoOeqrn1ufAdKNNo75QBGz3NEGT1fwlzVBAP/FNCQH7Jh+XJkOdr5Fj8egnkkTRFn7d/VlY6UOQlaOdbREhCqz/4A01HmprvUEYc0awoFFIl1qpZDVbUukCgYAlUfdqQd6cfkL+l2w5S3eowlMpl/HULB3LK+/d/kuOT6tdhRVT4JiAefysQKSFUEQ1Qtn9oOo8C86guwTvmfZMaoU+TSPuutFS2pRTgSM1PAMPr3aly5ziWPNf0VA7Rg1BVTMzaAkDM0Px0w2GLJb1GrwZ6j94I43Kxp1FVvRPmA==} +spring.datasource.url=jdbc:postgresql://${DB_HOST:localhost}:${DB_PORT:5430}/${DB_NAME:study} +spring.datasource.username=${DB_USER:roma} +spring.datasource.password=${DB_PASS:admin} +spring.datasource.driver-class-name=org.postgresql.Driver +spring.datasource.hikari.maximum-pool-size=10 \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 3752e4e..99d1cfd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -66,7 +66,7 @@ services: - REDIS_HOST=redis - REDIS_PORT=6379 - server: + product-service: build: context: . dockerfile: product-service/Dockerfile @@ -87,7 +87,21 @@ services: - GATEWAY_SERVER_PORT=1488 stdin_open: true tty: true + + auth-service: + build: auth-service/. + restart: on-failure + ports: + - ":5252" + environment: + - DB_USER=roma + - DB_PASS=admin + - DB_HOST=db + - DB_PORT=5432 + - DB_NAME=study + stdin_open: true + tty: true volumes: pgdata: - redis-data: \ No newline at end of file + redis-data: