-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor : ArgumentResolver -> Interceptor로 변경 (#200)
* feat : JwtInterceptor 클래스 추가 * refactor : 순환 참조 방지를 위해 메소드 분리 * feat : JwtInterceptor 적용 * refactor : 순환 참조 방지를 위해 메소드 분리 * refactor : 리뷰 조회 경로 변경 * feat : memberId 수정 * refactor : 테스트 코드 수정 * refactor : review 경로 변경 * feat : 메소드의 파라미터에 Long 타입이 있을 때(memberId가 있을 때) jwt 검증하도록 수정 * refactor : 수정했던 경로 복구 * refactor : 관리자용 메소드의 memberId 파라미터 삭제
- Loading branch information
Showing
18 changed files
with
194 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
src/main/java/com/example/sinitto/common/annotation/MemberId.java
This file was deleted.
Oops, something went wrong.
23 changes: 0 additions & 23 deletions
23
src/main/java/com/example/sinitto/common/config/ArgumentResolverConfig.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/com/example/sinitto/common/interceptor/JwtInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.example.sinitto.common.interceptor; | ||
|
||
import com.example.sinitto.common.exception.UnauthorizedException; | ||
import com.example.sinitto.member.service.MemberTokenService; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.method.HandlerMethod; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
@Component | ||
public class JwtInterceptor implements HandlerInterceptor { | ||
private final MemberTokenService memberTokenService; | ||
|
||
public JwtInterceptor(MemberTokenService memberTokenService){ | ||
this.memberTokenService = memberTokenService; | ||
} | ||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){ | ||
if (handler instanceof HandlerMethod) { | ||
HandlerMethod handlerMethod = (HandlerMethod) handler; | ||
Method method = handlerMethod.getMethod(); | ||
|
||
Class<?>[] parameterTypes = method.getParameterTypes(); | ||
|
||
for (Class<?> paramType : parameterTypes) { | ||
if (paramType.equals(Long.class)) { | ||
String authorizationHeader = request.getHeader("Authorization"); | ||
if (authorizationHeader == null || !authorizationHeader.startsWith("Bearer ")) { | ||
throw new UnauthorizedException("토큰이 없거나, 헤더 형식에 맞지 않습니다."); | ||
} | ||
|
||
String token = authorizationHeader.substring(7); | ||
|
||
request.setAttribute("memberId", memberTokenService.getMemberIdByToken(token)); | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
40 changes: 0 additions & 40 deletions
40
src/main/java/com/example/sinitto/common/resolver/MemberIdArgumentResolver.java
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
src/main/java/com/example/sinitto/common/resolver/MemberIdProvider.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.