-
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.
feat: 로그아웃시 사용자 정보를 받기 위한 로그인 리졸버 추가 (#13)
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/main/java/com/backend/util/LoginUserArgumentResolver.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,37 @@ | ||
package com.backend.util; | ||
|
||
import com.backend.domain.auth.dto.Login; | ||
import com.backend.domain.auth.dto.LoginUser; | ||
import com.backend.error.ErrorCode; | ||
import com.backend.error.exception.custom.BusinessException; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
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; | ||
|
||
public class LoginUserArgumentResolver implements HandlerMethodArgumentResolver { | ||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
boolean hasLoginAnnotation = parameter.hasParameterAnnotation(Login.class); | ||
boolean hasLoginUserType = LoginUser.class.isAssignableFrom(parameter.getParameterType()); | ||
|
||
return hasLoginAnnotation && hasLoginUserType; | ||
} | ||
|
||
@Override | ||
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, | ||
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
|
||
if (authentication == null) { | ||
throw new BusinessException(ErrorCode.USER_NOT_FOUND); | ||
} | ||
|
||
return LoginUser.builder() | ||
.email(authentication.getName()) | ||
.build(); | ||
} | ||
} |