diff --git a/src/main/java/org/bssm/attachit/domain/auth/presentation/AuthController.java b/src/main/java/org/bssm/attachit/domain/auth/presentation/AuthController.java index 6c3b77f..52286c0 100644 --- a/src/main/java/org/bssm/attachit/domain/auth/presentation/AuthController.java +++ b/src/main/java/org/bssm/attachit/domain/auth/presentation/AuthController.java @@ -1,11 +1,13 @@ package org.bssm.attachit.domain.auth.presentation; +import jakarta.servlet.http.HttpServletRequest; import leehj050211.bsmOauth.exception.BsmOAuthCodeNotFoundException; import leehj050211.bsmOauth.exception.BsmOAuthInvalidClientException; import leehj050211.bsmOauth.exception.BsmOAuthTokenNotFoundException; import lombok.RequiredArgsConstructor; import org.bssm.attachit.domain.auth.presentation.dto.response.TokenResponse; -import org.bssm.attachit.domain.auth.service.UserLoginService; +import org.bssm.attachit.domain.auth.service.UserSignInService; +import org.bssm.attachit.domain.auth.service.UserSignOutService; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -16,10 +18,16 @@ @RequestMapping("/auth") public class AuthController { - private final UserLoginService userLoginService; + private final UserSignInService userSignInService; + private final UserSignOutService userSignOutService; @PostMapping public ResponseEntity signIn(@RequestParam("code") String code) throws BsmOAuthInvalidClientException, IOException, BsmOAuthCodeNotFoundException, BsmOAuthTokenNotFoundException { - return userLoginService.execute(code); + return userSignInService.execute(code); + } + + @PostMapping("/signout") + public ResponseEntity signOut(HttpServletRequest httpServletRequest) { + return userSignOutService.execute(httpServletRequest); } } diff --git a/src/main/java/org/bssm/attachit/domain/auth/repository/RefreshTokenRepository.java b/src/main/java/org/bssm/attachit/domain/auth/repository/RefreshTokenRepository.java index c19e448..49dd31f 100644 --- a/src/main/java/org/bssm/attachit/domain/auth/repository/RefreshTokenRepository.java +++ b/src/main/java/org/bssm/attachit/domain/auth/repository/RefreshTokenRepository.java @@ -11,4 +11,6 @@ public interface RefreshTokenRepository extends JpaRepository findByEmail(String email); + + void deleteByRefreshToken(String refreshToken); } diff --git a/src/main/java/org/bssm/attachit/domain/auth/service/UserLoginService.java b/src/main/java/org/bssm/attachit/domain/auth/service/UserSignInService.java similarity index 98% rename from src/main/java/org/bssm/attachit/domain/auth/service/UserLoginService.java rename to src/main/java/org/bssm/attachit/domain/auth/service/UserSignInService.java index f75d7c1..3231362 100644 --- a/src/main/java/org/bssm/attachit/domain/auth/service/UserLoginService.java +++ b/src/main/java/org/bssm/attachit/domain/auth/service/UserSignInService.java @@ -18,7 +18,7 @@ @Service @RequiredArgsConstructor -public class UserLoginService { +public class UserSignInService { private final UserRepository userRepository; private final AuthProperties authProperties; diff --git a/src/main/java/org/bssm/attachit/domain/auth/service/UserSignOutService.java b/src/main/java/org/bssm/attachit/domain/auth/service/UserSignOutService.java new file mode 100644 index 0000000..3a816bc --- /dev/null +++ b/src/main/java/org/bssm/attachit/domain/auth/service/UserSignOutService.java @@ -0,0 +1,28 @@ +package org.bssm.attachit.domain.auth.service; + +import jakarta.servlet.http.HttpServletRequest; +import lombok.RequiredArgsConstructor; +import org.bssm.attachit.domain.auth.repository.RefreshTokenRepository; +import org.bssm.attachit.global.jwt.exception.TokenNotFoundException; +import org.bssm.attachit.global.jwt.util.JwtUtil; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +@Transactional +public class UserSignOutService { + + private final RefreshTokenRepository refreshTokenRepository; + private final JwtUtil jwtUtil; + + public ResponseEntity execute(HttpServletRequest httpServletRequest) { + String refreshToken = httpServletRequest.getHeader("Authorization-refresh").split(" ")[1].trim(); + if (refreshToken.isEmpty()) { + throw TokenNotFoundException.EXCEPTION; + } + refreshTokenRepository.deleteByRefreshToken(refreshToken); + return ResponseEntity.ok("success"); + } +} diff --git a/src/main/java/org/bssm/attachit/global/error/GlobalExceptionHandler.java b/src/main/java/org/bssm/attachit/global/error/GlobalExceptionHandler.java index fc86863..0e7c5c2 100644 --- a/src/main/java/org/bssm/attachit/global/error/GlobalExceptionHandler.java +++ b/src/main/java/org/bssm/attachit/global/error/GlobalExceptionHandler.java @@ -16,7 +16,7 @@ public ResponseEntity handleGlobal(AttachItException e) { final ErrorCode errorCode = e.getErrorCode(); log.error( "\n" + "{\n" + - "\t\"status\": " + errorCode.getStatus() + '\"' + + "\t\"status\": " + errorCode.getStatus() + ",\n\t\"message\": \"" + errorCode.getMessage() + '\"' + "\n}" ); @@ -27,3 +27,4 @@ public ResponseEntity handleGlobal(AttachItException e) { HttpStatus.valueOf(errorCode.getStatus())); } } + diff --git a/src/main/java/org/bssm/attachit/global/error/exception/ErrorCode.java b/src/main/java/org/bssm/attachit/global/error/exception/ErrorCode.java index b22db94..91540c2 100644 --- a/src/main/java/org/bssm/attachit/global/error/exception/ErrorCode.java +++ b/src/main/java/org/bssm/attachit/global/error/exception/ErrorCode.java @@ -1,12 +1,10 @@ package org.bssm.attachit.global.error.exception; -import com.fasterxml.jackson.annotation.JsonFormat; import lombok.AllArgsConstructor; import lombok.Getter; @Getter @AllArgsConstructor -@JsonFormat(shape = JsonFormat.Shape.OBJECT) public enum ErrorCode { USER_NOT_FOUND(404, "유저를 찾을 수 없습니다"), diff --git a/src/main/java/org/bssm/attachit/global/jwt/filter/JwtFilter.java b/src/main/java/org/bssm/attachit/global/jwt/filter/JwtFilter.java index 77c82f3..a022ca1 100644 --- a/src/main/java/org/bssm/attachit/global/jwt/filter/JwtFilter.java +++ b/src/main/java/org/bssm/attachit/global/jwt/filter/JwtFilter.java @@ -23,7 +23,6 @@ public class JwtFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { String token = jwtUtil.resolveToken(request); - if (token != null && !token.isEmpty()) { if (!refreshTokenRepository.existsByAccessToken(token)) { throw LoggedOutAccessTokenException.EXCEPTION; diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 607ffa3..f8fa750 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -10,7 +10,7 @@ spring: max-file-size: 20MB jpa: - show-sql: true + show-sql: false hibernate: ddl-auto: update open-in-view: false