From ed10d85cc7cb12a789d0df1f83d79037357f23f1 Mon Sep 17 00:00:00 2001 From: BeommoKoo-dev <95630007+BeommoKoo-dev@users.noreply.github.com> Date: Wed, 1 Nov 2023 17:57:14 +0900 Subject: [PATCH] =?UTF-8?q?NABI-101-refactor=20:=20=EC=BB=A8=EB=B2=A4?= =?UTF-8?q?=EC=85=98=20=EB=A7=9E=EA=B2=8C=20=EB=84=A4=EC=9D=B4=EB=B0=8D,?= =?UTF-8?q?=20=EA=B0=9C=ED=96=89,=20=EC=9D=B8=EB=8D=B4=ED=8A=B8=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/user/dto/UserLoginResponseDTO.java | 9 +++ .../security/jwt/provider/JwtProvider.java | 11 ++++ .../google/api/GoogleOAuth2Controller.java | 7 +- .../oauth2/google/domain/GoogleOAuth2.java | 29 ++++----- .../oauth2/google/domain/OAuth2.java | 12 ++-- .../oauth2/google/domain/Role.java | 3 - ...h2Token.java => GoogleOAuth2TokenDTO.java} | 3 +- ...GoogleUser.java => GoogleUserInfoDTO.java} | 11 +--- .../oauth2/google/dto/OAuth2Attributes.java | 64 ------------------- 9 files changed, 44 insertions(+), 105 deletions(-) rename src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/{GoogleOAuth2Token.java => GoogleOAuth2TokenDTO.java} (89%) rename src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/{GoogleUser.java => GoogleUserInfoDTO.java} (75%) delete mode 100644 src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/OAuth2Attributes.java diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/user/dto/UserLoginResponseDTO.java b/src/main/java/org/prgrms/nabimarketbe/domain/user/dto/UserLoginResponseDTO.java index 78db2db3..4dc82cd1 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/user/dto/UserLoginResponseDTO.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/user/dto/UserLoginResponseDTO.java @@ -1,9 +1,18 @@ package org.prgrms.nabimarketbe.domain.user.dto; +import org.prgrms.nabimarketbe.domain.user.entity.User; import org.prgrms.nabimarketbe.global.security.jwt.dto.TokenResponseDTO; public record UserLoginResponseDTO( UserResponseDto user, TokenResponseDTO token ) { + + public static UserLoginResponseDTO from(User user, TokenResponseDTO tokenResponseDTO) { + UserResponseDto userResponseDto = UserResponseDto.from(user); + + UserLoginResponseDTO response = new UserLoginResponseDTO(userResponseDto, tokenResponseDTO); + + return response; + } } diff --git a/src/main/java/org/prgrms/nabimarketbe/global/security/jwt/provider/JwtProvider.java b/src/main/java/org/prgrms/nabimarketbe/global/security/jwt/provider/JwtProvider.java index a45036ca..82368639 100644 --- a/src/main/java/org/prgrms/nabimarketbe/global/security/jwt/provider/JwtProvider.java +++ b/src/main/java/org/prgrms/nabimarketbe/global/security/jwt/provider/JwtProvider.java @@ -7,7 +7,9 @@ import javax.annotation.PostConstruct; import javax.servlet.http.HttpServletRequest; +import org.prgrms.nabimarketbe.global.security.entity.RefreshToken; import org.prgrms.nabimarketbe.global.security.jwt.dto.TokenResponseDTO; +import org.prgrms.nabimarketbe.oauth2.kakao.repository.RefreshTokenJpaRepo; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; @@ -32,12 +34,17 @@ public class JwtProvider { @Value("spring.jwt.secret") private String secretKey; + private String ROLES = "roles"; + private final Long accessTokenValidMillisecond = 60 * 60 * 1000L; // 1 hour + private final Long refreshTokenValidMillisecond = 14 * 24 * 60 * 60 * 1000L; // 14 day private final UserDetailsService userDetailsService; + private final RefreshTokenJpaRepo refreshTokenJpaRepo; + @PostConstruct protected void init() { // μ•”ν˜Έν™” @@ -67,6 +74,10 @@ public TokenResponseDTO createTokenDto(Long userPk, List roles) { .signWith(SignatureAlgorithm.HS256, secretKey) .compact(); + RefreshToken refreshTokenEntity = new RefreshToken(userPk, refreshToken); + + refreshTokenJpaRepo.save(refreshTokenEntity); + return TokenResponseDTO.builder() .grantType("Bearer") .accessToken(accessToken) diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/api/GoogleOAuth2Controller.java b/src/main/java/org/prgrms/nabimarketbe/oauth2/google/api/GoogleOAuth2Controller.java index ba15352c..3841eea8 100644 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/api/GoogleOAuth2Controller.java +++ b/src/main/java/org/prgrms/nabimarketbe/oauth2/google/api/GoogleOAuth2Controller.java @@ -22,14 +22,14 @@ @Slf4j @RequestMapping("api/v1/users/oauth2/authorize/google") public class GoogleOAuth2Controller { - private final GoogleOAuth2Service oauthService; @GetMapping("/login") public void socialLogin( HttpServletResponse response ) throws IOException { - String requestURL = oauthService.requestRedirectUrl(); + String requestURL = oauthService.requestRedirectUrl(); + response.sendRedirect(requestURL); } @@ -38,8 +38,7 @@ public ResponseEntity callback( @RequestParam(name = "code") String code ) throws JsonProcessingException { UserLoginResponseDTO loginResponseDTO = oauthService.OAuth2Login(code); - log.info("loginResponseDTO : {}", loginResponseDTO); + return ResponseEntity.ok(loginResponseDTO); } - } diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/domain/GoogleOAuth2.java b/src/main/java/org/prgrms/nabimarketbe/oauth2/google/domain/GoogleOAuth2.java index 5e2e14a5..06cfae24 100644 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/domain/GoogleOAuth2.java +++ b/src/main/java/org/prgrms/nabimarketbe/oauth2/google/domain/GoogleOAuth2.java @@ -4,8 +4,8 @@ import java.util.Map; import java.util.stream.Collectors; -import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleOAuth2Token; -import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleUser; +import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleOAuth2TokenDTO; +import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleUserInfoDTO; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; @@ -19,13 +19,10 @@ import com.fasterxml.jackson.databind.ObjectMapper; import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; @Component @RequiredArgsConstructor -@Slf4j public class GoogleOAuth2 implements OAuth2 { - @Value("${spring.security.oauth2.client.registration.google.url}") private String GOOGLE_SNS_LOGIN_URL; @@ -65,7 +62,6 @@ public String getOAuth2RedirectUrl() { .collect(Collectors.joining("&")); String redirectURL = GOOGLE_SNS_LOGIN_URL + "?" + parameterString; - log.info("redirect-URL={}", redirectURL); return redirectURL; } @@ -89,18 +85,16 @@ public ResponseEntity requestAccessToken(String code) { } @Override - public GoogleOAuth2Token getAccessToken(ResponseEntity accessToken) throws JsonProcessingException { - log.info("accessTokenBody: {}",accessToken.getBody()); - - return objectMapper.readValue(accessToken.getBody(), GoogleOAuth2Token.class); + public GoogleOAuth2TokenDTO getAccessToken(ResponseEntity accessToken) throws JsonProcessingException { + return objectMapper.readValue(accessToken.getBody(), GoogleOAuth2TokenDTO.class); } @Override - public ResponseEntity requestUserInfo(GoogleOAuth2Token googleOAuthToken) { + public ResponseEntity requestUserInfo(GoogleOAuth2TokenDTO googleOAuthToken) { HttpHeaders headers = new HttpHeaders(); HttpEntity> request = new HttpEntity<>(headers); - headers.add("Authorization","Bearer " + googleOAuthToken.accessToken()); + headers.add("Authorization", "Bearer " + googleOAuthToken.accessToken()); ResponseEntity exchange = restTemplate.exchange( GOOGLE_USER_INFO_REQUEST_URL, HttpMethod.GET, @@ -108,15 +102,16 @@ public ResponseEntity requestUserInfo(GoogleOAuth2Token googleOAuthToken String.class ); - log.info(exchange.getBody()); - return exchange; } @Override - public GoogleUser parseUserInfo(ResponseEntity userInfoResponse) throws JsonProcessingException { - GoogleUser googleUser = objectMapper.readValue(userInfoResponse.getBody(), GoogleUser.class); + public GoogleUserInfoDTO parseUserInfo(ResponseEntity userInfoResponse) throws JsonProcessingException { + GoogleUserInfoDTO googleUserInfoDTO = objectMapper.readValue( + userInfoResponse.getBody(), + GoogleUserInfoDTO.class + ); - return googleUser; + return googleUserInfoDTO; } } diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/domain/OAuth2.java b/src/main/java/org/prgrms/nabimarketbe/oauth2/google/domain/OAuth2.java index 2aa50b44..8b25537c 100644 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/domain/OAuth2.java +++ b/src/main/java/org/prgrms/nabimarketbe/oauth2/google/domain/OAuth2.java @@ -1,21 +1,19 @@ package org.prgrms.nabimarketbe.oauth2.google.domain; -import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleOAuth2Token; -import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleUser; +import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleOAuth2TokenDTO; +import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleUserInfoDTO; import org.springframework.http.ResponseEntity; import com.fasterxml.jackson.core.JsonProcessingException; public interface OAuth2 { - String getOAuth2RedirectUrl(); ResponseEntity requestAccessToken(String code); - GoogleOAuth2Token getAccessToken(ResponseEntity accessToken) throws JsonProcessingException; - - ResponseEntity requestUserInfo(GoogleOAuth2Token googleOAuthToken); + GoogleOAuth2TokenDTO getAccessToken(ResponseEntity accessToken) throws JsonProcessingException; - GoogleUser parseUserInfo(ResponseEntity userInfo) throws JsonProcessingException; + ResponseEntity requestUserInfo(GoogleOAuth2TokenDTO googleOAuthToken); + GoogleUserInfoDTO parseUserInfo(ResponseEntity userInfo) throws JsonProcessingException; } diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/domain/Role.java b/src/main/java/org/prgrms/nabimarketbe/oauth2/google/domain/Role.java index 9d57feea..901952f2 100644 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/domain/Role.java +++ b/src/main/java/org/prgrms/nabimarketbe/oauth2/google/domain/Role.java @@ -6,11 +6,8 @@ @Getter @RequiredArgsConstructor public enum Role { - GUEST("ROLE_GUEST"), - USER("ROLE_USER"); private final String key; - } diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/GoogleOAuth2Token.java b/src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/GoogleOAuth2TokenDTO.java similarity index 89% rename from src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/GoogleOAuth2Token.java rename to src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/GoogleOAuth2TokenDTO.java index 1fa97a86..2d923a0b 100644 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/GoogleOAuth2Token.java +++ b/src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/GoogleOAuth2TokenDTO.java @@ -2,8 +2,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; -public record GoogleOAuth2Token( - +public record GoogleOAuth2TokenDTO( @JsonProperty("access_token") String accessToken, diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/GoogleUser.java b/src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/GoogleUserInfoDTO.java similarity index 75% rename from src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/GoogleUser.java rename to src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/GoogleUserInfoDTO.java index e303e160..ddfa1dff 100644 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/GoogleUser.java +++ b/src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/GoogleUserInfoDTO.java @@ -4,17 +4,11 @@ import org.prgrms.nabimarketbe.domain.user.entity.User; -public record GoogleUser( +public record GoogleUserInfoDTO( String id, String email, - Boolean verifiedEmail, - String name, - String givenName, - String familyName, - String picture, - String locale + String picture ) { - private static final String PROVIDER = "GOOGLE"; public User toEntity(String nickName) { @@ -23,6 +17,7 @@ public User toEntity(String nickName) { .email(email) .imageUrl(picture) .provider(PROVIDER) + .nameAttributeKey(id) .roles(List.of("ROLE_USER")) .build(); } diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/OAuth2Attributes.java b/src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/OAuth2Attributes.java deleted file mode 100644 index 074abea3..00000000 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/OAuth2Attributes.java +++ /dev/null @@ -1,64 +0,0 @@ -package org.prgrms.nabimarketbe.oauth2.google.dto; - -import java.util.Collections; -import java.util.Map; - -import org.prgrms.nabimarketbe.domain.user.entity.User; -import lombok.Builder; -import lombok.Getter; - -@Getter -public class OAuth2Attributes { - - public Map attributes; - private String nameAttributeKey; - private String name; - private String email; - private String imageUrl; - - @Builder - public OAuth2Attributes( - Map attributes, - String nameAttributeKey, - String name, - String email, - String imageUrl - ) { - this.attributes=attributes; - this.nameAttributeKey=nameAttributeKey; - this.name=name; - this.email=email; - this.imageUrl = imageUrl; - } - - public static OAuth2Attributes of( - String registrationId, - String userNameAttributeName, - Map attributes - ) { - return ofGoogle(userNameAttributeName, attributes); - } - - private static OAuth2Attributes ofGoogle( - String userNameAttributeName, - Map attributes - ) { - return OAuth2Attributes.builder() - .name((String) attributes.get("name")) - .email((String)attributes.get("email")) - .nameAttributeKey(userNameAttributeName) - .attributes(attributes) - .imageUrl((String) attributes.get("picture")) - .build(); - } - - public User toEntity(){ - return User.builder() - .email(email) - .nickname(name) - .imageUrl(imageUrl) - .roles(Collections.singletonList("ROLE_USER")) - .build(); - } - -}