Skip to content

Commit

Permalink
feat: 필터에 TokenExpiredException 익셉션 처리하도록 작성 (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
mybloom committed Jun 11, 2022
1 parent 4917712 commit bf9a637
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions BE/src/main/java/org/team4/airbnb/filter/AuthCheckFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.jsonwebtoken.Claims;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import javax.servlet.FilterChain;
Expand All @@ -16,6 +17,7 @@
import org.springframework.web.filter.OncePerRequestFilter;
import org.team4.airbnb.auth.JwtTokenProvider;
import org.team4.airbnb.exception.ErrorResponse;
import org.team4.airbnb.exception.TokenExpiredException;
import org.team4.airbnb.exception.TokenInValidateException;

@Slf4j
Expand All @@ -34,12 +36,17 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
validateHeader(header);
String accessToken = header.substring("Bearer ".length());

jwtTokenProvider.validateJwtToken(accessToken);
// Claims claims = jwtTokenProvider.parseJwtToken(accessToken);
Claims claims = jwtTokenProvider.validateJwtToken(accessToken);

filterChain.doFilter(request, response);
} catch (TokenInValidateException e) {
ErrorResponse errorResponse = new ErrorResponse(new String("invalid token".getBytes(),StandardCharsets.UTF_8));
} catch (TokenInValidateException inValidateException) {
ErrorResponse errorResponse = new ErrorResponse(
new String("invalid token".getBytes(), StandardCharsets.UTF_8));
response.setStatus(HttpStatus.UNAUTHORIZED.value());
response.getWriter().write(convertObjectToJson(errorResponse));
} catch (TokenExpiredException expiredException) {
ErrorResponse errorResponse = new ErrorResponse(
new String("authentication is expired".getBytes(), StandardCharsets.UTF_8));
response.setStatus(HttpStatus.UNAUTHORIZED.value());
response.getWriter().write(convertObjectToJson(errorResponse));
}
Expand Down

0 comments on commit bf9a637

Please sign in to comment.