From 077527a16f920d9a4aa592c431a719eab98ed374 Mon Sep 17 00:00:00 2001 From: suhhyun524 <2835223@ewhain.net> Date: Tue, 13 Aug 2024 00:12:29 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[refact]=20=EB=A6=AC=ED=94=84=EB=A0=88?= =?UTF-8?q?=EC=8B=9C=ED=86=A0=ED=81=B0=20=EB=B0=9C=EA=B8=89=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=88=98=EC=A0=95=20#185?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ceos/backend/domain/admin/AdminController.java | 11 +++++++---- .../backend/domain/admin/service/AdminService.java | 12 ++++++++---- .../java/ceos/backend/global/config/RedisConfig.java | 3 --- .../backend/global/config/jwt/TokenProvider.java | 8 ++++---- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/main/java/ceos/backend/domain/admin/AdminController.java b/src/main/java/ceos/backend/domain/admin/AdminController.java index a982cf6e..3989a060 100644 --- a/src/main/java/ceos/backend/domain/admin/AdminController.java +++ b/src/main/java/ceos/backend/domain/admin/AdminController.java @@ -7,6 +7,7 @@ import ceos.backend.global.config.user.AdminDetails; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.servlet.http.HttpServletRequest; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -38,9 +39,10 @@ public void signUp(@RequestBody @Valid SignUpRequest signUpRequest) { @Operation(summary = "로그인") @PostMapping("/signin") - public TokenResponse signIn(@RequestBody @Valid SignInRequest signInRequest) { + public TokenResponse signIn(HttpServletRequest request, @RequestBody @Valid SignInRequest signInRequest) { log.info("로그인"); - return adminService.signIn(signInRequest); + String device = request.getHeader("User-Agent").contains("mobile") ? "mobile" : "web"; + return adminService.signIn(device, signInRequest); } @Operation(summary = "아이디 찾기") @@ -68,9 +70,10 @@ public void resetPwd( @Operation(summary = "로그아웃") @PostMapping("/logout") - public void logout(@AuthenticationPrincipal AdminDetails adminUser) { + public void logout(HttpServletRequest request, @AuthenticationPrincipal AdminDetails adminUser) { log.info("로그아웃"); - adminService.logout(adminUser); + String device = request.getHeader("User-Agent").contains("mobile") ? "mobile" : "web"; + adminService.logout(device, adminUser); } @Operation(summary = "토큰 재발급") diff --git a/src/main/java/ceos/backend/domain/admin/service/AdminService.java b/src/main/java/ceos/backend/domain/admin/service/AdminService.java index 08c2c54e..7179b5ae 100644 --- a/src/main/java/ceos/backend/domain/admin/service/AdminService.java +++ b/src/main/java/ceos/backend/domain/admin/service/AdminService.java @@ -51,16 +51,18 @@ public void signUp(SignUpRequest signUpRequest) { } @Transactional - public TokenResponse signIn(SignInRequest signInRequest) { + public TokenResponse signIn(String device, SignInRequest signInRequest) { final Admin admin = adminHelper.findForSignIn(signInRequest); final Authentication authentication = adminHelper.adminAuthorizationInput(admin); adminHelper.checkRole(admin); + String redisKey = admin.getId().toString() + ":" + device; + // 토큰 발급 final String accessToken = tokenProvider.createAccessToken(admin.getId(), authentication); - final String refreshToken = tokenProvider.createRefreshToken(admin.getId(), authentication); + final String refreshToken = tokenProvider.createRefreshToken(admin.getId(), authentication, redisKey); return adminMapper.toTokenResponse(accessToken, refreshToken); } @@ -97,11 +99,13 @@ public void resetPwd(ResetPwdRequest resetPwdRequest, AdminDetails adminUser) { } @Transactional - public void logout(AdminDetails adminUser) { + public void logout(String device, AdminDetails adminUser) { final Admin admin = adminUser.getAdmin(); + String redisKey = admin.getId().toString() + ":" + device; + // 레디스 삭제 - tokenProvider.deleteRefreshToken(admin.getId()); + tokenProvider.deleteRefreshToken(redisKey); } @Transactional diff --git a/src/main/java/ceos/backend/global/config/RedisConfig.java b/src/main/java/ceos/backend/global/config/RedisConfig.java index a1570ec5..a43ee2dc 100644 --- a/src/main/java/ceos/backend/global/config/RedisConfig.java +++ b/src/main/java/ceos/backend/global/config/RedisConfig.java @@ -25,10 +25,7 @@ public RedisConnectionFactory redisConnectionFactory() { @Bean public RedisTemplate redisTemplate() { - // redisTemplate를 받아와서 set, get, delete를 사용 RedisTemplate redisTemplate = new RedisTemplate<>(); - // setKeySerializer, setValueSerializer 설정 - // redis-cli을 통해 직접 데이터를 조회 시 알아볼 수 없는 형태로 출력되는 것을 방지 redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new StringRedisSerializer()); redisTemplate.setConnectionFactory(redisConnectionFactory()); diff --git a/src/main/java/ceos/backend/global/config/jwt/TokenProvider.java b/src/main/java/ceos/backend/global/config/jwt/TokenProvider.java index d9f7ca90..5d1268e3 100644 --- a/src/main/java/ceos/backend/global/config/jwt/TokenProvider.java +++ b/src/main/java/ceos/backend/global/config/jwt/TokenProvider.java @@ -82,7 +82,7 @@ public String createAccessToken(Long id, Authentication authentication) { .compact(); } - public String createRefreshToken(Long id, Authentication authentication) { + public String createRefreshToken(Long id, Authentication authentication, String redisKey) { String authorities = authentication.getAuthorities().stream() .map(GrantedAuthority::getAuthority) @@ -107,13 +107,13 @@ public String createRefreshToken(Long id, Authentication authentication) { redisTemplate .opsForValue() - .set(id.toString(), refreshToken, refreshExpirationTime, TimeUnit.SECONDS); + .set(redisKey, refreshToken, refreshExpirationTime, TimeUnit.SECONDS); return refreshToken; } - public void deleteRefreshToken(Long id) { - redisTemplate.delete(id.toString()); + public void deleteRefreshToken(String redisKey) { + redisTemplate.delete(redisKey); } public String getTokenUserId(String token) { From 6382d90c1140dc4f5a2d34cff12d240fcdcfad5f Mon Sep 17 00:00:00 2001 From: suhhyun524 <2835223@ewhain.net> Date: Wed, 14 Aug 2024 01:25:39 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[refact]=20=EC=83=81=EC=88=98=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/ceos/backend/domain/admin/AdminController.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/ceos/backend/domain/admin/AdminController.java b/src/main/java/ceos/backend/domain/admin/AdminController.java index 3989a060..439152f6 100644 --- a/src/main/java/ceos/backend/domain/admin/AdminController.java +++ b/src/main/java/ceos/backend/domain/admin/AdminController.java @@ -22,6 +22,8 @@ public class AdminController { private final AdminService adminService; + private static final String MOBILE = "mobile"; + private static final String WEB = "web"; @Operation(summary = "닉네임 확인") @PostMapping("/username") @@ -41,7 +43,7 @@ public void signUp(@RequestBody @Valid SignUpRequest signUpRequest) { @PostMapping("/signin") public TokenResponse signIn(HttpServletRequest request, @RequestBody @Valid SignInRequest signInRequest) { log.info("로그인"); - String device = request.getHeader("User-Agent").contains("mobile") ? "mobile" : "web"; + String device = request.getHeader("User-Agent").contains("mobile") ? MOBILE : WEB; return adminService.signIn(device, signInRequest); } @@ -72,7 +74,7 @@ public void resetPwd( @PostMapping("/logout") public void logout(HttpServletRequest request, @AuthenticationPrincipal AdminDetails adminUser) { log.info("로그아웃"); - String device = request.getHeader("User-Agent").contains("mobile") ? "mobile" : "web"; + String device = request.getHeader("User-Agent").contains("mobile") ? MOBILE : WEB; adminService.logout(device, adminUser); }