diff --git a/src/main/java/org/swmaestro/repl/gifthub/auth/service/OAuthService.java b/src/main/java/org/swmaestro/repl/gifthub/auth/service/OAuthService.java index 0c739309..76b8087d 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/auth/service/OAuthService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/auth/service/OAuthService.java @@ -92,7 +92,7 @@ public OAuthUserInfoDto mapToDto(OAuth oAuth) { .id(oAuth.getPlatformId()) .email(oAuth.getEmail()) .nickname(oAuth.getNickname()) - .Provider(oAuth.getPlatform().toString()) + .Provider(oAuth.getPlatform().toString().toLowerCase()) .build(); } } diff --git a/src/main/java/org/swmaestro/repl/gifthub/auth/service/UserService.java b/src/main/java/org/swmaestro/repl/gifthub/auth/service/UserService.java index 18a30dbf..8588d546 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/auth/service/UserService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/auth/service/UserService.java @@ -74,14 +74,14 @@ public boolean isValidatePassword(String password) { public User read(String username) { User user = userRepository.findByUsername(username); if (user == null) { - return null; + throw new BusinessException("존재하지 않는 회원입니다.", StatusEnum.NOT_FOUND); } return user; } public UserReadResponseDto read(Long id) { Optional user = userRepository.findById(id); - if (user.isEmpty() || user.get().getDeletedAt() != null) { + if (user.isEmpty() || !user.get().isEnabled()) { throw new BusinessException("존재하지 않는 회원입니다.", StatusEnum.NOT_FOUND); } return UserReadResponseDto.builder() diff --git a/src/main/java/org/swmaestro/repl/gifthub/filter/JwtAuthenticationFilter.java b/src/main/java/org/swmaestro/repl/gifthub/filter/JwtAuthenticationFilter.java index c3955f59..8d9d7d81 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/filter/JwtAuthenticationFilter.java +++ b/src/main/java/org/swmaestro/repl/gifthub/filter/JwtAuthenticationFilter.java @@ -1,33 +1,54 @@ package org.swmaestro.repl.gifthub.filter; -import jakarta.servlet.FilterChain; -import jakarta.servlet.ServletException; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import lombok.RequiredArgsConstructor; +import java.io.IOException; + import org.springframework.core.annotation.Order; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Component; import org.springframework.web.filter.OncePerRequestFilter; +import org.swmaestro.repl.gifthub.util.ErrorMessage; import org.swmaestro.repl.gifthub.util.JwtProvider; +import org.swmaestro.repl.gifthub.util.StatusEnum; -import java.io.IOException; +import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import lombok.RequiredArgsConstructor; @Order(0) @Component @RequiredArgsConstructor public class JwtAuthenticationFilter extends OncePerRequestFilter { private final JwtProvider jwtProvider; + private final ObjectMapper objectMapper = new ObjectMapper(); @Override - protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException, + UsernameNotFoundException { String token = jwtProvider.resolveToken(request); if (token != null && jwtProvider.validateToken(token)) { token = token.substring(7); - Authentication auth = jwtProvider.getAuthentication(token); - SecurityContextHolder.getContext().setAuthentication(auth); + try { + Authentication auth = jwtProvider.getAuthentication(token); + SecurityContextHolder.getContext().setAuthentication(auth); + } catch (UsernameNotFoundException e) { + e.printStackTrace(); + response.setStatus(401); + response.setCharacterEncoding("utf-8"); + response.setContentType("application/json"); + response.getWriter().write(objectMapper.writeValueAsString( + ErrorMessage.builder() + .status(StatusEnum.UNAUTHORIZED.statusCode) + .path(request.getRequestURI()) + .error("탈퇴한 회원입니다.") + .build())); + return; + } } filterChain.doFilter(request, response); } diff --git a/src/main/java/org/swmaestro/repl/gifthub/util/JwtProvider.java b/src/main/java/org/swmaestro/repl/gifthub/util/JwtProvider.java index 087b003a..cc790c09 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/util/JwtProvider.java +++ b/src/main/java/org/swmaestro/repl/gifthub/util/JwtProvider.java @@ -10,6 +10,7 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Component; import org.swmaestro.repl.gifthub.auth.repository.RefreshTokenRepository; import org.swmaestro.repl.gifthub.auth.service.UserService; @@ -96,6 +97,9 @@ public String resolveToken(HttpServletRequest request) { */ public Authentication getAuthentication(String token) { UserDetails userDetails = userService.loadUserByUsername(this.getUsername(token)); + if (userDetails == null) { + throw new UsernameNotFoundException("존재하지 않는 회원입니다."); + } return new UsernamePasswordAuthenticationToken(userDetails, "", userDetails.getAuthorities()); }