From e74003a0b4d1d97d0b8644f629192202879ab772 Mon Sep 17 00:00:00 2001 From: Hyunkyu Lee <88573971+wken5577@users.noreply.github.com> Date: Thu, 4 Apr 2024 19:56:11 +0900 Subject: [PATCH] =?UTF-8?q?[FIX]=20#806=20auth=20=ED=95=84=EC=9A=94?= =?UTF-8?q?=EC=97=86=EB=8A=94=20=EA=B0=9D=EC=B2=B4=20=EC=82=AD=EC=A0=9C,?= =?UTF-8?q?=20db=EA=B2=80=EC=A6=9D=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= =?UTF-8?q?=20(#807)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../security/config/SecurityConfig.java | 14 --------- .../jwt/utils/TokenAuthenticationFilter.java | 10 ++++-- .../service/CustomUserDetailsService.java | 31 ------------------- 3 files changed, 7 insertions(+), 48 deletions(-) delete mode 100644 gg-pingpong-api/src/main/java/gg/pingpong/api/global/security/service/CustomUserDetailsService.java diff --git a/gg-pingpong-api/src/main/java/gg/pingpong/api/global/security/config/SecurityConfig.java b/gg-pingpong-api/src/main/java/gg/pingpong/api/global/security/config/SecurityConfig.java index 1e00b4326..d79cc9581 100644 --- a/gg-pingpong-api/src/main/java/gg/pingpong/api/global/security/config/SecurityConfig.java +++ b/gg-pingpong-api/src/main/java/gg/pingpong/api/global/security/config/SecurityConfig.java @@ -4,8 +4,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.http.HttpMethod; -import org.springframework.security.authentication.AuthenticationManager; -import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @@ -18,29 +16,17 @@ import gg.pingpong.api.global.security.handler.OAuthAuthenticationSuccessHandler; import gg.pingpong.api.global.security.jwt.utils.TokenAuthenticationFilter; import gg.pingpong.api.global.security.repository.OAuthAuthorizationRequestBasedOnCookieRepository; -import gg.pingpong.api.global.security.service.CustomUserDetailsService; import lombok.RequiredArgsConstructor; @RequiredArgsConstructor @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { - private final CustomUserDetailsService userDetailsService; private final OAuthAuthenticationSuccessHandler oAuth2AuthenticationSuccessHandler; private final CorsProperties corsProperties; private final TokenAuthenticationFilter tokenAuthenticationFilter; private final OAuthAuthorizationRequestBasedOnCookieRepository oAuth2AuthorizationRequestBasedOnCookieRepository; - @Override - protected void configure(AuthenticationManagerBuilder auth) throws Exception { - auth.userDetailsService(userDetailsService); - } - - @Bean - @Override - public AuthenticationManager authenticationManagerBean() throws Exception { - return super.authenticationManagerBean(); - } @Override protected void configure(HttpSecurity http) throws Exception { diff --git a/gg-pingpong-api/src/main/java/gg/pingpong/api/global/security/jwt/utils/TokenAuthenticationFilter.java b/gg-pingpong-api/src/main/java/gg/pingpong/api/global/security/jwt/utils/TokenAuthenticationFilter.java index 151819b4f..9758a9f54 100644 --- a/gg-pingpong-api/src/main/java/gg/pingpong/api/global/security/jwt/utils/TokenAuthenticationFilter.java +++ b/gg-pingpong-api/src/main/java/gg/pingpong/api/global/security/jwt/utils/TokenAuthenticationFilter.java @@ -20,7 +20,9 @@ import gg.auth.utils.AuthTokenProvider; import gg.auth.utils.HeaderUtil; -import gg.pingpong.api.global.security.service.CustomUserDetailsService; +import gg.data.user.User; +import gg.pingpong.api.global.security.UserPrincipal; +import gg.repo.user.UserRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -29,7 +31,7 @@ @Component public class TokenAuthenticationFilter extends OncePerRequestFilter { private final AuthTokenProvider tokenProvider; - private final CustomUserDetailsService customUserDetailsService; + private final UserRepository userRepository; @Override protected void doFilterInternal( @@ -56,7 +58,9 @@ private OAuth2AuthenticationToken validate(HttpServletRequest request) { Long userId = tokenProvider.getUserIdFromAccessToken(accessToken); //access token 검증 if (userId != null) { - UserDetails userDetails = customUserDetailsService.loadUserById(userId); + User loginUser = userRepository.findById(userId) + .orElseThrow(() -> new RuntimeException("user not found in db")); + UserDetails userDetails = UserPrincipal.create(loginUser); return new OAuth2AuthenticationToken((OAuth2User)userDetails, userDetails.getAuthorities(), "42"); } throw new RuntimeException("token not validated"); diff --git a/gg-pingpong-api/src/main/java/gg/pingpong/api/global/security/service/CustomUserDetailsService.java b/gg-pingpong-api/src/main/java/gg/pingpong/api/global/security/service/CustomUserDetailsService.java deleted file mode 100644 index ea0fd803d..000000000 --- a/gg-pingpong-api/src/main/java/gg/pingpong/api/global/security/service/CustomUserDetailsService.java +++ /dev/null @@ -1,31 +0,0 @@ -package gg.pingpong.api.global.security.service; - -import org.springframework.security.core.userdetails.UserDetails; -import org.springframework.security.core.userdetails.UserDetailsService; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import gg.data.user.User; -import gg.pingpong.api.global.security.UserPrincipal; -import gg.pingpong.api.user.user.service.UserFindService; -import gg.utils.exception.user.UserNotFoundException; -import lombok.RequiredArgsConstructor; - -@Service -@RequiredArgsConstructor -@Transactional -public class CustomUserDetailsService implements UserDetailsService { - - private final UserFindService userFindService; - - @Override - public UserDetails loadUserByUsername(String username) throws UserNotFoundException { - User user = userFindService.findByIntraId(username); - return UserPrincipal.create(user); - } - - public UserDetails loadUserById(Long id) { - User user = userFindService.findUserById(id); - return UserPrincipal.create(user); - } -}