Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor : ArgumentResolver -> Interceptor로 변경 #200

Merged
merged 12 commits into from
Nov 8, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,6 @@ public CorsFilter corsFilter() {

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(jwtInterceptor).addPathPatterns("/api/**")
.excludePathPatterns("/api/auth/**")
.excludePathPatterns("/api/reviews")
.excludePathPatterns("/api/members/sinitto")
.excludePathPatterns("/api/members/guard")
.excludePathPatterns("/api/reviews")
.excludePathPatterns("/api/callbacks/twilio")
.excludePathPatterns("/api/hellocalls/admin/reports");
registry.addInterceptor(jwtInterceptor).addPathPatterns("/api/**");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
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;
Expand All @@ -16,14 +19,26 @@ public JwtInterceptor(MemberTokenService memberTokenService){
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
String authorizationHeader = request.getHeader("Authorization");
if (authorizationHeader == null || !authorizationHeader.startsWith("Bearer ")) {
throw new UnauthorizedException("토큰이 없거나, 헤더 형식에 맞지 않습니다.");
}
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();

Class<?>[] parameterTypes = method.getParameterTypes();

String token = authorizationHeader.substring(7);
for (Class<?> paramType : parameterTypes) {
if (paramType.equals(Long.class)) {
String authorizationHeader = request.getHeader("Authorization");
if (authorizationHeader == null || !authorizationHeader.startsWith("Bearer ")) {
throw new UnauthorizedException("토큰이 없거나, 헤더 형식에 맞지 않습니다.");
}
Comment on lines +29 to +33
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


request.setAttribute("memberId", memberTokenService.getMemberIdByToken(token));
String token = authorizationHeader.substring(7);

request.setAttribute("memberId", memberTokenService.getMemberIdByToken(token));
return true;
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 이해했습니다. 오 이런방식이 ㅎ.ㄷ


return true;
}
Expand Down