From bad722265bf067f8cc955494854b25857b180252 Mon Sep 17 00:00:00 2001 From: Jin Hyuk Date: Tue, 8 Oct 2024 11:56:49 +0900 Subject: [PATCH] feat/#507: spotless --- .../adapter/in/auth/AuthController.java | 17 ++++++----- .../application/port/in/auth/AuthUsecase.java | 9 +++--- .../application/service/auth/AuthService.java | 23 ++++++++------ .../docs/auth/ReissueTokenExceptionDocs.java | 5 ++-- .../exceed/common/dto/HttpRequestDTO.java | 3 +- .../exceed/common/dto/ReissueTokenDTO.java | 3 +- .../common/exception/auth/AuthError.java | 3 +- .../auth/NotFoundRefreshTokenException.java | 8 ++--- .../common/security/domain/JwtManager.java | 30 +++++++++++-------- .../security/exception/SecurityErrorCode.java | 7 +++-- 10 files changed, 59 insertions(+), 49 deletions(-) diff --git a/BE/exceed/src/main/java/com/gaebaljip/exceed/adapter/in/auth/AuthController.java b/BE/exceed/src/main/java/com/gaebaljip/exceed/adapter/in/auth/AuthController.java index 0d751767..5abeb527 100644 --- a/BE/exceed/src/main/java/com/gaebaljip/exceed/adapter/in/auth/AuthController.java +++ b/BE/exceed/src/main/java/com/gaebaljip/exceed/adapter/in/auth/AuthController.java @@ -5,10 +5,6 @@ import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; -import com.gaebaljip.exceed.common.docs.auth.ReissueTokenExceptionDocs; -import com.gaebaljip.exceed.common.dto.HttpRequestDTO; -import com.gaebaljip.exceed.common.dto.ReissueTokenDTO; -import com.gaebaljip.exceed.common.exception.auth.NotFoundRefreshTokenException; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -20,7 +16,11 @@ import com.gaebaljip.exceed.common.ApiResponse; import com.gaebaljip.exceed.common.ApiResponseGenerator; import com.gaebaljip.exceed.common.docs.auth.LoginExceptionDocs; +import com.gaebaljip.exceed.common.docs.auth.ReissueTokenExceptionDocs; +import com.gaebaljip.exceed.common.dto.HttpRequestDTO; import com.gaebaljip.exceed.common.dto.LoginResponseDTO; +import com.gaebaljip.exceed.common.dto.ReissueTokenDTO; +import com.gaebaljip.exceed.common.exception.auth.NotFoundRefreshTokenException; import com.gaebaljip.exceed.common.security.AuthConstants; import com.gaebaljip.exceed.common.swagger.ApiErrorExceptionsExample; @@ -52,11 +52,14 @@ public ApiResponse> login( @Operation(summary = "토큰 재발급", description = "토큰 재발급 한다.") @PostMapping("/auth/refresh") @ApiErrorExceptionsExample(ReissueTokenExceptionDocs.class) - public ApiResponse> refresh(HttpServletRequest request, HttpServletResponse response) { + public ApiResponse> refresh( + HttpServletRequest request, HttpServletResponse response) { String accessToken = request.getHeader(AuthConstants.AUTH_HEADER.getValue()); String refreshToken = getCookie(request.getCookies()).getValue(); - HttpRequestDTO httpRequestDTO = HttpRequestDTO.of(request.getRequestURL().toString(), request.getMethod()); - ReissueTokenDTO reissueTokenDTO = authUsecase.reIssueToken(accessToken, refreshToken, httpRequestDTO); + HttpRequestDTO httpRequestDTO = + HttpRequestDTO.of(request.getRequestURL().toString(), request.getMethod()); + ReissueTokenDTO reissueTokenDTO = + authUsecase.reIssueToken(accessToken, refreshToken, httpRequestDTO); response.setHeader(AuthConstants.AUTH_HEADER.getValue(), reissueTokenDTO.accessToken()); setCookie(response, reissueTokenDTO.refreshToken()); return ApiResponseGenerator.success(HttpStatus.OK); diff --git a/BE/exceed/src/main/java/com/gaebaljip/exceed/application/port/in/auth/AuthUsecase.java b/BE/exceed/src/main/java/com/gaebaljip/exceed/application/port/in/auth/AuthUsecase.java index ea3aa5db..7acff1c0 100644 --- a/BE/exceed/src/main/java/com/gaebaljip/exceed/application/port/in/auth/AuthUsecase.java +++ b/BE/exceed/src/main/java/com/gaebaljip/exceed/application/port/in/auth/AuthUsecase.java @@ -1,17 +1,16 @@ package com.gaebaljip.exceed.application.port.in.auth; -import com.gaebaljip.exceed.common.dto.HttpRequestDTO; -import com.gaebaljip.exceed.common.dto.ReissueTokenDTO; import org.springframework.stereotype.Component; import com.gaebaljip.exceed.adapter.in.auth.request.LoginRequest; +import com.gaebaljip.exceed.common.dto.HttpRequestDTO; import com.gaebaljip.exceed.common.dto.LoginResponseDTO; - -import javax.servlet.http.HttpServletRequest; +import com.gaebaljip.exceed.common.dto.ReissueTokenDTO; @Component public interface AuthUsecase { LoginResponseDTO execute(LoginRequest request); - ReissueTokenDTO reIssueToken(String accessToken, String refreshToken, HttpRequestDTO requestDTO); + ReissueTokenDTO reIssueToken( + String accessToken, String refreshToken, HttpRequestDTO requestDTO); } diff --git a/BE/exceed/src/main/java/com/gaebaljip/exceed/application/service/auth/AuthService.java b/BE/exceed/src/main/java/com/gaebaljip/exceed/application/service/auth/AuthService.java index f972cdf2..1b2897fb 100644 --- a/BE/exceed/src/main/java/com/gaebaljip/exceed/application/service/auth/AuthService.java +++ b/BE/exceed/src/main/java/com/gaebaljip/exceed/application/service/auth/AuthService.java @@ -1,5 +1,9 @@ package com.gaebaljip.exceed.application.service.auth; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + import com.gaebaljip.exceed.adapter.in.auth.request.LoginRequest; import com.gaebaljip.exceed.application.domain.member.MemberEntity; import com.gaebaljip.exceed.application.port.in.auth.AuthUsecase; @@ -10,10 +14,8 @@ import com.gaebaljip.exceed.common.exception.auth.PasswordMismatchException; import com.gaebaljip.exceed.common.security.domain.JwtManager; import com.gaebaljip.exceed.common.security.exception.InvalidJwtException; + import lombok.RequiredArgsConstructor; -import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; @Service @RequiredArgsConstructor @@ -30,16 +32,19 @@ public LoginResponseDTO execute(LoginRequest request) { if (!bCryptPasswordEncoder.matches(request.password(), member.getPassword())) { throw PasswordMismatchException.EXECPTION; } - LoginResponseDTO loginResponseDTO = LoginResponseDTO.builder() - .accessToken(jwtManager.generateAccessToken(member.getId())) - .refreshToken(jwtManager.generateRefreshToken(member.getId())) - .build(); + LoginResponseDTO loginResponseDTO = + LoginResponseDTO.builder() + .accessToken(jwtManager.generateAccessToken(member.getId())) + .refreshToken(jwtManager.generateRefreshToken(member.getId())) + .build(); jwtManager.saveRefreshToken(member.getId().toString(), loginResponseDTO.refreshToken()); return loginResponseDTO; } + @Override - public ReissueTokenDTO reIssueToken(String accessToken, String refreshToken, HttpRequestDTO requestDTO) { - if(jwtManager.validateRefreshToken(refreshToken, requestDTO)) { + public ReissueTokenDTO reIssueToken( + String accessToken, String refreshToken, HttpRequestDTO requestDTO) { + if (jwtManager.validateRefreshToken(refreshToken, requestDTO)) { return jwtManager.reissueToken(accessToken); } throw InvalidJwtException.EXECPTION; diff --git a/BE/exceed/src/main/java/com/gaebaljip/exceed/common/docs/auth/ReissueTokenExceptionDocs.java b/BE/exceed/src/main/java/com/gaebaljip/exceed/common/docs/auth/ReissueTokenExceptionDocs.java index aea72b41..6f4c38cb 100644 --- a/BE/exceed/src/main/java/com/gaebaljip/exceed/common/docs/auth/ReissueTokenExceptionDocs.java +++ b/BE/exceed/src/main/java/com/gaebaljip/exceed/common/docs/auth/ReissueTokenExceptionDocs.java @@ -1,9 +1,7 @@ package com.gaebaljip.exceed.common.docs.auth; import com.gaebaljip.exceed.common.exception.EatCeedException; -import com.gaebaljip.exceed.common.exception.auth.AuthError; import com.gaebaljip.exceed.common.exception.auth.NotFoundRefreshTokenException; -import com.gaebaljip.exceed.common.security.exception.InvalidJwtException; import com.gaebaljip.exceed.common.security.exception.SecurityErrorCode; import com.gaebaljip.exceed.common.swagger.ExceptionDoc; import com.gaebaljip.exceed.common.swagger.ExplainError; @@ -21,5 +19,6 @@ public class ReissueTokenExceptionDocs implements SwaggerExampleExceptions { public EatCeedException 리프레시_토큰이_만료_되었을_때 = new EatCeedException(SecurityErrorCode.EXPIRED_JWT); @ExplainError("리프레시 토큰이 서버에서 지원하는 토큰이 아닐 떄") - public EatCeedException 리프레시_토큰이_서버에서_지원하는_토큰이_아닐_때 = new EatCeedException(SecurityErrorCode.UNSUPPORTED_JWT); + public EatCeedException 리프레시_토큰이_서버에서_지원하는_토큰이_아닐_때 = + new EatCeedException(SecurityErrorCode.UNSUPPORTED_JWT); } diff --git a/BE/exceed/src/main/java/com/gaebaljip/exceed/common/dto/HttpRequestDTO.java b/BE/exceed/src/main/java/com/gaebaljip/exceed/common/dto/HttpRequestDTO.java index 71d39192..aee223cd 100644 --- a/BE/exceed/src/main/java/com/gaebaljip/exceed/common/dto/HttpRequestDTO.java +++ b/BE/exceed/src/main/java/com/gaebaljip/exceed/common/dto/HttpRequestDTO.java @@ -4,8 +4,7 @@ public record HttpRequestDTO(String url, String method) { @Builder - public HttpRequestDTO { - } + public HttpRequestDTO {} public static HttpRequestDTO of(String url, String method) { return new HttpRequestDTO(url, method); diff --git a/BE/exceed/src/main/java/com/gaebaljip/exceed/common/dto/ReissueTokenDTO.java b/BE/exceed/src/main/java/com/gaebaljip/exceed/common/dto/ReissueTokenDTO.java index 0c8ab299..bba6426c 100644 --- a/BE/exceed/src/main/java/com/gaebaljip/exceed/common/dto/ReissueTokenDTO.java +++ b/BE/exceed/src/main/java/com/gaebaljip/exceed/common/dto/ReissueTokenDTO.java @@ -4,6 +4,5 @@ public record ReissueTokenDTO(String accessToken, String refreshToken) { @Builder - public ReissueTokenDTO { - } + public ReissueTokenDTO {} } diff --git a/BE/exceed/src/main/java/com/gaebaljip/exceed/common/exception/auth/AuthError.java b/BE/exceed/src/main/java/com/gaebaljip/exceed/common/exception/auth/AuthError.java index 17dab68b..e772b7e4 100644 --- a/BE/exceed/src/main/java/com/gaebaljip/exceed/common/exception/auth/AuthError.java +++ b/BE/exceed/src/main/java/com/gaebaljip/exceed/common/exception/auth/AuthError.java @@ -15,8 +15,7 @@ public enum AuthError implements BaseError { PASSWORD_MISMATCH(400, "5003", "비밀번호가 일치하지 않습니다."), MEMBER_NOT_CHECKED(400, "5004", "해당 회원은 이메일 검증이 완료되지 않았습니다."), - NOT_FOUND_REFRESHTOKEN(400, "5005", "리프레시 토큰이 존재하지 않습니다.") - ; + NOT_FOUND_REFRESHTOKEN(400, "5005", "리프레시 토큰이 존재하지 않습니다."); private final Integer status; private final String code; diff --git a/BE/exceed/src/main/java/com/gaebaljip/exceed/common/exception/auth/NotFoundRefreshTokenException.java b/BE/exceed/src/main/java/com/gaebaljip/exceed/common/exception/auth/NotFoundRefreshTokenException.java index 4de1b394..cf6250b7 100644 --- a/BE/exceed/src/main/java/com/gaebaljip/exceed/common/exception/auth/NotFoundRefreshTokenException.java +++ b/BE/exceed/src/main/java/com/gaebaljip/exceed/common/exception/auth/NotFoundRefreshTokenException.java @@ -4,9 +4,9 @@ public class NotFoundRefreshTokenException extends EatCeedException { - public static EatCeedException EXECPTION = new NotFoundRefreshTokenException(); + public static EatCeedException EXECPTION = new NotFoundRefreshTokenException(); - public NotFoundRefreshTokenException() { - super(AuthError.NOT_FOUND_REFRESHTOKEN); - } + public NotFoundRefreshTokenException() { + super(AuthError.NOT_FOUND_REFRESHTOKEN); + } } diff --git a/BE/exceed/src/main/java/com/gaebaljip/exceed/common/security/domain/JwtManager.java b/BE/exceed/src/main/java/com/gaebaljip/exceed/common/security/domain/JwtManager.java index 6899b0a0..65024d51 100644 --- a/BE/exceed/src/main/java/com/gaebaljip/exceed/common/security/domain/JwtManager.java +++ b/BE/exceed/src/main/java/com/gaebaljip/exceed/common/security/domain/JwtManager.java @@ -1,5 +1,15 @@ package com.gaebaljip.exceed.common.security.domain; +import java.security.Key; +import java.time.LocalDateTime; +import java.util.Date; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.security.core.AuthenticationException; +import org.springframework.stereotype.Component; + import com.gaebaljip.exceed.adapter.out.redis.RedisAdapter; import com.gaebaljip.exceed.common.dto.HttpRequestDTO; import com.gaebaljip.exceed.common.dto.ReissueTokenDTO; @@ -7,26 +17,19 @@ import com.gaebaljip.exceed.common.security.exception.ExpiredJwtException; import com.gaebaljip.exceed.common.security.exception.InvalidJwtException; import com.gaebaljip.exceed.common.security.exception.UnSupportedJwtException; + import io.jsonwebtoken.*; import io.jsonwebtoken.io.Decoders; import io.jsonwebtoken.security.Keys; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.security.core.AuthenticationException; -import org.springframework.stereotype.Component; - -import javax.servlet.http.HttpServletRequest; -import java.security.Key; -import java.time.LocalDateTime; -import java.util.Date; @Component @Slf4j public class JwtManager { - private static final long ACCESS_TOKEN_EXPIRE_TIME = 1000 * 60 * 60 * 24 * 3; // 3일 + private static final long ACCESS_TOKEN_EXPIRE_TIME = 1000 * 60; // 3일 private static final long REFRESH_TOKEN_EXPIRE_TIME = 1000 * 60 * 60 * 24 * 7; // 7일 private final Key key; - private RedisAdapter redisAdapter; + private RedisAdapter redisAdapter; public JwtManager(@Value("${jwt.secret}") String secretKey, RedisAdapter redisAdapter) { byte[] keyBytes = Decoders.BASE64.decode(secretKey); @@ -170,10 +173,13 @@ public Claims parseClaims(String Token) { public ReissueTokenDTO reissueToken(String accessToken) { String accessTokenMemberId = parseClaims(accessToken).getSubject(); - String refreshToken = redisAdapter.query(accessTokenMemberId).orElseThrow(() -> NotFoundRefreshTokenException.EXECPTION); + String refreshToken = + redisAdapter + .query(accessTokenMemberId) + .orElseThrow(() -> NotFoundRefreshTokenException.EXECPTION); String refreshTokenMemberId = parseClaims(refreshToken).getSubject(); - if(accessTokenMemberId.equals(refreshTokenMemberId)) { + if (accessTokenMemberId.equals(refreshTokenMemberId)) { return ReissueTokenDTO.builder() .accessToken(generateAccessToken(Long.parseLong(accessTokenMemberId))) .refreshToken(generateRefreshToken(Long.parseLong(refreshTokenMemberId))) diff --git a/BE/exceed/src/main/java/com/gaebaljip/exceed/common/security/exception/SecurityErrorCode.java b/BE/exceed/src/main/java/com/gaebaljip/exceed/common/security/exception/SecurityErrorCode.java index 19cad25d..ff7cff85 100644 --- a/BE/exceed/src/main/java/com/gaebaljip/exceed/common/security/exception/SecurityErrorCode.java +++ b/BE/exceed/src/main/java/com/gaebaljip/exceed/common/security/exception/SecurityErrorCode.java @@ -1,14 +1,15 @@ package com.gaebaljip.exceed.common.security.exception; +import java.lang.reflect.Field; +import java.util.Objects; + import com.gaebaljip.exceed.common.Error; import com.gaebaljip.exceed.common.exception.BaseError; import com.gaebaljip.exceed.common.swagger.ExplainError; + import lombok.AllArgsConstructor; import lombok.Getter; -import java.lang.reflect.Field; -import java.util.Objects; - @Getter @AllArgsConstructor public enum SecurityErrorCode implements BaseError {