From 784324809355fa4ffcebc8b2347d41364b18d3dc Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Wed, 6 Mar 2024 22:38:57 +0900 Subject: [PATCH 01/23] =?UTF-8?q?feat=20:=20=EC=BF=A0=ED=82=A4=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=EC=B1=85=EC=9E=84=EC=9D=84=20=EA=B0=80=EC=A7=84=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../support/AuthCookieGenerator.java | 36 +++++++++++++++++++ .../presentation/support/CookieGenerator.java | 7 ++++ 2 files changed, 43 insertions(+) create mode 100644 BE/eeos/src/main/java/com/blackcompany/eeos/auth/presentation/support/AuthCookieGenerator.java create mode 100644 BE/eeos/src/main/java/com/blackcompany/eeos/common/presentation/support/CookieGenerator.java diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/presentation/support/AuthCookieGenerator.java b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/presentation/support/AuthCookieGenerator.java new file mode 100644 index 000000000..e1606520b --- /dev/null +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/presentation/support/AuthCookieGenerator.java @@ -0,0 +1,36 @@ +package com.blackcompany.eeos.auth.presentation.support; + +import com.blackcompany.eeos.common.presentation.support.CookieGenerator; +import com.blackcompany.eeos.common.utils.TimeUtil; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.ResponseCookie; +import org.springframework.stereotype.Component; + +@Component +public class AuthCookieGenerator implements CookieGenerator { + + private static final Boolean HTTP_ONLY = true; + private static final Boolean SECURE = true; + private static final String SAMESITE = "None"; + + @Value("${token.cookie.domain}") + private String domain; + + @Value("${token.cookie.path}") + private String path; + + @Value("${security.jwt.refresh.validTime}") + private Long validTime; + + @Override + public ResponseCookie createCookie(String key, String value) { + return ResponseCookie.from(key, value) + .path(path) + .domain(domain) + .httpOnly(HTTP_ONLY) + .secure(SECURE) + .sameSite(SAMESITE) + .maxAge(TimeUtil.convertSecondsFromMillis(validTime)) + .build(); + } +} diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/common/presentation/support/CookieGenerator.java b/BE/eeos/src/main/java/com/blackcompany/eeos/common/presentation/support/CookieGenerator.java new file mode 100644 index 000000000..05998f0e0 --- /dev/null +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/common/presentation/support/CookieGenerator.java @@ -0,0 +1,7 @@ +package com.blackcompany.eeos.common.presentation.support; + +import org.springframework.http.ResponseCookie; + +public interface CookieGenerator { + ResponseCookie createCookie(String key, String value); +} From b205ddd3c034efb1398ea331dfaf0e7282feed99 Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Fri, 8 Mar 2024 01:00:53 +0900 Subject: [PATCH 02/23] =?UTF-8?q?refactor=20:=20Cookie=20=EA=B4=80?= =?UTF-8?q?=EB=A6=AC=ED=95=98=EB=8A=94=20=EC=B1=85=EC=9E=84=EC=9D=84=20?= =?UTF-8?q?=EA=B0=80=EC=A7=84=20=ED=81=B4=EB=9E=98=EC=8A=A4=EB=AA=85=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{AuthCookieGenerator.java => AuthCookieManager.java} | 4 ++-- .../support/{CookieGenerator.java => CookieManager.java} | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) rename BE/eeos/src/main/java/com/blackcompany/eeos/auth/presentation/support/{AuthCookieGenerator.java => AuthCookieManager.java} (86%) rename BE/eeos/src/main/java/com/blackcompany/eeos/common/presentation/support/{CookieGenerator.java => CookieManager.java} (82%) diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/presentation/support/AuthCookieGenerator.java b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/presentation/support/AuthCookieManager.java similarity index 86% rename from BE/eeos/src/main/java/com/blackcompany/eeos/auth/presentation/support/AuthCookieGenerator.java rename to BE/eeos/src/main/java/com/blackcompany/eeos/auth/presentation/support/AuthCookieManager.java index e1606520b..7680d9102 100644 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/presentation/support/AuthCookieGenerator.java +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/presentation/support/AuthCookieManager.java @@ -1,13 +1,13 @@ package com.blackcompany.eeos.auth.presentation.support; -import com.blackcompany.eeos.common.presentation.support.CookieGenerator; +import com.blackcompany.eeos.common.presentation.support.CookieManager; import com.blackcompany.eeos.common.utils.TimeUtil; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.ResponseCookie; import org.springframework.stereotype.Component; @Component -public class AuthCookieGenerator implements CookieGenerator { +public class AuthCookieManager implements CookieManager { private static final Boolean HTTP_ONLY = true; private static final Boolean SECURE = true; diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/common/presentation/support/CookieGenerator.java b/BE/eeos/src/main/java/com/blackcompany/eeos/common/presentation/support/CookieManager.java similarity index 82% rename from BE/eeos/src/main/java/com/blackcompany/eeos/common/presentation/support/CookieGenerator.java rename to BE/eeos/src/main/java/com/blackcompany/eeos/common/presentation/support/CookieManager.java index 05998f0e0..d52ac4ded 100644 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/common/presentation/support/CookieGenerator.java +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/common/presentation/support/CookieManager.java @@ -2,6 +2,6 @@ import org.springframework.http.ResponseCookie; -public interface CookieGenerator { +public interface CookieManager { ResponseCookie createCookie(String key, String value); } From e8188b0c7dea03843d1a3124c0d44c69d334c6ff Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Fri, 8 Mar 2024 01:02:01 +0900 Subject: [PATCH 03/23] =?UTF-8?q?feat=20:=20Auth=20=EA=B4=80=EB=A0=A8=20?= =?UTF-8?q?=EC=83=81=EC=88=98=20=EA=B4=80=EB=A6=AC=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EC=B1=85=EC=9E=84=EC=9D=84=20=EA=B0=80=EC=A7=84=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../eeos/auth/presentation/support/AuthConstants.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 BE/eeos/src/main/java/com/blackcompany/eeos/auth/presentation/support/AuthConstants.java diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/presentation/support/AuthConstants.java b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/presentation/support/AuthConstants.java new file mode 100644 index 000000000..977f954c5 --- /dev/null +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/presentation/support/AuthConstants.java @@ -0,0 +1,5 @@ +package com.blackcompany.eeos.auth.presentation.support; + +public class AuthConstants { + public static final String TOKEN_KEY = "eeos_token"; +} From 86239e48c0614e678683a33879c6546b1a61a516 Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Fri, 8 Mar 2024 01:02:59 +0900 Subject: [PATCH 04/23] =?UTF-8?q?refactor=20:=20=EC=BF=A0=ED=82=A4=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=EC=9D=84=20CookieManager=EA=B0=80=20?= =?UTF-8?q?=EC=B1=85=EC=9E=84=EC=A7=80=EB=8F=84=EB=A1=9D=20=EB=A9=94?= =?UTF-8?q?=EC=8B=9C=EC=A7=80=20=EC=A0=84=EB=8B=AC=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AuthController.java | 36 ++++++------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/presentation/controller/AuthController.java b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/presentation/controller/AuthController.java index f49b8e5fb..b0efde529 100644 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/presentation/controller/AuthController.java +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/presentation/controller/AuthController.java @@ -5,16 +5,17 @@ import com.blackcompany.eeos.auth.application.dto.response.TokenResponse; import com.blackcompany.eeos.auth.application.usecase.LoginUsecase; import com.blackcompany.eeos.auth.application.usecase.ReissueUsecase; +import com.blackcompany.eeos.auth.presentation.support.AuthConstants; import com.blackcompany.eeos.auth.presentation.support.TokenExtractor; import com.blackcompany.eeos.common.presentation.respnose.ApiResponse; import com.blackcompany.eeos.common.presentation.respnose.ApiResponseBody.SuccessBody; import com.blackcompany.eeos.common.presentation.respnose.ApiResponseGenerator; import com.blackcompany.eeos.common.presentation.respnose.MessageCode; -import com.blackcompany.eeos.common.utils.TimeUtil; +import com.blackcompany.eeos.common.presentation.support.CookieManager; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseCookie; import org.springframework.web.bind.annotation.PathVariable; @@ -29,26 +30,20 @@ public class AuthController { private final LoginUsecase loginUsecase; private final ReissueUsecase reissueUsecase; private final TokenExtractor tokenExtractor; + private final CookieManager cookieManager; private final TokenResponseConverter tokenResponseConverter; - private final String cookieKey; - private final String domain; - private final long validTime; public AuthController( LoginUsecase loginUsecase, ReissueUsecase reissueUsecase, @Qualifier("cookie") TokenExtractor tokenExtractor, TokenResponseConverter tokenResponseConverter, - @Value("${api.cookie-key}") String cookieKey, - @Value("${api.domain}") String domain, - @Value("${security.jwt.refresh.validTime}") long validTime) { + CookieManager cookieManager) { this.loginUsecase = loginUsecase; this.reissueUsecase = reissueUsecase; this.tokenExtractor = tokenExtractor; this.tokenResponseConverter = tokenResponseConverter; - this.cookieKey = cookieKey; - this.domain = domain; - this.validTime = validTime; + this.cookieManager = cookieManager; } @PostMapping("/login/{oauthServerType}") @@ -73,25 +68,14 @@ ApiResponse> reissue( return ApiResponseGenerator.success(response, HttpStatus.CREATED, MessageCode.CREATE); } - private TokenResponse toResponse(TokenModel tokenModel, HttpServletResponse httpResponse) { + private TokenResponse toResponse(TokenModel tokenModel, HttpServletResponse httpServletResponse) { TokenResponse response = tokenResponseConverter.from(tokenModel.getAccessToken(), tokenModel.getAccessExpiredTime()); - setCookie(httpResponse, tokenModel); - return response; - } - - private void setCookie(HttpServletResponse response, TokenModel tokenModel) { ResponseCookie cookie = - ResponseCookie.from(cookieKey, tokenModel.getRefreshToken()) - .path("/") - .domain(domain) - .httpOnly(true) - .secure(true) - .sameSite("None") - .maxAge(TimeUtil.convertSecondsFromMillis(validTime)) - .build(); + cookieManager.createCookie(AuthConstants.TOKEN_KEY, tokenModel.getRefreshToken()); + httpServletResponse.addHeader(HttpHeaders.SET_COOKIE, cookie.toString()); - response.addHeader("Set-Cookie", cookie.toString()); + return response; } } From 0b5380327354d0af4049003291c43ff86b41a95b Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Fri, 8 Mar 2024 01:04:51 +0900 Subject: [PATCH 05/23] =?UTF-8?q?feat=20:=20redis=20=EC=9D=98=EC=A1=B4?= =?UTF-8?q?=EC=84=B1=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/eeos/build.gradle | 4 ++++ .../docker-compose.yml | 8 ++++++- .../blackcompany/eeos/config/RedisConfig.java | 21 +++++++++++++++++++ .../resources/application-local-redis.yml | 7 +++++++ .../src/main/resources/application-redis.yml | 7 +++++++ BE/eeos/src/main/resources/application.yml | 3 +++ 6 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 BE/eeos/src/main/java/com/blackcompany/eeos/config/RedisConfig.java create mode 100644 BE/eeos/src/main/resources/application-local-redis.yml create mode 100644 BE/eeos/src/main/resources/application-redis.yml diff --git a/BE/eeos/build.gradle b/BE/eeos/build.gradle index 545d67f5d..ca1e8f17c 100644 --- a/BE/eeos/build.gradle +++ b/BE/eeos/build.gradle @@ -74,6 +74,10 @@ dependencies { implementation 'org.flywaydb:flyway-core' implementation 'org.flywaydb:flyway-mysql' + // redis + implementation 'org.springframework.boot:spring-boot-starter-data-redis' + + // openfeign implementation 'org.springframework.cloud:spring-cloud-starter-openfeign' diff --git a/BE/eeos/resources/local-develop-environment/docker-compose.yml b/BE/eeos/resources/local-develop-environment/docker-compose.yml index b3ca25a17..c107468ae 100644 --- a/BE/eeos/resources/local-develop-environment/docker-compose.yml +++ b/BE/eeos/resources/local-develop-environment/docker-compose.yml @@ -21,4 +21,10 @@ services: environment: - ADMINER_DEFAULT_SERVER=overflow-mysql8 - ADMINER_DESIGN=nette - - ADMINER_PLUGINS=tables-filter tinymce \ No newline at end of file + - ADMINER_PLUGINS=tables-filter tinymce + + overflow-redis: # Redis + container_name: eeos-redis + image: redis:latest + ports: + - "16379:6379" \ No newline at end of file diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/config/RedisConfig.java b/BE/eeos/src/main/java/com/blackcompany/eeos/config/RedisConfig.java new file mode 100644 index 000000000..c3bcea9b0 --- /dev/null +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/config/RedisConfig.java @@ -0,0 +1,21 @@ +package com.blackcompany.eeos.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; + +@Configuration +public class RedisConfig { + @Value("${spring.redis.host}") + private String host; + + @Value("${spring.redis.port}") + private int port; + + @Bean + public RedisConnectionFactory redisConnectionFactory() { + return new LettuceConnectionFactory(host, port); + } +} diff --git a/BE/eeos/src/main/resources/application-local-redis.yml b/BE/eeos/src/main/resources/application-local-redis.yml new file mode 100644 index 000000000..cd99df1b1 --- /dev/null +++ b/BE/eeos/src/main/resources/application-local-redis.yml @@ -0,0 +1,7 @@ +spring: + config: + activate: + on-profile: local-redis + redis: + host: localhost + port : 16379 \ No newline at end of file diff --git a/BE/eeos/src/main/resources/application-redis.yml b/BE/eeos/src/main/resources/application-redis.yml new file mode 100644 index 000000000..a65cbc47f --- /dev/null +++ b/BE/eeos/src/main/resources/application-redis.yml @@ -0,0 +1,7 @@ +spring: + config: + activate: + on-profile: redis + redis: + host: ${REDIS_HOST} + port : ${REDIS_PORT} \ No newline at end of file diff --git a/BE/eeos/src/main/resources/application.yml b/BE/eeos/src/main/resources/application.yml index 51eb056bd..7944468c3 100644 --- a/BE/eeos/src/main/resources/application.yml +++ b/BE/eeos/src/main/resources/application.yml @@ -7,6 +7,7 @@ spring: - oauth - token - actuator + - local-redis dev: - mysql - api @@ -14,6 +15,7 @@ spring: - token - log - actuator + - redis prod: - mysql - api @@ -21,4 +23,5 @@ spring: - token - log - actuator + - redis active: local \ No newline at end of file From 3bd6974e7c05d5b8c05b2f42ae1bbd9b07b01bcc Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Fri, 8 Mar 2024 01:05:02 +0900 Subject: [PATCH 06/23] =?UTF-8?q?refactor=20:=20env=20=ED=82=A4=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD=EC=9C=BC=EB=A1=9C=20=EC=9D=B8=ED=95=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/eeos/src/main/resources/application-api.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/BE/eeos/src/main/resources/application-api.yml b/BE/eeos/src/main/resources/application-api.yml index a2c2dbbf3..c9498757e 100644 --- a/BE/eeos/src/main/resources/application-api.yml +++ b/BE/eeos/src/main/resources/application-api.yml @@ -7,6 +7,7 @@ cors: allow-origin: urls: ${CORS_URL} -api: - domain: ${API_DOMAIN} - cookie-key: ${COOKIE_KEY} \ No newline at end of file +token: + cookie: + domain: ${COOKIE_TOKEN_DOMAIN} + path: ${COOKIE_TOKEN_PATH} \ No newline at end of file From ffbb5b083e3d2d38f90f985ddb7bd002bd7a1e28 Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Fri, 8 Mar 2024 01:06:52 +0900 Subject: [PATCH 07/23] =?UTF-8?q?refactor=20:=20=EA=B0=81=20=EA=B0=9D?= =?UTF-8?q?=EC=B2=B4=EC=97=90=EC=84=9C=20=EC=BF=A0=ED=82=A4=20key=EB=A5=BC?= =?UTF-8?q?=20=EC=83=81=EC=88=98=EB=A1=9C=20=EA=B4=80=EB=A6=AC=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EA=B3=A0=20=EC=83=81=EC=88=98=EB=A5=BC=20?= =?UTF-8?q?=EA=B4=80=EB=A6=AC=ED=95=98=EB=8A=94=20=EA=B0=9D=EC=B2=B4?= =?UTF-8?q?=EB=A1=9C=EB=B6=80=ED=84=B0=20=EA=B0=80=EC=A0=B8=EC=98=A4?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../eeos/auth/presentation/support/CookieTokenExtractor.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/presentation/support/CookieTokenExtractor.java b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/presentation/support/CookieTokenExtractor.java index cd7baabd1..25d86d504 100644 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/presentation/support/CookieTokenExtractor.java +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/presentation/support/CookieTokenExtractor.java @@ -8,14 +8,13 @@ @Component("cookie") public class CookieTokenExtractor implements TokenExtractor { - private static final String cookieKey = "eeos_token"; @Override public String extract(HttpServletRequest request) { Cookie[] cookies = getCookies(request); for (Cookie cookie : cookies) { - if (Objects.equals(cookieKey, cookie.getName())) { + if (Objects.equals(AuthConstants.TOKEN_KEY, cookie.getName())) { return getValue(cookie.getValue()); } } From af843b20ccc2655c7c85349b304dd2ea59a65fa5 Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Fri, 8 Mar 2024 01:15:25 +0900 Subject: [PATCH 08/23] =?UTF-8?q?refactor=20:=20token=20=EA=B4=80=EB=A6=AC?= =?UTF-8?q?=20redis=EB=A1=9C=20=ED=95=98=EB=8F=84=EB=A1=9D=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../eeos/auth/persistence/AuthInfoEntity.java | 41 ------------------- .../persistence/AuthInfoEntityConverter.java | 11 ----- .../auth/persistence/AuthInfoRepository.java | 15 ------- .../MemberAuthenticationEntity.java | 22 ++++++++++ .../MemberAuthenticationRepository.java | 21 ++++++++++ 5 files changed, 43 insertions(+), 67 deletions(-) delete mode 100644 BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/AuthInfoEntity.java delete mode 100644 BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/AuthInfoEntityConverter.java delete mode 100644 BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/AuthInfoRepository.java create mode 100644 BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/MemberAuthenticationEntity.java create mode 100644 BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/MemberAuthenticationRepository.java diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/AuthInfoEntity.java b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/AuthInfoEntity.java deleted file mode 100644 index b519cf478..000000000 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/AuthInfoEntity.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.blackcompany.eeos.auth.persistence; - -import com.blackcompany.eeos.common.persistence.BaseEntity; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.Index; -import javax.persistence.Table; -import lombok.AccessLevel; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.ToString; -import lombok.experimental.SuperBuilder; - -@Getter -@NoArgsConstructor(access = AccessLevel.PROTECTED) -@AllArgsConstructor(access = AccessLevel.PRIVATE) -@ToString -@SuperBuilder(toBuilder = true) -@Entity -@Table( - name = AuthInfoEntity.ENTITY_PREFIX, - indexes = @Index(name = "idx_auth_info_member_id", columnList = "auth_info_member_id")) -public class AuthInfoEntity extends BaseEntity { - - public static final String ENTITY_PREFIX = "auth_info"; - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = ENTITY_PREFIX + "_id", nullable = false) - private Long id; - - @Column(name = ENTITY_PREFIX + "_token", nullable = false) - private String token; - - @Column(name = ENTITY_PREFIX + "_member_id", nullable = false) - private Long memberId; -} diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/AuthInfoEntityConverter.java b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/AuthInfoEntityConverter.java deleted file mode 100644 index 771325228..000000000 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/AuthInfoEntityConverter.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.blackcompany.eeos.auth.persistence; - -import org.springframework.stereotype.Component; - -@Component -public class AuthInfoEntityConverter { - - public AuthInfoEntity from(Long memberId, String refreshToken) { - return AuthInfoEntity.builder().memberId(memberId).token(refreshToken).build(); - } -} diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/AuthInfoRepository.java b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/AuthInfoRepository.java deleted file mode 100644 index 7efb870d8..000000000 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/AuthInfoRepository.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.blackcompany.eeos.auth.persistence; - -import java.util.Optional; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.query.Param; - -public interface AuthInfoRepository extends JpaRepository { - @Query("SELECT a FROM AuthInfoEntity a WHERE a.memberId=:memberId AND a.token =:token") - Optional findByMemberIdAndToken( - @Param("memberId") Long memberId, @Param("token") String token); - - @Query("SELECT a FROM AuthInfoEntity a WHERE a.token =:token") - Optional findByToken(@Param("token") String token); -} diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/MemberAuthenticationEntity.java b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/MemberAuthenticationEntity.java new file mode 100644 index 000000000..29ceed9b3 --- /dev/null +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/MemberAuthenticationEntity.java @@ -0,0 +1,22 @@ +package com.blackcompany.eeos.auth.persistence; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.ToString; +import org.springframework.data.annotation.Id; +import org.springframework.data.redis.core.RedisHash; +import org.springframework.data.redis.core.TimeToLive; + +@RedisHash("MemberAuthentication") +@Getter +@Setter +@ToString +@AllArgsConstructor +@NoArgsConstructor +public class MemberAuthenticationEntity { + @Id private String token; + private Long memberId; + @TimeToLive private Long expiration; +} diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/MemberAuthenticationRepository.java b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/MemberAuthenticationRepository.java new file mode 100644 index 000000000..8de14d724 --- /dev/null +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/MemberAuthenticationRepository.java @@ -0,0 +1,21 @@ +package com.blackcompany.eeos.auth.persistence; + +import java.util.concurrent.TimeUnit; +import lombok.RequiredArgsConstructor; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Repository; + +@Repository +@RequiredArgsConstructor +public class MemberAuthenticationRepository { + + private final RedisTemplate redisTemplate; + + public void setData(String key, Long value, Long expiredTime) { + redisTemplate.opsForValue().set(key, value, expiredTime, TimeUnit.MILLISECONDS); + } + + public boolean isExistKey(String key) { + return redisTemplate.hasKey(key); + } +} From ab9698504378fc4bfb89340c2370ee7c33a5d49b Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Fri, 8 Mar 2024 01:17:15 +0900 Subject: [PATCH 09/23] =?UTF-8?q?refactor=20:=20OAuth=20=EC=A0=95=EB=B3=B4?= =?UTF-8?q?=20=EA=B4=80=EB=A6=AC=20=EC=97=94=ED=8B=B0=ED=8B=B0=EB=AA=85=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../converter/OauthInfoEntityConverter.java | 24 ------------------- .../converter/OauthMemberEntityConverter.java | 24 +++++++++++++++++++ ...InfoEntity.java => OAuthMemberEntity.java} | 6 ++--- ...sitory.java => OAuthMemberRepository.java} | 6 ++--- .../eeos/auth/fixture/FakeOauthMember.java | 6 ++--- 5 files changed, 33 insertions(+), 33 deletions(-) delete mode 100644 BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/domain/converter/OauthInfoEntityConverter.java create mode 100644 BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/domain/converter/OauthMemberEntityConverter.java rename BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/{OauthInfoEntity.java => OAuthMemberEntity.java} (85%) rename BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/{OauthInfoRepository.java => OAuthMemberRepository.java} (51%) diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/domain/converter/OauthInfoEntityConverter.java b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/domain/converter/OauthInfoEntityConverter.java deleted file mode 100644 index a3224b6d9..000000000 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/domain/converter/OauthInfoEntityConverter.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.blackcompany.eeos.auth.application.domain.converter; - -import com.blackcompany.eeos.auth.application.domain.OauthMemberModel; -import com.blackcompany.eeos.auth.persistence.OauthInfoEntity; -import com.blackcompany.eeos.common.support.converter.AbstractEntityConverter; -import org.springframework.stereotype.Component; - -@Component -public class OauthInfoEntityConverter - implements AbstractEntityConverter { - @Override - public OauthMemberModel from(final OauthInfoEntity oauthEntity) { - return OauthMemberModel.builder().oauthId(oauthEntity.getOauthId()).build(); - } - - @Override - public OauthInfoEntity toEntity(final OauthMemberModel oauthMemberModel) { - return OauthInfoEntity.builder().oauthId(oauthMemberModel.getOauthId()).build(); - } - - public OauthInfoEntity toEntity(final String oauthId, Long memberId) { - return OauthInfoEntity.builder().oauthId(oauthId).memberId(memberId).build(); - } -} diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/domain/converter/OauthMemberEntityConverter.java b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/domain/converter/OauthMemberEntityConverter.java new file mode 100644 index 000000000..5b90a8648 --- /dev/null +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/domain/converter/OauthMemberEntityConverter.java @@ -0,0 +1,24 @@ +package com.blackcompany.eeos.auth.application.domain.converter; + +import com.blackcompany.eeos.auth.application.domain.OauthMemberModel; +import com.blackcompany.eeos.auth.persistence.OAuthMemberEntity; +import com.blackcompany.eeos.common.support.converter.AbstractEntityConverter; +import org.springframework.stereotype.Component; + +@Component +public class OauthMemberEntityConverter + implements AbstractEntityConverter { + @Override + public OauthMemberModel from(final OAuthMemberEntity entity) { + return OauthMemberModel.builder().oauthId(entity.getOauthId()).build(); + } + + @Override + public OAuthMemberEntity toEntity(final OauthMemberModel model) { + return OAuthMemberEntity.builder().oauthId(model.getOauthId()).build(); + } + + public OAuthMemberEntity toEntity(final String oauthId, Long memberId) { + return OAuthMemberEntity.builder().oauthId(oauthId).memberId(memberId).build(); + } +} diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/OauthInfoEntity.java b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/OAuthMemberEntity.java similarity index 85% rename from BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/OauthInfoEntity.java rename to BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/OAuthMemberEntity.java index 0cc4ea385..ce188b9e1 100644 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/OauthInfoEntity.java +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/OAuthMemberEntity.java @@ -20,9 +20,9 @@ @ToString @SuperBuilder(toBuilder = true) @Entity -@Table(name = OauthInfoEntity.ENTITY_PREFIX) -public class OauthInfoEntity extends BaseEntity { - public static final String ENTITY_PREFIX = "oauth_info"; +@Table(name = OAuthMemberEntity.ENTITY_PREFIX) +public class OAuthMemberEntity extends BaseEntity { + public static final String ENTITY_PREFIX = "oauth_member"; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/OauthInfoRepository.java b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/OAuthMemberRepository.java similarity index 51% rename from BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/OauthInfoRepository.java rename to BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/OAuthMemberRepository.java index 33b8d0f66..a24d3d8df 100644 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/OauthInfoRepository.java +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/OAuthMemberRepository.java @@ -5,7 +5,7 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; -public interface OauthInfoRepository extends JpaRepository { - @Query("SELECT o FROM OauthInfoEntity o WHERE o.oauthId=:oauthId") - Optional findByOauthId(@Param("oauthId") String oauthId); +public interface OAuthMemberRepository extends JpaRepository { + @Query("SELECT o FROM OAuthMemberEntity o WHERE o.oauthId=:oauthId") + Optional findByOauthId(@Param("oauthId") String oauthId); } diff --git a/BE/eeos/src/test/java/com/blackcompany/eeos/auth/fixture/FakeOauthMember.java b/BE/eeos/src/test/java/com/blackcompany/eeos/auth/fixture/FakeOauthMember.java index c27741375..d889ba058 100644 --- a/BE/eeos/src/test/java/com/blackcompany/eeos/auth/fixture/FakeOauthMember.java +++ b/BE/eeos/src/test/java/com/blackcompany/eeos/auth/fixture/FakeOauthMember.java @@ -2,7 +2,7 @@ import com.blackcompany.eeos.auth.application.domain.OauthMemberModel; import com.blackcompany.eeos.auth.application.domain.OauthServerType; -import com.blackcompany.eeos.auth.persistence.OauthInfoEntity; +import com.blackcompany.eeos.auth.persistence.OAuthMemberEntity; public class FakeOauthMember { public static OauthMemberModel oauthMemberModel() { @@ -13,7 +13,7 @@ public static OauthMemberModel oauthMemberModel() { .build(); } - public static OauthInfoEntity oauthInfoEntity() { - return OauthInfoEntity.builder().oauthId("oauthId").memberId(1L).build(); + public static OAuthMemberEntity oauthInfoEntity() { + return OAuthMemberEntity.builder().oauthId("oauthId").memberId(1L).build(); } } From 4d8582a524e6f3b6e94f21973527fcf3bdf2d929 Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Fri, 8 Mar 2024 01:20:15 +0900 Subject: [PATCH 10/23] =?UTF-8?q?refactor=20:=20=EC=9E=AC=EB=B0=9C?= =?UTF-8?q?=EA=B8=89=20=EC=8B=9C=20=EB=B8=94=EB=9E=99=EB=A6=AC=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=EC=97=90=20=ED=8F=AC=ED=95=A8=EB=90=9C=20=ED=86=A0?= =?UTF-8?q?=ED=81=B0=EC=9D=B8=EC=A7=80=20=ED=8C=90=EB=8B=A8=ED=95=98?= =?UTF-8?q?=EA=B3=A0=20=EC=95=84=EB=8B=90=20=EC=8B=9C=EC=97=90=EB=A7=8C=20?= =?UTF-8?q?=EC=9E=AC=EB=B0=9C=EA=B8=89=20=EA=B0=80=EB=8A=A5=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EB=A1=9C=EC=A7=81=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/service/ReissueService.java | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/ReissueService.java b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/ReissueService.java index a75bf1ed8..7d1166795 100644 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/ReissueService.java +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/ReissueService.java @@ -4,11 +4,10 @@ import com.blackcompany.eeos.auth.application.domain.token.TokenResolver; import com.blackcompany.eeos.auth.application.exception.InvalidTokenException; import com.blackcompany.eeos.auth.application.usecase.ReissueUsecase; -import com.blackcompany.eeos.auth.persistence.AuthInfoEntity; -import com.blackcompany.eeos.auth.persistence.AuthInfoRepository; -import java.util.Optional; +import com.blackcompany.eeos.auth.persistence.MemberAuthenticationRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -18,30 +17,32 @@ @Slf4j public class ReissueService implements ReissueUsecase { private final CreateTokenService createTokenService; - private final AuthInfoRepository authInfoRepository; + private final MemberAuthenticationRepository memberAuthenticationRepository; private final TokenResolver tokenResolver; + @Value("${security.jwt.refresh.validTime}") + private long validTime; + @Transactional @Override public TokenModel execute(final String token) { Long memberId = tokenResolver.getUserInfoByCookie(token); - validateToken(memberId, token); + + validateToken(token); + savedUsedToken(token, memberId); return createTokenService.execute(memberId); } - private void validateToken(final Long memberId, final String token) { - Optional validToken = - authInfoRepository.findByMemberIdAndToken(memberId, token); - if (validToken.isPresent()) { - validToken.ifPresent(authInfoRepository::delete); - return; + private void validateToken(final String token) { + boolean isExistToken = memberAuthenticationRepository.isExistKey(token); + + if (isExistToken) { + throw new InvalidTokenException(); } - deleteInvalidToken(token); } - private void deleteInvalidToken(final String token) { - authInfoRepository.findByToken(token).ifPresent(authInfoRepository::delete); - throw new InvalidTokenException(); + private void savedUsedToken(final String token, final Long memberId) { + memberAuthenticationRepository.setData(token, memberId, validTime); } } From a3ab303b2dd5b1a1f812a49107f66fdc29ee8431 Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Sat, 9 Mar 2024 09:59:18 +0900 Subject: [PATCH 11/23] =?UTF-8?q?refactor=20:=20=EC=84=9C=EB=B9=84?= =?UTF-8?q?=EC=8A=A4=EC=97=90=20=EB=A7=9E=EB=8A=94=20=ED=86=A0=ED=81=B0?= =?UTF-8?q?=EC=9D=84=20=EB=B0=98=ED=99=98=ED=95=98=EB=8A=94=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=EB=AA=85=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AuthenticationTokenGenerator.java} | 23 ++------- .../service/CreateTokenServiceTest.java | 51 ------------------- 2 files changed, 4 insertions(+), 70 deletions(-) rename BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/{service/CreateTokenService.java => support/AuthenticationTokenGenerator.java} (52%) delete mode 100644 BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/CreateTokenServiceTest.java diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/CreateTokenService.java b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/support/AuthenticationTokenGenerator.java similarity index 52% rename from BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/CreateTokenService.java rename to BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/support/AuthenticationTokenGenerator.java index 8edddde04..7ed96d128 100644 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/CreateTokenService.java +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/support/AuthenticationTokenGenerator.java @@ -1,42 +1,27 @@ -package com.blackcompany.eeos.auth.application.service; +package com.blackcompany.eeos.auth.application.support; import com.blackcompany.eeos.auth.application.domain.TokenModel; import com.blackcompany.eeos.auth.application.domain.converter.TokenModelConverter; import com.blackcompany.eeos.auth.application.domain.token.TokenProvider; import com.blackcompany.eeos.auth.application.domain.token.TokenResolver; -import com.blackcompany.eeos.auth.persistence.AuthInfoEntity; -import com.blackcompany.eeos.auth.persistence.AuthInfoEntityConverter; -import com.blackcompany.eeos.auth.persistence.AuthInfoRepository; import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; +import org.springframework.stereotype.Component; -@Service +@Component @RequiredArgsConstructor -@Transactional(readOnly = true) -public class CreateTokenService { +public class AuthenticationTokenGenerator { private final TokenProvider tokenProvider; private final TokenModelConverter tokenModelConverter; private final TokenResolver tokenResolver; - private final AuthInfoRepository authInfoRepository; - private final AuthInfoEntityConverter authInfoEntityConverter; - @Transactional public TokenModel execute(final Long memberId) { String accessToken = tokenProvider.createAccessToken(memberId); String refreshToken = tokenProvider.createRefreshToken(memberId); - saveToken(memberId, refreshToken); - return tokenModelConverter.from( accessToken, tokenResolver.getExpiredDateByHeader(accessToken), refreshToken, tokenResolver.getExpiredDateByCookie(refreshToken)); } - - private void saveToken(final Long memberId, final String token) { - AuthInfoEntity authInfoEntity = authInfoEntityConverter.from(memberId, token); - authInfoRepository.save(authInfoEntity); - } } diff --git a/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/CreateTokenServiceTest.java b/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/CreateTokenServiceTest.java deleted file mode 100644 index 880deb38e..000000000 --- a/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/CreateTokenServiceTest.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.blackcompany.eeos.auth.application.service; - -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import com.blackcompany.eeos.auth.application.domain.converter.TokenModelConverter; -import com.blackcompany.eeos.auth.application.domain.token.TokenProvider; -import com.blackcompany.eeos.auth.application.domain.token.TokenResolver; -import com.blackcompany.eeos.auth.persistence.AuthInfoEntity; -import com.blackcompany.eeos.auth.persistence.AuthInfoEntityConverter; -import com.blackcompany.eeos.auth.persistence.AuthInfoRepository; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Spy; -import org.mockito.junit.jupiter.MockitoExtension; - -@ExtendWith(MockitoExtension.class) -class CreateTokenServiceTest { - @InjectMocks CreateTokenService createTokenService; - @Mock TokenResolver tokenResolver; - @Spy TokenModelConverter tokeModelConverter; - - @Mock TokenProvider tokenProvider; - @Mock AuthInfoRepository authInfoRepository; - @Mock AuthInfoEntityConverter authInfoEntityConverter; - - @Test - @DisplayName("토큰 생성 요청이 들어오면 토큰을 생성한 후 토큰을 저장한다.") - void save_token_when_request_create_token() { - // given - Long userId = 1L; - String accessToken = "mocked_access_token"; - String refreshToken = "mocked_refresh_token"; - - AuthInfoEntity entity = - AuthInfoEntity.builder().id(1L).memberId(userId).token(refreshToken).build(); - - when(tokenProvider.createAccessToken(userId)).thenReturn(accessToken); - when(tokenProvider.createRefreshToken(userId)).thenReturn(refreshToken); - when(authInfoEntityConverter.from(userId, refreshToken)).thenReturn(entity); - - // when - createTokenService.execute(userId); - - // then - verify(authInfoRepository).save(entity); - } -} From f5d42a262fc48e4b23e8b2eba59ec1f9d38ca1b8 Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Sat, 9 Mar 2024 09:59:49 +0900 Subject: [PATCH 12/23] =?UTF-8?q?refactor=20:=20=EB=B3=84=EB=8F=84?= =?UTF-8?q?=EC=9D=98=20=ED=81=B4=EB=9E=98=EC=8A=A4=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=A7=84=ED=96=89=ED=95=98=EC=A7=80=20=EC=95=8A=EA=B3=A0=20?= =?UTF-8?q?=ED=95=9C=20=ED=81=B4=EB=9E=98=EC=8A=A4=EC=9D=98=20=EC=B1=85?= =?UTF-8?q?=EC=9E=84=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/AuthFacadeService.java | 41 ++++++++++-- .../auth/application/service/AuthService.java | 44 ------------- .../service/AuthFacadeServiceTest.java | 12 ++-- .../application/service/AuthServiceTest.java | 66 ------------------- 4 files changed, 39 insertions(+), 124 deletions(-) delete mode 100644 BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/AuthService.java delete mode 100644 BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/AuthServiceTest.java diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/AuthFacadeService.java b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/AuthFacadeService.java index a11fcff04..da396af8a 100644 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/AuthFacadeService.java +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/AuthFacadeService.java @@ -2,11 +2,16 @@ import com.blackcompany.eeos.auth.application.domain.OauthMemberModel; import com.blackcompany.eeos.auth.application.domain.TokenModel; +import com.blackcompany.eeos.auth.application.domain.converter.OauthMemberEntityConverter; +import com.blackcompany.eeos.auth.application.support.AuthenticationTokenGenerator; import com.blackcompany.eeos.auth.application.usecase.LoginUsecase; -import com.blackcompany.eeos.auth.persistence.OauthInfoEntity; +import com.blackcompany.eeos.auth.persistence.OAuthMemberEntity; +import com.blackcompany.eeos.auth.persistence.OAuthMemberRepository; +import com.blackcompany.eeos.member.application.model.converter.MemberEntityConverter; +import com.blackcompany.eeos.member.persistence.MemberEntity; +import com.blackcompany.eeos.member.persistence.MemberRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @Service @@ -14,14 +19,36 @@ @Transactional public class AuthFacadeService implements LoginUsecase { private final OauthClientService oauthClientService; - private final AuthService authService; - private final CreateTokenService createTokenService; + private final MemberRepository memberRepository; + private final OAuthMemberRepository oAuthMemberRepository; + private final AuthenticationTokenGenerator authenticationTokenGenerator; + private final MemberEntityConverter memberEntityConverter; + private final OauthMemberEntityConverter oauthMemberEntityConverter; - @Transactional(propagation = Propagation.REQUIRED) + @Transactional @Override public TokenModel login(String oauthServerType, String authCode, String uri) { OauthMemberModel model = oauthClientService.getOauthMember(oauthServerType, authCode, uri); - OauthInfoEntity entity = authService.login(model); - return createTokenService.execute(entity.getMemberId()); + OAuthMemberEntity entity = login(model); + return authenticationTokenGenerator.execute(entity.getMemberId()); + } + + private OAuthMemberEntity login(final OauthMemberModel model) { + return oAuthMemberRepository + .findByOauthId(model.getOauthId()) + .orElseGet(() -> signUpMember(model)); + } + + private OAuthMemberEntity signUpMember(final OauthMemberModel model) { + MemberEntity entity = + memberEntityConverter.toEntity(model.getName(), model.getOauthServerType()); + + MemberEntity savedMember = memberRepository.save(entity); + return saveOauthInfoEntity(model.getOauthId(), savedMember.getId()); + } + + private OAuthMemberEntity saveOauthInfoEntity(final String oauthId, final Long memberId) { + OAuthMemberEntity entity = oauthMemberEntityConverter.toEntity(oauthId, memberId); + return oAuthMemberRepository.save(entity); } } diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/AuthService.java b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/AuthService.java deleted file mode 100644 index 87fd35a94..000000000 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/AuthService.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.blackcompany.eeos.auth.application.service; - -import com.blackcompany.eeos.auth.application.domain.OauthMemberModel; -import com.blackcompany.eeos.auth.application.domain.converter.OauthInfoEntityConverter; -import com.blackcompany.eeos.auth.persistence.OauthInfoEntity; -import com.blackcompany.eeos.auth.persistence.OauthInfoRepository; -import com.blackcompany.eeos.member.application.model.converter.MemberEntityConverter; -import com.blackcompany.eeos.member.persistence.MemberEntity; -import com.blackcompany.eeos.member.persistence.MemberRepository; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Propagation; -import org.springframework.transaction.annotation.Transactional; - -@Service -@Transactional(readOnly = true) -@RequiredArgsConstructor -public class AuthService { - - private final OauthInfoRepository oauthInfoRepository; - private final MemberRepository memberRepository; - private final MemberEntityConverter memberEntityConverter; - private final OauthInfoEntityConverter oauthInfoEntityConverter; - - @Transactional(propagation = Propagation.REQUIRED) - public OauthInfoEntity login(final OauthMemberModel model) { - return oauthInfoRepository - .findByOauthId(model.getOauthId()) - .orElseGet(() -> signUpMember(model)); - } - - private OauthInfoEntity signUpMember(final OauthMemberModel model) { - MemberEntity entity = - memberEntityConverter.toEntity(model.getName(), model.getOauthServerType()); - - MemberEntity savedMember = memberRepository.save(entity); - return createOauthInfoEntity(model.getOauthId(), savedMember.getId()); - } - - private OauthInfoEntity createOauthInfoEntity(final String oauthId, final Long memberId) { - OauthInfoEntity entity = oauthInfoEntityConverter.toEntity(oauthId, memberId); - return oauthInfoRepository.save(entity); - } -} diff --git a/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/AuthFacadeServiceTest.java b/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/AuthFacadeServiceTest.java index bcd61d0e7..efadf8dbf 100644 --- a/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/AuthFacadeServiceTest.java +++ b/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/AuthFacadeServiceTest.java @@ -4,8 +4,9 @@ import static org.mockito.Mockito.when; import com.blackcompany.eeos.auth.application.domain.OauthMemberModel; +import com.blackcompany.eeos.auth.application.support.AuthenticationTokenGenerator; import com.blackcompany.eeos.auth.fixture.FakeOauthMember; -import com.blackcompany.eeos.auth.persistence.OauthInfoEntity; +import com.blackcompany.eeos.auth.persistence.OAuthMemberEntity; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -16,10 +17,8 @@ @ExtendWith(MockitoExtension.class) class AuthFacadeServiceTest { - @Mock CreateTokenService createTokenService; - - @Mock AuthService authService; @Mock OauthClientService oauthClientService; + @Mock AuthenticationTokenGenerator authenticationTokenGenerator; @InjectMocks AuthFacadeService authFacadeService; @@ -33,15 +32,14 @@ void response_token() { String uri = "uri"; OauthMemberModel oauthMemberModel = FakeOauthMember.oauthMemberModel(); - OauthInfoEntity oauthInfoEntity = FakeOauthMember.oauthInfoEntity(); + OAuthMemberEntity oAuthMemberEntity = FakeOauthMember.oauthInfoEntity(); when(oauthClientService.getOauthMember(type, authCode, uri)).thenReturn(oauthMemberModel); - when(authService.login(oauthMemberModel)).thenReturn(oauthInfoEntity); // when authFacadeService.login(type, authCode, uri); // then - Mockito.verify(createTokenService).execute(memberId); + Mockito.verify(authenticationTokenGenerator).execute(memberId); } } diff --git a/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/AuthServiceTest.java b/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/AuthServiceTest.java deleted file mode 100644 index 5d2cc322f..000000000 --- a/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/AuthServiceTest.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.blackcompany.eeos.auth.application.service; - -import static org.junit.jupiter.api.Assertions.assertAll; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import com.blackcompany.eeos.auth.application.domain.converter.OauthInfoEntityConverter; -import com.blackcompany.eeos.auth.fixture.FakeOauthMember; -import com.blackcompany.eeos.auth.persistence.OauthInfoEntity; -import com.blackcompany.eeos.auth.persistence.OauthInfoRepository; -import com.blackcompany.eeos.member.application.model.ActiveStatus; -import com.blackcompany.eeos.member.application.model.converter.MemberEntityConverter; -import com.blackcompany.eeos.member.fixture.MemberFixture; -import com.blackcompany.eeos.member.persistence.MemberRepository; -import java.util.Optional; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.Spy; -import org.mockito.junit.jupiter.MockitoExtension; - -@ExtendWith(MockitoExtension.class) -class AuthServiceTest { - @Mock OauthInfoRepository oauthInfoRepository; - @InjectMocks AuthService authService; - @Mock MemberRepository memberRepository; - @Spy OauthInfoEntityConverter oauthInfoEntityConverter; - - @Spy MemberEntityConverter memberEntityConverter; - - @Test - @DisplayName("새로운 회원인 경우 oauth에서 가져온 회원 정보를 저장한다.") - void login_existing_user() { - // given - when(oauthInfoRepository.findByOauthId(FakeOauthMember.oauthMemberModel().getOauthId())) - .thenReturn(Optional.ofNullable(null)); - when(memberRepository.save(Mockito.any())) - .thenReturn(MemberFixture.멤버_엔티티(1L, ActiveStatus.AM)); - - // when - authService.login(FakeOauthMember.oauthMemberModel()); - - // then - assertAll( - () -> verify(memberRepository).save(Mockito.any()), - () -> verify(oauthInfoRepository).save(Mockito.any())); - } - - @Test - @DisplayName("기존 회원인 경우 존재하던 oauth 정보를 가져온다.") - void login_new_user() { - // given - when(oauthInfoRepository.findByOauthId(FakeOauthMember.oauthMemberModel().getOauthId())) - .thenReturn(Optional.of(FakeOauthMember.oauthInfoEntity())); - - // when - OauthInfoEntity entity = authService.login(FakeOauthMember.oauthMemberModel()); - - // then - assertEquals(entity.getOauthId(), FakeOauthMember.oauthMemberModel().getOauthId()); - } -} From 37842e8417cfbd8d451f90d97b3bfe43a034425d Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Sat, 9 Mar 2024 10:00:11 +0900 Subject: [PATCH 13/23] =?UTF-8?q?refactor=20:=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=EA=B7=9C=EC=B9=99=EC=97=90=20=EB=A7=9E=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../eeos/auth/persistence/MemberAuthenticationRepository.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/MemberAuthenticationRepository.java b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/MemberAuthenticationRepository.java index 8de14d724..e3b05abdf 100644 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/MemberAuthenticationRepository.java +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/persistence/MemberAuthenticationRepository.java @@ -11,11 +11,11 @@ public class MemberAuthenticationRepository { private final RedisTemplate redisTemplate; - public void setData(String key, Long value, Long expiredTime) { + public void save(String key, Long value, Long expiredTime) { redisTemplate.opsForValue().set(key, value, expiredTime, TimeUnit.MILLISECONDS); } - public boolean isExistKey(String key) { + public boolean isExistToken(String key) { return redisTemplate.hasKey(key); } } From 894857bfc6af4f76c4e9c55e8a10ae9c9e081e35 Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Sat, 9 Mar 2024 10:09:40 +0900 Subject: [PATCH 14/23] =?UTF-8?q?fix=20:=20=EC=99=B8=EB=B6=80=20=ED=98=B8?= =?UTF-8?q?=EC=B6=9C=20api=20=ED=8A=B8=EB=9E=9C=EC=A0=9D=EC=85=98=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=ED=95=98=EC=A7=80=20=EC=95=8A=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../eeos/auth/application/service/OauthClientService.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/OauthClientService.java b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/OauthClientService.java index be9654aff..993594a08 100644 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/OauthClientService.java +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/OauthClientService.java @@ -4,15 +4,12 @@ import com.blackcompany.eeos.auth.application.domain.client.OauthMemberClientComposite; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Propagation; -import org.springframework.transaction.annotation.Transactional; @Service @RequiredArgsConstructor public class OauthClientService { private final OauthMemberClientComposite oauthMemberClientComposite; - @Transactional(propagation = Propagation.REQUIRED) public OauthMemberModel getOauthMember(String oauthServerType, String authCode, String uri) { OauthMemberModel model = oauthMemberClientComposite.fetch(oauthServerType, authCode, uri); model.validateNameFormat(); From f63c0503a1fb3663e3eaa17c647b5c4907eaf469 Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Sat, 9 Mar 2024 10:08:24 +0900 Subject: [PATCH 15/23] =?UTF-8?q?refactor=20:=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=ED=95=9C=20=ED=86=A0=ED=81=B0=20=ED=95=B4=EB=8B=B9=20=ED=86=A0?= =?UTF-8?q?=ED=81=B0=20=EB=A7=8C=EB=A3=8C=20=EC=8B=9C=EA=B0=84=EB=A7=8C?= =?UTF-8?q?=ED=81=BC=20=EB=B8=94=EB=9E=99=EB=A6=AC=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EB=A1=9C=20=EC=A0=80=EC=9E=A5=ED=95=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/service/ReissueService.java | 21 +++++----- .../service/ReissueServiceTest.java | 38 ++++++++----------- 2 files changed, 26 insertions(+), 33 deletions(-) diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/ReissueService.java b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/ReissueService.java index 7d1166795..199ac12c2 100644 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/ReissueService.java +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/ReissueService.java @@ -3,11 +3,11 @@ import com.blackcompany.eeos.auth.application.domain.TokenModel; import com.blackcompany.eeos.auth.application.domain.token.TokenResolver; import com.blackcompany.eeos.auth.application.exception.InvalidTokenException; +import com.blackcompany.eeos.auth.application.support.AuthenticationTokenGenerator; import com.blackcompany.eeos.auth.application.usecase.ReissueUsecase; import com.blackcompany.eeos.auth.persistence.MemberAuthenticationRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -16,33 +16,34 @@ @Transactional(readOnly = true) @Slf4j public class ReissueService implements ReissueUsecase { - private final CreateTokenService createTokenService; + private final AuthenticationTokenGenerator authenticationTokenGenerator; private final MemberAuthenticationRepository memberAuthenticationRepository; private final TokenResolver tokenResolver; - @Value("${security.jwt.refresh.validTime}") - private long validTime; - @Transactional @Override public TokenModel execute(final String token) { Long memberId = tokenResolver.getUserInfoByCookie(token); validateToken(token); - savedUsedToken(token, memberId); + saveUsedToken(token, memberId); - return createTokenService.execute(memberId); + return authenticationTokenGenerator.execute(memberId); } private void validateToken(final String token) { - boolean isExistToken = memberAuthenticationRepository.isExistKey(token); + boolean isExistToken = memberAuthenticationRepository.isExistToken(token); if (isExistToken) { throw new InvalidTokenException(); } } - private void savedUsedToken(final String token, final Long memberId) { - memberAuthenticationRepository.setData(token, memberId, validTime); + private void saveUsedToken(final String token, final Long memberId) { + memberAuthenticationRepository.save(token, memberId, getExpiredToken(token)); + } + + private Long getExpiredToken(final String token) { + return tokenResolver.getExpiredDateByHeader(token); } } diff --git a/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/ReissueServiceTest.java b/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/ReissueServiceTest.java index 9e674152f..67351f062 100644 --- a/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/ReissueServiceTest.java +++ b/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/ReissueServiceTest.java @@ -6,10 +6,8 @@ import com.blackcompany.eeos.auth.application.domain.token.TokenResolver; import com.blackcompany.eeos.auth.application.exception.InvalidTokenException; -import com.blackcompany.eeos.auth.fixture.FakeAuthInfo; -import com.blackcompany.eeos.auth.persistence.AuthInfoEntity; -import com.blackcompany.eeos.auth.persistence.AuthInfoRepository; -import java.util.Optional; +import com.blackcompany.eeos.auth.application.support.AuthenticationTokenGenerator; +import com.blackcompany.eeos.auth.persistence.MemberAuthenticationRepository; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -20,51 +18,45 @@ @ExtendWith(MockitoExtension.class) class ReissueServiceTest { - @Mock CreateTokenService createTokenService; - @Mock AuthInfoRepository authInfoRepository; + @Mock + AuthenticationTokenGenerator authenticationTokenGenerator; + @Mock MemberAuthenticationRepository memberAuthenticationRepository; @Mock TokenResolver tokenResolver; @InjectMocks ReissueService reissueService; @Test - @DisplayName("전달받은 토큰이 서버가 가지고 있는 유저의 토큰이 아닐 때 유효하지 않은 토큰을 제거하고 예외를 발생시킨다.") + @DisplayName("블랙리스트에 등록된 토큰이 아니라면 예외가 발생한다.") void exception_when_token_invalid() { // given String token = "token"; Long memberId = 1L; - AuthInfoEntity authInfoEntity = FakeAuthInfo.authInfoEntity(); - when(tokenResolver.getUserInfoByCookie(token)).thenReturn(memberId); - when(authInfoRepository.findByMemberIdAndToken(memberId, token)) - .thenReturn(Optional.ofNullable(null)); - when(authInfoRepository.findByToken(token)).thenReturn(Optional.ofNullable(authInfoEntity)); + when(memberAuthenticationRepository.isExistToken(token)).thenReturn(Boolean.TRUE); // when & then - assertAll( - () -> assertThrows(InvalidTokenException.class, () -> reissueService.execute(token)), - () -> verify(authInfoRepository).delete(authInfoEntity)); + assertThrows(InvalidTokenException.class, () -> reissueService.execute(token)); } @Test - @DisplayName("전달받은 토큰이 유효한 토큰이라면 해당 토큰 정보를 제거하고 새로운 토큰을 생성한다.") - void execute() { + @DisplayName("정상적인 토큰이라면 이전에 사용한 토큰은 블랙리스트로 등록하고 새로운 토큰을 생성한다.") + void token_valid() { // given String token = "token"; Long memberId = 1L; - - AuthInfoEntity authInfoEntity = FakeAuthInfo.authInfoEntity(); + Long validTime = 1L; when(tokenResolver.getUserInfoByCookie(token)).thenReturn(memberId); - when(authInfoRepository.findByMemberIdAndToken(memberId, token)) - .thenReturn(Optional.ofNullable(authInfoEntity)); + when(memberAuthenticationRepository.isExistToken(token)).thenReturn(Boolean.FALSE); + when(tokenResolver.getExpiredDateByHeader(token)).thenReturn(validTime); // when reissueService.execute(token); // then assertAll( - () -> verify(authInfoRepository).delete(authInfoEntity), - () -> verify(createTokenService).execute(memberId)); + () -> verify(memberAuthenticationRepository).save(token, memberId, validTime), + () -> verify(authenticationTokenGenerator).execute(memberId)); } } From e63722c82d3e6edb0cc7422faa525042ffe2372c Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Sat, 9 Mar 2024 13:48:20 +0900 Subject: [PATCH 16/23] =?UTF-8?q?chore=20:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20import=EB=AC=B8=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../eeos/auth/application/service/ReissueService.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/ReissueService.java b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/ReissueService.java index 199ac12c2..2fd99b886 100644 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/ReissueService.java +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/ReissueService.java @@ -7,14 +7,12 @@ import com.blackcompany.eeos.auth.application.usecase.ReissueUsecase; import com.blackcompany.eeos.auth.persistence.MemberAuthenticationRepository; import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service @RequiredArgsConstructor @Transactional(readOnly = true) -@Slf4j public class ReissueService implements ReissueUsecase { private final AuthenticationTokenGenerator authenticationTokenGenerator; private final MemberAuthenticationRepository memberAuthenticationRepository; From 8d00b36dd3cbc42afcb9f4cf6f9cba3891d5aa81 Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Sat, 9 Mar 2024 13:48:43 +0900 Subject: [PATCH 17/23] =?UTF-8?q?refactor=20:=20=EC=99=B8=EB=B6=80=20?= =?UTF-8?q?=ED=98=B8=EC=B6=9C=20api=EA=B0=80=20=ED=8A=B8=EB=9E=9C=EC=A0=9D?= =?UTF-8?q?=EC=85=98=EC=97=90=20=EB=AC=B6=EC=97=AC=EC=9E=88=EC=A7=80=20?= =?UTF-8?q?=EC=95=8A=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/AuthFacadeService.java | 34 +-------------- .../auth/application/service/AuthService.java | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+), 32 deletions(-) create mode 100644 BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/AuthService.java diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/AuthFacadeService.java b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/AuthFacadeService.java index da396af8a..bbc93a578 100644 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/AuthFacadeService.java +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/AuthFacadeService.java @@ -2,53 +2,23 @@ import com.blackcompany.eeos.auth.application.domain.OauthMemberModel; import com.blackcompany.eeos.auth.application.domain.TokenModel; -import com.blackcompany.eeos.auth.application.domain.converter.OauthMemberEntityConverter; import com.blackcompany.eeos.auth.application.support.AuthenticationTokenGenerator; import com.blackcompany.eeos.auth.application.usecase.LoginUsecase; import com.blackcompany.eeos.auth.persistence.OAuthMemberEntity; -import com.blackcompany.eeos.auth.persistence.OAuthMemberRepository; -import com.blackcompany.eeos.member.application.model.converter.MemberEntityConverter; -import com.blackcompany.eeos.member.persistence.MemberEntity; -import com.blackcompany.eeos.member.persistence.MemberRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; @Service @RequiredArgsConstructor -@Transactional public class AuthFacadeService implements LoginUsecase { private final OauthClientService oauthClientService; - private final MemberRepository memberRepository; - private final OAuthMemberRepository oAuthMemberRepository; + private final AuthService authService; private final AuthenticationTokenGenerator authenticationTokenGenerator; - private final MemberEntityConverter memberEntityConverter; - private final OauthMemberEntityConverter oauthMemberEntityConverter; - @Transactional @Override public TokenModel login(String oauthServerType, String authCode, String uri) { OauthMemberModel model = oauthClientService.getOauthMember(oauthServerType, authCode, uri); - OAuthMemberEntity entity = login(model); + OAuthMemberEntity entity = authService.authenticate(model); return authenticationTokenGenerator.execute(entity.getMemberId()); } - - private OAuthMemberEntity login(final OauthMemberModel model) { - return oAuthMemberRepository - .findByOauthId(model.getOauthId()) - .orElseGet(() -> signUpMember(model)); - } - - private OAuthMemberEntity signUpMember(final OauthMemberModel model) { - MemberEntity entity = - memberEntityConverter.toEntity(model.getName(), model.getOauthServerType()); - - MemberEntity savedMember = memberRepository.save(entity); - return saveOauthInfoEntity(model.getOauthId(), savedMember.getId()); - } - - private OAuthMemberEntity saveOauthInfoEntity(final String oauthId, final Long memberId) { - OAuthMemberEntity entity = oauthMemberEntityConverter.toEntity(oauthId, memberId); - return oAuthMemberRepository.save(entity); - } } diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/AuthService.java b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/AuthService.java new file mode 100644 index 000000000..3cbc1f79f --- /dev/null +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/auth/application/service/AuthService.java @@ -0,0 +1,42 @@ +package com.blackcompany.eeos.auth.application.service; + +import com.blackcompany.eeos.auth.application.domain.OauthMemberModel; +import com.blackcompany.eeos.auth.application.domain.converter.OauthMemberEntityConverter; +import com.blackcompany.eeos.auth.persistence.OAuthMemberEntity; +import com.blackcompany.eeos.auth.persistence.OAuthMemberRepository; +import com.blackcompany.eeos.member.application.model.converter.MemberEntityConverter; +import com.blackcompany.eeos.member.persistence.MemberEntity; +import com.blackcompany.eeos.member.persistence.MemberRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class AuthService { + private final MemberRepository memberRepository; + private final OAuthMemberRepository oAuthMemberRepository; + private final MemberEntityConverter memberEntityConverter; + private final OauthMemberEntityConverter oauthMemberEntityConverter; + + @Transactional + public OAuthMemberEntity authenticate(final OauthMemberModel model) { + return oAuthMemberRepository + .findByOauthId(model.getOauthId()) + .orElseGet(() -> signUpMember(model)); + } + + private OAuthMemberEntity signUpMember(final OauthMemberModel model) { + MemberEntity entity = + memberEntityConverter.toEntity(model.getName(), model.getOauthServerType()); + + MemberEntity savedMember = memberRepository.save(entity); + return saveOauthInfoEntity(model.getOauthId(), savedMember.getId()); + } + + private OAuthMemberEntity saveOauthInfoEntity(final String oauthId, final Long memberId) { + OAuthMemberEntity entity = oauthMemberEntityConverter.toEntity(oauthId, memberId); + return oAuthMemberRepository.save(entity); + } +} From ed77294193fb409584c3ac2e7d792646fa0cedee Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Sat, 9 Mar 2024 14:59:29 +0900 Subject: [PATCH 18/23] =?UTF-8?q?feat=20:=20redis=20template=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/blackcompany/eeos/config/RedisConfig.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/config/RedisConfig.java b/BE/eeos/src/main/java/com/blackcompany/eeos/config/RedisConfig.java index c3bcea9b0..c887070e0 100644 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/config/RedisConfig.java +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/config/RedisConfig.java @@ -5,6 +5,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; @Configuration public class RedisConfig { @@ -18,4 +19,12 @@ public class RedisConfig { public RedisConnectionFactory redisConnectionFactory() { return new LettuceConnectionFactory(host, port); } + + @Bean + public RedisTemplate redisTemplate() { + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(redisConnectionFactory()); + + return redisTemplate; + } } From e2448a71306b45e1dbd1bbe2b7d1ecedc5d18d27 Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Sat, 9 Mar 2024 14:59:46 +0900 Subject: [PATCH 19/23] =?UTF-8?q?chore=20:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20fixture=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/blackcompany/eeos/auth/fixture/FakeAuthInfo.java | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 BE/eeos/src/test/java/com/blackcompany/eeos/auth/fixture/FakeAuthInfo.java diff --git a/BE/eeos/src/test/java/com/blackcompany/eeos/auth/fixture/FakeAuthInfo.java b/BE/eeos/src/test/java/com/blackcompany/eeos/auth/fixture/FakeAuthInfo.java deleted file mode 100644 index b09460013..000000000 --- a/BE/eeos/src/test/java/com/blackcompany/eeos/auth/fixture/FakeAuthInfo.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.blackcompany.eeos.auth.fixture; - -import com.blackcompany.eeos.auth.persistence.AuthInfoEntity; - -public class FakeAuthInfo { - public static AuthInfoEntity authInfoEntity() { - return AuthInfoEntity.builder().memberId(1L).token("token").build(); - } -} From 98d292f4fa46d3b2a4410d199b95c4a8c78e63e0 Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Sat, 9 Mar 2024 15:00:29 +0900 Subject: [PATCH 20/23] =?UTF-8?q?fix=20:=20=EB=B3=84=EB=8F=84=EC=9D=98=20a?= =?UTF-8?q?uthService=20=EC=83=9D=EC=84=B1=EC=9C=BC=EB=A1=9C=20=EC=9D=B8?= =?UTF-8?q?=ED=95=9C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/AuthFacadeServiceTest.java | 2 + .../application/service/AuthServiceTest.java | 66 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/AuthServiceTest.java diff --git a/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/AuthFacadeServiceTest.java b/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/AuthFacadeServiceTest.java index efadf8dbf..bd66449b4 100644 --- a/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/AuthFacadeServiceTest.java +++ b/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/AuthFacadeServiceTest.java @@ -19,6 +19,7 @@ class AuthFacadeServiceTest { @Mock OauthClientService oauthClientService; @Mock AuthenticationTokenGenerator authenticationTokenGenerator; + @Mock AuthService authService; @InjectMocks AuthFacadeService authFacadeService; @@ -35,6 +36,7 @@ void response_token() { OAuthMemberEntity oAuthMemberEntity = FakeOauthMember.oauthInfoEntity(); when(oauthClientService.getOauthMember(type, authCode, uri)).thenReturn(oauthMemberModel); + when(authService.authenticate(oauthMemberModel)).thenReturn(oAuthMemberEntity); // when authFacadeService.login(type, authCode, uri); diff --git a/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/AuthServiceTest.java b/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/AuthServiceTest.java new file mode 100644 index 000000000..0c7598697 --- /dev/null +++ b/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/AuthServiceTest.java @@ -0,0 +1,66 @@ +package com.blackcompany.eeos.auth.application.service; + +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.blackcompany.eeos.auth.application.domain.converter.OauthMemberEntityConverter; +import com.blackcompany.eeos.auth.fixture.FakeOauthMember; +import com.blackcompany.eeos.auth.persistence.OAuthMemberEntity; +import com.blackcompany.eeos.auth.persistence.OAuthMemberRepository; +import com.blackcompany.eeos.member.application.model.ActiveStatus; +import com.blackcompany.eeos.member.application.model.converter.MemberEntityConverter; +import com.blackcompany.eeos.member.fixture.MemberFixture; +import com.blackcompany.eeos.member.persistence.MemberRepository; +import java.util.Optional; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class AuthServiceTest { + + @Mock MemberRepository memberRepository; + @Mock OAuthMemberRepository oAuthMemberRepository; + @Spy MemberEntityConverter memberEntityConverter; + @Spy OauthMemberEntityConverter oauthMemberEntityConverter; + @InjectMocks AuthService authService; + + @Test + @DisplayName("신규 회원인 경우 oauth에서 가져온 회원 정보를 저장한다.") + void login_existing_user() { + // given + when(oAuthMemberRepository.findByOauthId(FakeOauthMember.oauthMemberModel().getOauthId())) + .thenReturn(Optional.ofNullable(null)); + when(memberRepository.save(Mockito.any())) + .thenReturn(MemberFixture.멤버_엔티티(1L, ActiveStatus.AM)); + + // when + authService.authenticate(FakeOauthMember.oauthMemberModel()); + + // then + assertAll( + () -> verify(memberRepository).save(Mockito.any()), + () -> verify(oAuthMemberRepository).save(Mockito.any())); + } + + @Test + @DisplayName("기존 회원인 경우 존재하던 oauth 정보를 가져온다.") + void login_new_user() { + // given + when(oAuthMemberRepository.findByOauthId(FakeOauthMember.oauthMemberModel().getOauthId())) + .thenReturn(Optional.of(FakeOauthMember.oauthInfoEntity())); + + // when + OAuthMemberEntity entity = authService.authenticate(FakeOauthMember.oauthMemberModel()); + + // then + assertEquals(entity.getOauthId(), FakeOauthMember.oauthMemberModel().getOauthId()); + } +} From 305ed70f84fd8b85faad016f4c1ab0ed43981513 Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Sat, 9 Mar 2024 15:00:47 +0900 Subject: [PATCH 21/23] =?UTF-8?q?fix=20:=20test=ED=99=98=EA=B2=BD=EC=97=90?= =?UTF-8?q?=EC=84=9C=20local=EC=9A=A9=20db=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/resources/application-test-api.yml | 8 ++++--- .../test/resources/application-test-mysql.yml | 21 ------------------- BE/eeos/src/test/resources/application.yml | 3 ++- 3 files changed, 7 insertions(+), 25 deletions(-) delete mode 100644 BE/eeos/src/test/resources/application-test-mysql.yml diff --git a/BE/eeos/src/test/resources/application-test-api.yml b/BE/eeos/src/test/resources/application-test-api.yml index 7f27f1ead..883b2cfef 100644 --- a/BE/eeos/src/test/resources/application-test-api.yml +++ b/BE/eeos/src/test/resources/application-test-api.yml @@ -2,10 +2,12 @@ spring: config: activate: on-profile: test-api + cors: allow-origin: urls: test -api: - domain: domain - cookie-key: key \ No newline at end of file +token: + cookie: + domain: domain + path: key \ No newline at end of file diff --git a/BE/eeos/src/test/resources/application-test-mysql.yml b/BE/eeos/src/test/resources/application-test-mysql.yml deleted file mode 100644 index 358d01a22..000000000 --- a/BE/eeos/src/test/resources/application-test-mysql.yml +++ /dev/null @@ -1,21 +0,0 @@ -spring: - config: - activate: - on-profile: test-mysql - datasource: - url: jdbc:mysql://localhost:13309/eeos?useSSL=false&serverTimezone=Asia/Seoul&useLegacyDatetimeCode=false&allowPublicKeyRetrieval=true - username: root - password: root - driver-class-name: com.mysql.cj.jdbc.Driver - jpa: - hibernate: - ddl-auto: create - properties: - hibernate: - format_sql: true - flyway: - enabled: false - -logging: - level: - sql: debug \ No newline at end of file diff --git a/BE/eeos/src/test/resources/application.yml b/BE/eeos/src/test/resources/application.yml index 21c8f965f..3f1c48125 100644 --- a/BE/eeos/src/test/resources/application.yml +++ b/BE/eeos/src/test/resources/application.yml @@ -2,7 +2,8 @@ spring: profiles: group: test: - - test-mysql + - local-mysql + - local-redis - test-api - test-token - test-oauth From c5ebc4085657a33d508688f2eb403298eb775751 Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Sat, 9 Mar 2024 15:01:04 +0900 Subject: [PATCH 22/23] =?UTF-8?q?chore=20:=20lint=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../eeos/auth/application/service/ReissueServiceTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/ReissueServiceTest.java b/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/ReissueServiceTest.java index 67351f062..881afe4c5 100644 --- a/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/ReissueServiceTest.java +++ b/BE/eeos/src/test/java/com/blackcompany/eeos/auth/application/service/ReissueServiceTest.java @@ -18,8 +18,7 @@ @ExtendWith(MockitoExtension.class) class ReissueServiceTest { - @Mock - AuthenticationTokenGenerator authenticationTokenGenerator; + @Mock AuthenticationTokenGenerator authenticationTokenGenerator; @Mock MemberAuthenticationRepository memberAuthenticationRepository; @Mock TokenResolver tokenResolver; From f217d630d22b1837728f7a7f4aa4468cb471af39 Mon Sep 17 00:00:00 2001 From: mando <88534959+kssumin@users.noreply.github.com> Date: Sat, 9 Mar 2024 15:07:23 +0900 Subject: [PATCH 23/23] =?UTF-8?q?fix=20:=20ci=EC=8B=9C=EC=97=90=20local?= =?UTF-8?q?=EC=9A=A9=20db=20=EC=97=B4=EB=8F=84=EB=A1=9D=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/backend-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/backend-ci.yml b/.github/workflows/backend-ci.yml index 93468f561..0e81809b9 100644 --- a/.github/workflows/backend-ci.yml +++ b/.github/workflows/backend-ci.yml @@ -33,7 +33,7 @@ jobs: - name: DB를 실행 run: | - docker-compose -f resources/test-develop-environment/docker-compose.yml up -d + docker-compose -f resources/local-develop-environment/docker-compose.yml up -d sleep 20 - name: Gradle 캐싱