From 30ec07c368a0534e25bcb9eb2af3e1dcaaeea575 Mon Sep 17 00:00:00 2001 From: Hyojin Ahn Date: Wed, 19 Nov 2025 13:49:12 -0500 Subject: [PATCH] =?UTF-8?q?-=20Token=20=EC=97=90=20EmpFirstName,=20EmpLast?= =?UTF-8?q?Name=20=EC=B6=94=EA=B0=80=20-=20Global=20Exception=20Handler=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../goi/erp/auth/AuthenticationService.java | 6 ++- .../exception/GlobalExceptionHandler.java | 40 +++++++++++++++++++ .../exception/InvalidPasswordException.java | 9 +++++ .../exception/UserNotFoundException.java | 9 +++++ .../java/com/goi/erp/config/JwtService.java | 12 ++++-- 5 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/goi/erp/common/exception/GlobalExceptionHandler.java create mode 100644 src/main/java/com/goi/erp/common/exception/InvalidPasswordException.java create mode 100644 src/main/java/com/goi/erp/common/exception/UserNotFoundException.java diff --git a/src/main/java/com/goi/erp/auth/AuthenticationService.java b/src/main/java/com/goi/erp/auth/AuthenticationService.java index 6132dc8..3597d92 100644 --- a/src/main/java/com/goi/erp/auth/AuthenticationService.java +++ b/src/main/java/com/goi/erp/auth/AuthenticationService.java @@ -1,6 +1,8 @@ package com.goi.erp.auth; import com.fasterxml.jackson.databind.ObjectMapper; +import com.goi.erp.common.exception.InvalidPasswordException; +import com.goi.erp.common.exception.UserNotFoundException; import com.goi.erp.config.JwtService; import com.goi.erp.token.Token; import com.goi.erp.token.TokenRepository; @@ -49,11 +51,11 @@ public class AuthenticationService { public AuthenticationResponse authenticate(AuthenticationRequest request) { // 1. Employee 조회 Employee employee = employeeRepository.findByEmpLoginId(request.getEmpLoginId()) - .orElseThrow(() -> new RuntimeException("Employee not found")); + .orElseThrow(() -> new UserNotFoundException("Employee not found")); // 2. 비밀번호 검증 if (!passwordEncoder.matches(request.getEmpLoginPassword(), employee.getEmpLoginPassword())) { - throw new RuntimeException("Invalid password"); + throw new InvalidPasswordException("Invalid password"); } // 3. EmployeeRole 조회 → Role 이름 리스트 생성 diff --git a/src/main/java/com/goi/erp/common/exception/GlobalExceptionHandler.java b/src/main/java/com/goi/erp/common/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..3473d48 --- /dev/null +++ b/src/main/java/com/goi/erp/common/exception/GlobalExceptionHandler.java @@ -0,0 +1,40 @@ +package com.goi.erp.common.exception; + +import java.time.LocalDateTime; +import java.util.HashMap; +import java.util.Map; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(RuntimeException.class) + public ResponseEntity handleRuntimeException(RuntimeException ex) { + + Map body = new HashMap<>(); + body.put("error", ex.getMessage()); // ex.getMessage() → "Invalid password" + body.put("timestamp", LocalDateTime.now()); + body.put("status", HttpStatus.BAD_REQUEST.value()); + + return ResponseEntity.badRequest().body(body); + } + + @ExceptionHandler(UserNotFoundException.class) + public ResponseEntity handleUserNotFound(UserNotFoundException ex) { + return ResponseEntity + .status(HttpStatus.NOT_FOUND) + .body(Map.of("error", ex.getMessage())); + } + + @ExceptionHandler(InvalidPasswordException.class) + public ResponseEntity handleInvalidPassword(InvalidPasswordException ex) { + return ResponseEntity + .status(HttpStatus.UNAUTHORIZED) + .body(Map.of("error", ex.getMessage())); + } +} + diff --git a/src/main/java/com/goi/erp/common/exception/InvalidPasswordException.java b/src/main/java/com/goi/erp/common/exception/InvalidPasswordException.java new file mode 100644 index 0000000..ceb1ec5 --- /dev/null +++ b/src/main/java/com/goi/erp/common/exception/InvalidPasswordException.java @@ -0,0 +1,9 @@ +package com.goi.erp.common.exception; + +public class InvalidPasswordException extends RuntimeException { + private static final long serialVersionUID = 1L; + + public InvalidPasswordException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/main/java/com/goi/erp/common/exception/UserNotFoundException.java b/src/main/java/com/goi/erp/common/exception/UserNotFoundException.java new file mode 100644 index 0000000..36edbac --- /dev/null +++ b/src/main/java/com/goi/erp/common/exception/UserNotFoundException.java @@ -0,0 +1,9 @@ +package com.goi.erp.common.exception; + +public class UserNotFoundException extends RuntimeException { + private static final long serialVersionUID = 1L; + + public UserNotFoundException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/main/java/com/goi/erp/config/JwtService.java b/src/main/java/com/goi/erp/config/JwtService.java index 91f9ada..f8c4229 100644 --- a/src/main/java/com/goi/erp/config/JwtService.java +++ b/src/main/java/com/goi/erp/config/JwtService.java @@ -4,7 +4,6 @@ import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import io.jsonwebtoken.io.Decoders; -import io.jsonwebtoken.io.Encoders; import io.jsonwebtoken.security.Keys; import java.security.Key; import java.util.Date; @@ -14,8 +13,6 @@ import java.util.Map; import java.util.function.Function; import org.springframework.beans.factory.annotation.Value; -import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; -import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import com.goi.erp.employee.Employee; @@ -56,6 +53,10 @@ public class JwtService { // 일반 계정이면 상세 권한 넣기 extraClaims.put("permissions", permissions); } + + // 직원 이름 추가 + extraClaims.put("firstName", employee.getEmpFirstName()); + extraClaims.put("lastName", employee.getEmpLastName()); return buildToken(extraClaims, employee.getEmpUuid().toString(), jwtExpiration); } @@ -73,6 +74,11 @@ public class JwtService { // 일반 계정이면 상세 권한 넣기 extraClaims.put("permissions", permissions); } + + // 직원 이름 추가 + extraClaims.put("firstName", employee.getEmpFirstName()); + extraClaims.put("lastName", employee.getEmpLastName()); + return buildToken(extraClaims, employee.getEmpUuid().toString(), refreshExpiration); }