Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.petmatz.api.global.exception;

import com.petmatz.api.global.dto.Response;
import com.petmatz.common.exception.BaseErrorCode;
import com.petmatz.common.exception.ErrorReason;
import com.petmatz.domain.user.exception.UserErrorCode;
import com.petmatz.domain.user.exception.UserException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(UserException.class)
public ResponseEntity<Response<Void>> handleUserException(UserException ex) {
// 예외에서 에러 코드 가져오기
BaseErrorCode baseErrorCode = ex.getErrorCode();
ErrorReason errorReason = baseErrorCode.getErrorReason();

int statusCode = errorReason.status();
String message = errorReason.message();
String errorCode2 = errorReason.errorCode();

// 실패 응답 생성
Response<Void> response = Response.error(errorCode2, message);

// 상태 코드와 함께 응답 반환
return new ResponseEntity<>(response, HttpStatus.valueOf(statusCode));
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/petmatz/api/user/controller/JwtController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.petmatz.api.user.controller;

import com.petmatz.api.global.dto.Response;
import com.petmatz.common.security.jwt.JwtManager;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseCookie;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
public class JwtController {

private final JwtManager jwtManager;

@PostMapping("/token/reissue")
public Response<Void> reissueAccessToken(HttpServletResponse response, String refreshToken) {
jwtManager.refreshAccessToken(response, refreshToken);
return Response.success();
}
}
149 changes: 0 additions & 149 deletions src/main/java/com/petmatz/api/user/controller/PastUserController.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.petmatz.common.security.filter;

import com.petmatz.common.security.jwt.JwtManager;
import com.petmatz.common.security.jwt.JwtProvider;
import com.petmatz.domain.user.constant.LoginRole;
import com.petmatz.domain.user.entity.User;
Expand Down Expand Up @@ -34,7 +35,7 @@
@RequiredArgsConstructor
public class JwtAuthenticationFilter extends OncePerRequestFilter {
private final UserRepository userRepository;
private final JwtProvider jwtProvider;
private final JwtManager jwtManager;

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
Expand All @@ -48,7 +49,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
}

// JWT 유효성 검증 및 사용자 ID 추출
Long userId = jwtProvider.validateAndGetUserId(token); // validate 메서드가 userId를 반환하도록 수정
Long userId = jwtManager.validateAndGetUserId(token); // validate 메서드가 userId를 반환하도록 수정
if (userId == null) {
filterChain.doFilter(request, response);
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.petmatz.common.security.handler;

import com.petmatz.common.security.jwt.JwtManager;
import com.petmatz.common.security.jwt.JwtProvider;
import com.petmatz.domain.user.entity.CustomOAuthUser;
import com.petmatz.domain.user.entity.User;
Expand All @@ -19,7 +20,7 @@
@RequiredArgsConstructor
public class OAuthSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

private final JwtProvider jwtProvider;
private final JwtManager jwtManager;
private final UserRepository userRepository;
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
Expand All @@ -30,7 +31,7 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
String accountId = oAuth2User.getName();

// JWT 생성
String token = jwtProvider.create(userId, accountId);
String token = jwtManager.createAccessToken(userId, accountId);

// JWT 쿠키 설정
ResponseCookie jwtCookie = ResponseCookie.from("jwt", token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

public interface JwtExtractProvider {
Long findIdFromJwt();
String findAccountIdFromJwt();
String findAccountIdFromJwt(); // Email 로도 사용
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.petmatz.common.security.jwt;

import com.petmatz.domain.user.repository.UserRepository;
import com.petmatz.infra.redis.component.RedisTokenComponent;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
Expand All @@ -13,7 +14,7 @@
@RequiredArgsConstructor
public class JwtExtractProviderImpl implements JwtExtractProvider {

private final JwtProvider jwtProvider; // JWT를 검증하고 ID를 추출하는 클래스
private final JwtManager jwtManager; // JWT를 검증하고 ID를 추출하는 클래스
private final UserRepository userRepository;

@Override
Expand All @@ -33,7 +34,7 @@ public Long findIdFromJwt() {
return (Long) principal; // Principal이 Long 타입인 경우 직접 반환
} else if (principal instanceof String) {
// Principal이 String인 경우 JWT에서 ID 추출
return jwtProvider.validateAndGetUserId((String) principal);
return jwtManager.validateAndGetUserId((String) principal);
} else {
throw new IllegalArgumentException("Invalid principal type: " + principal.getClass().getName());
}
Expand Down Expand Up @@ -63,7 +64,8 @@ public String findAccountIdFromJwt() {
return userRepository.findAccountIdByUserId(userId); // Repository 메서드 사용
} else if (principal instanceof String) {
// Principal이 String 타입인 경우 JWT로 간주하고 accountId 추출
Map<String, Object> claims = jwtProvider.validate((String) principal);
Map<String, Object> claims = jwtManager.validate((String) principal);

if (claims != null && claims.containsKey("accountId")) {
return (String) claims.get("accountId");
}
Expand Down
Loading