diff --git a/src/main/java/org/withtime/be/withtimebe/domain/auth/service/command/AuthCommandServiceImpl.java b/src/main/java/org/withtime/be/withtimebe/domain/auth/service/command/AuthCommandServiceImpl.java index cdd75fd..aaa3057 100644 --- a/src/main/java/org/withtime/be/withtimebe/domain/auth/service/command/AuthCommandServiceImpl.java +++ b/src/main/java/org/withtime/be/withtimebe/domain/auth/service/command/AuthCommandServiceImpl.java @@ -39,7 +39,7 @@ public class AuthCommandServiceImpl implements AuthCommandService { public void signUp(AuthRequestDTO.SignUp request) { validateSignUp(request); - Member member = memberRepository.save(AuthConverter.toLocalMember(request.email(), request.username(), passwordEncoder.encode(request.password()), request.phoneNumber(), request.gender(), request.birth())); + Member member = memberRepository.save(AuthConverter.toLocalMember(request.email(), request.username(), request.socialId() != null ? passwordEncoder.encode(request.password()) : null, request.phoneNumber(), request.gender(), request.birth())); if (request.socialId() != null) { Social social = socialRepository.findById(request.socialId()).orElseThrow(() -> new SocialException(SocialErrorCode.NOT_FOUND_SOCIAL)); diff --git a/src/main/java/org/withtime/be/withtimebe/global/error/code/AuthErrorCode.java b/src/main/java/org/withtime/be/withtimebe/global/error/code/AuthErrorCode.java index 36ae815..dc0d7e9 100644 --- a/src/main/java/org/withtime/be/withtimebe/global/error/code/AuthErrorCode.java +++ b/src/main/java/org/withtime/be/withtimebe/global/error/code/AuthErrorCode.java @@ -8,8 +8,10 @@ @AllArgsConstructor public enum AuthErrorCode implements BaseErrorCode { + NOT_FOUND_LOGIN_MEMBER(HttpStatus.NOT_FOUND, "AUTH404_1", "해당 이메일을 찾을 수 없습니다."), FAIL_AUTH_LOGIN(HttpStatus.UNAUTHORIZED, "AUTH401_1", "일반 로그인에 실패했습니다."), - ALREADY_EXIST_EMAIL(HttpStatus.BAD_REQUEST, "AUTH400_1", "이미 존재하는 이메일입니다.") + ALREADY_EXIST_EMAIL(HttpStatus.BAD_REQUEST, "AUTH400_1", "이미 존재하는 이메일입니다."), + ONLY_AVAILABLE_SOCIAL(HttpStatus.BAD_REQUEST, "AUTH400_2", "소셜 로그인만 가능합니다.") ; private final HttpStatus httpStatus; private final String code; diff --git a/src/main/java/org/withtime/be/withtimebe/global/security/filter/JsonLoginFilter.java b/src/main/java/org/withtime/be/withtimebe/global/security/filter/JsonLoginFilter.java index fcf343d..8662dd5 100644 --- a/src/main/java/org/withtime/be/withtimebe/global/security/filter/JsonLoginFilter.java +++ b/src/main/java/org/withtime/be/withtimebe/global/security/filter/JsonLoginFilter.java @@ -8,7 +8,10 @@ import jakarta.servlet.http.HttpServletResponse; import lombok.Getter; import lombok.RequiredArgsConstructor; +import org.namul.api.payload.code.BaseErrorCode; +import org.namul.api.payload.code.DefaultResponseErrorCode; import org.namul.api.payload.code.dto.supports.DefaultResponseErrorReasonDTO; +import org.namul.api.payload.error.exception.ServerApplicationException; import org.namul.api.payload.writer.FailureResponseWriter; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; @@ -51,6 +54,8 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse return; } this.successfulAuthentication(request, response, authentication); + } catch (ServerApplicationException e) { + handleServerApplicationException(response, e); } catch (Exception e) { handleException(response, e); } @@ -70,7 +75,8 @@ public Authentication attemptAuthentication(HttpServletRequest request) throws A } catch (IOException e) { throw new AuthenticationServiceException("Json Parsing Error In Json Filter"); } catch (Exception e) { - throw new AuthenticationServiceException("CustomJsonUsernamePasswordLoginFilter(" + e.getClass() + "): " + e.getMessage()); + Throwable throwable = e.getCause(); + throw throwable instanceof ServerApplicationException serverApplicationException ? serverApplicationException : new AuthenticationServiceException("CustomJsonUsernamePasswordLoginFilter(" + e.getClass() + "): " + e.getMessage()); } } @@ -93,6 +99,14 @@ private AuthRequestDTO.Login getBodyInRequest(HttpServletRequest request) throws return om.readValue(content, AuthRequestDTO.Login.class); } + private void handleServerApplicationException(HttpServletResponse response, ServerApplicationException e) throws IOException { + ObjectMapper om = new ObjectMapper(); + DefaultResponseErrorReasonDTO reasonDTO = e.getCode().getReason() instanceof DefaultResponseErrorReasonDTO defaultResponseErrorReasonDTO ? defaultResponseErrorReasonDTO : DefaultResponseErrorCode._UNAUTHORIZED.getReason(); + response.setStatus(reasonDTO.getHttpStatus().value()); + response.setContentType(MediaType.APPLICATION_JSON_VALUE); + om.writeValue(response.getOutputStream(), failureResponseWriter.onFailure(reasonDTO, e.getMessage())); + } + private void handleException(HttpServletResponse response, Exception e) throws IOException { ObjectMapper om = new ObjectMapper(); DefaultResponseErrorReasonDTO reasonDTO = AuthErrorCode.FAIL_AUTH_LOGIN.getReason(); diff --git a/src/main/java/org/withtime/be/withtimebe/global/security/service/CustomUserDetailsService.java b/src/main/java/org/withtime/be/withtimebe/global/security/service/CustomUserDetailsService.java index f9ab984..f0fb89d 100644 --- a/src/main/java/org/withtime/be/withtimebe/global/security/service/CustomUserDetailsService.java +++ b/src/main/java/org/withtime/be/withtimebe/global/security/service/CustomUserDetailsService.java @@ -7,8 +7,8 @@ import org.springframework.stereotype.Component; import org.withtime.be.withtimebe.domain.member.entity.Member; import org.withtime.be.withtimebe.domain.member.repository.MemberRepository; -import org.withtime.be.withtimebe.global.error.code.MemberErrorCode; -import org.withtime.be.withtimebe.global.error.exception.MemberException; +import org.withtime.be.withtimebe.global.error.code.AuthErrorCode; +import org.withtime.be.withtimebe.global.error.exception.AuthException; import org.withtime.be.withtimebe.global.security.domain.CustomUserDetails; @Component @@ -19,7 +19,10 @@ public class CustomUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException { - Member member = memberRepository.findByEmail(email).orElseThrow(() -> new MemberException(MemberErrorCode.NOT_FOUND)); + Member member = memberRepository.findByEmail(email).orElseThrow(() -> new AuthException(AuthErrorCode.NOT_FOUND_LOGIN_MEMBER)); + if (member.getPassword() == null) { + throw new AuthException(AuthErrorCode.ONLY_AVAILABLE_SOCIAL); + } return new CustomUserDetails(member); } }