From 9e46e5b5e1a273ac1a226c5f35a61de3df6986cb Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Sun, 22 Oct 2023 00:42:10 +0900 Subject: [PATCH 1/5] fix: to lowercase letter --- .../org/swmaestro/repl/gifthub/auth/service/OAuthService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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(); } } From cdf520b8ff51261bd04a58406cd74d0a75248ff7 Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Sun, 22 Oct 2023 00:47:37 +0900 Subject: [PATCH 2/5] fix: 500 error to 404 error --- .../swmaestro/repl/gifthub/auth/service/UserService.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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..4aec599f 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 @@ -73,15 +73,15 @@ public boolean isValidatePassword(String password) { public User read(String username) { User user = userRepository.findByUsername(username); - if (user == null) { - return null; + if (user == null || !user.isEnabled()) { + 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() @@ -164,7 +164,7 @@ public OAuth deleteOAuthInfo(User user, OAuthPlatform oAuthPlatform) { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { - return userRepository.findByUsername(username).getDeletedAt() == null ? userRepository.findByUsername(username) : null; + return userRepository.findByUsername(username); } public List deleteOAuthInfo(User user) { From 0ce15c6c04cb140298d8ad657423ab78e83385d9 Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Sun, 22 Oct 2023 16:46:53 +0900 Subject: [PATCH 3/5] fix: load-user-by-username method to original --- .../org/swmaestro/repl/gifthub/auth/service/UserService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 4aec599f..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 @@ -73,7 +73,7 @@ public boolean isValidatePassword(String password) { public User read(String username) { User user = userRepository.findByUsername(username); - if (user == null || !user.isEnabled()) { + if (user == null) { throw new BusinessException("존재하지 않는 회원입니다.", StatusEnum.NOT_FOUND); } return user; @@ -164,7 +164,7 @@ public OAuth deleteOAuthInfo(User user, OAuthPlatform oAuthPlatform) { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { - return userRepository.findByUsername(username); + return userRepository.findByUsername(username).getDeletedAt() == null ? userRepository.findByUsername(username) : null; } public List deleteOAuthInfo(User user) { From 7dee46926fc1bd179919326d4fd58f3d0a4dff59 Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Sun, 22 Oct 2023 16:47:51 +0900 Subject: [PATCH 4/5] fix: throw user-name-not-found-exception-error --- .../java/org/swmaestro/repl/gifthub/util/JwtProvider.java | 4 ++++ 1 file changed, 4 insertions(+) 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()); } From d7bbcf13b8fb3412e7f95b585be718174919242f Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Sun, 22 Oct 2023 16:48:38 +0900 Subject: [PATCH 5/5] fix: user-name-not-found exception-handling --- .../filter/JwtAuthenticationFilter.java | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) 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); }