-
Notifications
You must be signed in to change notification settings - Fork 578
Expand file tree
/
Copy pathAuthUserArgumentResolver.java
More file actions
57 lines (48 loc) · 2.68 KB
/
AuthUserArgumentResolver.java
File metadata and controls
57 lines (48 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package org.example.expert.config;
import jakarta.servlet.http.HttpServletRequest;
import org.example.expert.domain.auth.exception.AuthException;
import org.example.expert.domain.common.annotation.Auth;
import org.example.expert.domain.common.dto.AuthUser;
import org.example.expert.domain.user.enums.UserRole;
import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
// 어노테이션 추가
@Component
public class AuthUserArgumentResolver implements HandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(MethodParameter parameter) {
boolean hasAuthAnnotation = parameter.getParameterAnnotation(Auth.class) != null;
boolean isAuthUserType = parameter.getParameterType().equals(AuthUser.class);
// @Auth 어노테이션과 AuthUser 타입이 함께 사용되지 않은 경우 예외 발생
if (hasAuthAnnotation != isAuthUserType) {
throw new AuthException("@Auth와 AuthUser 타입은 함께 사용되어야 합니다.");
}
return hasAuthAnnotation;
}
@Override
public Object resolveArgument(
@Nullable MethodParameter parameter,
@Nullable ModelAndViewContainer mavContainer,
NativeWebRequest webRequest,
@Nullable WebDataBinderFactory binderFactory
) {
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
// JwtFilter 에서 set 한 userId, email, userRole 값을 가져옴
Long userId = (Long) request.getAttribute("userId");
String email = (String) request.getAttribute("email");
UserRole userRole = UserRole.of((String) request.getAttribute("userRole"));
return new AuthUser(userId, email, userRole);
}
}
/**
* AuthUserArgumentResolver의 역할:
* * 1. 전달자 역할: JwtFilter에서 HttpServletRequest에 저장한 유저 정보(ID, 이메일, 권한)를 꺼내옵니다.
* 2. 객체 변환: 꺼낸 정보들을 컨트롤러가 바로 사용할 수 있도록 AuthUser 객체로 변환(포장)합니다.
* 3. 자동 주입: 컨트롤러 메서드의 파라미터에 @Auth 어노테이션이 있으면, 생성된 AuthUser 객체를 자동으로 넘겨줍니다.
* * 결과적으로 컨트롤러에서 복잡한 로직 없이 유저 정보를 '전달'받아 바로 쓸 수 있게 해주는 고마운 배달부입니다!
*/