Skip to content

Commit

Permalink
feat: 로직에 로깅 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
maiload committed Sep 7, 2024
1 parent 557be51 commit b621c30
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,6 +38,7 @@ public class UserController {
public ResponseEntity<Void> signUp(@Valid @RequestBody SignUpRequest signUpRequest) {
String encodedPassword = passwordEncoder.encode(signUpRequest.password());
userService.saveUser(signUpRequest.toDto(encodedPassword));
log.info("UserController.signUp");
return SuccessResponse.created();
}

Expand All @@ -48,6 +51,7 @@ public ResponseEntity<Void> signUp(@Valid @RequestBody SignUpRequest signUpReque
@PostMapping("/login")
public ResponseEntity<Object> login(@Valid @RequestBody LogInRequest logInRequest, HttpServletResponse response) {
TokenInfo tokenInfo = userService.logInUser(response, logInRequest.toDto());
log.info("UserController.login");
return SuccessResponse.ok(tokenInfo.accessToken());
}

Expand All @@ -59,6 +63,7 @@ public ResponseEntity<Object> login(@Valid @RequestBody LogInRequest logInReques
@PostMapping("/logout")
public ResponseEntity<Void> logout(HttpServletRequest request, HttpServletResponse response) {
userService.logOutUser(request, response);
log.info("UserController.logout");
return SuccessResponse.ok();
}

Expand All @@ -70,6 +75,7 @@ public ResponseEntity<Void> logout(HttpServletRequest request, HttpServletRespon
@GetMapping("/reissue")
public ResponseEntity<Object> reissue(HttpServletRequest request, HttpServletResponse response) {
TokenInfo tokenInfo = userService.reissueToken(request, response);
log.info("UserController.reissue");
return SuccessResponse.ok(tokenInfo.accessToken());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ public static ResponseEntity<Object> ok(Object data) {
return ResponseEntity.ok(data);
}

public static ResponseEntity<Void> ok() {
return ResponseEntity.ok().build();
}
public static ResponseEntity<Void> ok() {return ResponseEntity.ok().build(); }

public static ResponseEntity<Void> noContent() {
return ResponseEntity.noContent().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
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;
import org.springframework.web.filter.GenericFilterBean;

import java.io.IOException;

@Slf4j
@Component
@RequiredArgsConstructor
public class JwtAuthenticationFilter extends GenericFilterBean {
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -42,6 +44,8 @@ public static String getSecureCookie(HttpServletRequest request) {
break;
}
}
}else{
throw new JwtTokenException(Error.CANNOT_FIND_REFRESH_TOKEN_COOKIE);
}

return refreshToken;
Expand Down

0 comments on commit b621c30

Please sign in to comment.