Skip to content

Commit

Permalink
feat: 로그아웃시 사용자 정보를 받기 위한 로그인 리졸버 추가 (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdohyung committed Nov 16, 2023
1 parent b161566 commit 8a2e672
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/main/java/com/backend/util/LoginUserArgumentResolver.java
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();
}
}

0 comments on commit 8a2e672

Please sign in to comment.