From b621c3024b4ffead40df37728c9b28d32fa8b3b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=EC=A7=84?= Date: Sat, 7 Sep 2024 11:06:50 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=A1=9C=EC=A7=81=EC=97=90=20=EB=A1=9C?= =?UTF-8?q?=EA=B9=85=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/UserController.java | 6 ++++++ .../ktbgratitudediary/exception/Error.java | 3 ++- .../response/SuccessResponse.java | 4 +--- .../security/filter/JwtAuthenticationFilter.java | 15 +++++++++++++-- .../ktbgratitudediary/service/UserService.java | 3 ++- .../ktbgratitudediary/util/CookieUtil.java | 4 ++++ 6 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/main/java/ktb/hackathon/ktbgratitudediary/controller/UserController.java b/src/main/java/ktb/hackathon/ktbgratitudediary/controller/UserController.java index 432b3db..7b4cb6c 100644 --- a/src/main/java/ktb/hackathon/ktbgratitudediary/controller/UserController.java +++ b/src/main/java/ktb/hackathon/ktbgratitudediary/controller/UserController.java @@ -15,10 +15,12 @@ import ktb.hackathon.ktbgratitudediary.response.SuccessResponse; import ktb.hackathon.ktbgratitudediary.service.UserService; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.http.ResponseEntity; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.web.bind.annotation.*; +@Slf4j @RestController @RequestMapping("/api/v1/users/") @RequiredArgsConstructor @@ -36,6 +38,7 @@ public class UserController { public ResponseEntity signUp(@Valid @RequestBody SignUpRequest signUpRequest) { String encodedPassword = passwordEncoder.encode(signUpRequest.password()); userService.saveUser(signUpRequest.toDto(encodedPassword)); + log.info("UserController.signUp"); return SuccessResponse.created(); } @@ -48,6 +51,7 @@ public ResponseEntity signUp(@Valid @RequestBody SignUpRequest signUpReque @PostMapping("/login") public ResponseEntity login(@Valid @RequestBody LogInRequest logInRequest, HttpServletResponse response) { TokenInfo tokenInfo = userService.logInUser(response, logInRequest.toDto()); + log.info("UserController.login"); return SuccessResponse.ok(tokenInfo.accessToken()); } @@ -59,6 +63,7 @@ public ResponseEntity login(@Valid @RequestBody LogInRequest logInReques @PostMapping("/logout") public ResponseEntity logout(HttpServletRequest request, HttpServletResponse response) { userService.logOutUser(request, response); + log.info("UserController.logout"); return SuccessResponse.ok(); } @@ -70,6 +75,7 @@ public ResponseEntity logout(HttpServletRequest request, HttpServletRespon @GetMapping("/reissue") public ResponseEntity reissue(HttpServletRequest request, HttpServletResponse response) { TokenInfo tokenInfo = userService.reissueToken(request, response); + log.info("UserController.reissue"); return SuccessResponse.ok(tokenInfo.accessToken()); } } diff --git a/src/main/java/ktb/hackathon/ktbgratitudediary/exception/Error.java b/src/main/java/ktb/hackathon/ktbgratitudediary/exception/Error.java index a6e767c..7ae2fd8 100644 --- a/src/main/java/ktb/hackathon/ktbgratitudediary/exception/Error.java +++ b/src/main/java/ktb/hackathon/ktbgratitudediary/exception/Error.java @@ -14,7 +14,8 @@ public enum Error { BLACKLIST_TOKEN(HttpStatus.UNAUTHORIZED.value(), 4010), BROKEN_TOKEN(HttpStatus.UNAUTHORIZED.value(), 4011), CANNOT_FIND_REFRESH_TOKEN_COOKIE(HttpStatus.UNAUTHORIZED.value(), 4012), - ACCESS_TOKEN_EXPIRED(HttpStatus.UNAUTHORIZED.value(), 4013); + ACCESS_TOKEN_EXPIRED(HttpStatus.UNAUTHORIZED.value(), 4013), + REFRESH_TOKEN_IS_NULL_OR_EMPTY(HttpStatus.UNAUTHORIZED.value(), 4014); private final int httpStatus; private final int detailCode; diff --git a/src/main/java/ktb/hackathon/ktbgratitudediary/response/SuccessResponse.java b/src/main/java/ktb/hackathon/ktbgratitudediary/response/SuccessResponse.java index d3c4162..49dcfcd 100644 --- a/src/main/java/ktb/hackathon/ktbgratitudediary/response/SuccessResponse.java +++ b/src/main/java/ktb/hackathon/ktbgratitudediary/response/SuccessResponse.java @@ -12,9 +12,7 @@ public static ResponseEntity ok(Object data) { return ResponseEntity.ok(data); } - public static ResponseEntity ok() { - return ResponseEntity.ok().build(); - } + public static ResponseEntity ok() {return ResponseEntity.ok().build(); } public static ResponseEntity noContent() { return ResponseEntity.noContent().build(); diff --git a/src/main/java/ktb/hackathon/ktbgratitudediary/security/filter/JwtAuthenticationFilter.java b/src/main/java/ktb/hackathon/ktbgratitudediary/security/filter/JwtAuthenticationFilter.java index 8314658..62087c6 100644 --- a/src/main/java/ktb/hackathon/ktbgratitudediary/security/filter/JwtAuthenticationFilter.java +++ b/src/main/java/ktb/hackathon/ktbgratitudediary/security/filter/JwtAuthenticationFilter.java @@ -5,8 +5,10 @@ import jakarta.servlet.ServletRequest; import jakarta.servlet.ServletResponse; import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import ktb.hackathon.ktbgratitudediary.security.JwtTokenProvider; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; @@ -14,6 +16,7 @@ import java.io.IOException; +@Slf4j @Component @RequiredArgsConstructor public class JwtAuthenticationFilter extends GenericFilterBean { @@ -22,9 +25,17 @@ public class JwtAuthenticationFilter extends GenericFilterBean { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { - String token = resolveToken((HttpServletRequest) servletRequest); + HttpServletRequest request = (HttpServletRequest) servletRequest; + log.info("RequestURI: {}", request.getRequestURI()); + String token = resolveToken(request); + if(token == null) { + log.error("AccessToken is Null"); + ((HttpServletResponse) servletResponse) + .sendError(HttpServletResponse.SC_UNAUTHORIZED, "AccessToken is Null"); + return; + } - if (token != null && jwtTokenProvider.validateToken(token)) { + if (jwtTokenProvider.validateToken(token)) { var authentication = jwtTokenProvider.getAuthentication(token); SecurityContextHolder.getContext().setAuthentication(authentication); } diff --git a/src/main/java/ktb/hackathon/ktbgratitudediary/service/UserService.java b/src/main/java/ktb/hackathon/ktbgratitudediary/service/UserService.java index 445654c..ec0514e 100644 --- a/src/main/java/ktb/hackathon/ktbgratitudediary/service/UserService.java +++ b/src/main/java/ktb/hackathon/ktbgratitudediary/service/UserService.java @@ -19,6 +19,7 @@ import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; import java.util.Optional; @@ -59,7 +60,7 @@ public void logOutUser(HttpServletRequest request, HttpServletResponse response) private String checkRefreshToken(HttpServletRequest request) { String refreshToken = CookieUtil.getSecureCookie(request); - if (refreshToken == null) throw new JwtTokenException(Error.CANNOT_FIND_REFRESH_TOKEN_COOKIE); + if (!StringUtils.hasText(refreshToken)) throw new JwtTokenException(Error.REFRESH_TOKEN_IS_NULL_OR_EMPTY); jwtTokenProvider.validateToken(refreshToken); Boolean isBlockedToken = blackListTokenRepository.existsByToken(refreshToken); if (isBlockedToken) { diff --git a/src/main/java/ktb/hackathon/ktbgratitudediary/util/CookieUtil.java b/src/main/java/ktb/hackathon/ktbgratitudediary/util/CookieUtil.java index 6aaf312..1f9495f 100755 --- a/src/main/java/ktb/hackathon/ktbgratitudediary/util/CookieUtil.java +++ b/src/main/java/ktb/hackathon/ktbgratitudediary/util/CookieUtil.java @@ -3,6 +3,8 @@ import jakarta.servlet.http.Cookie; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import ktb.hackathon.ktbgratitudediary.exception.Error; +import ktb.hackathon.ktbgratitudediary.exception.JwtTokenException; public class CookieUtil { private static final String REFRESH_TOKEN = "refreshToken"; @@ -42,6 +44,8 @@ public static String getSecureCookie(HttpServletRequest request) { break; } } + }else{ + throw new JwtTokenException(Error.CANNOT_FIND_REFRESH_TOKEN_COOKIE); } return refreshToken;