- Token 에 EmpFirstName, EmpLastName 추가

- Global Exception Handler 추가
This commit is contained in:
Hyojin Ahn 2025-11-19 13:49:12 -05:00
parent 3d6a6b5cc6
commit 30ec07c368
5 changed files with 71 additions and 5 deletions

View File

@ -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 이름 리스트 생성

View File

@ -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<String, Object> 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()));
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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;
@ -57,6 +54,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);
}