Skip to content

Commit

Permalink
feat/#507: spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
LJH098 committed Oct 8, 2024
1 parent f98c7a3 commit bad7222
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -52,11 +52,14 @@ public ApiResponse<ApiResponse.CustomBody<Void>> login(
@Operation(summary = "토큰 재발급", description = "토큰 재발급 한다.")
@PostMapping("/auth/refresh")
@ApiErrorExceptionsExample(ReissueTokenExceptionDocs.class)
public ApiResponse<ApiResponse.CustomBody<Void>> refresh(HttpServletRequest request, HttpServletResponse response) {
public ApiResponse<ApiResponse.CustomBody<Void>> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@

public record ReissueTokenDTO(String accessToken, String refreshToken) {
@Builder
public ReissueTokenDTO {
}
public ReissueTokenDTO {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
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;
import com.gaebaljip.exceed.common.exception.auth.NotFoundRefreshTokenException;
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);
Expand Down Expand Up @@ -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)))
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit bad7222

Please sign in to comment.