From 0adcb94833c5b23f52b4e58da4bb54340e392364 Mon Sep 17 00:00:00 2001 From: mybloom Date: Sun, 12 Jun 2022 00:21:18 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20filter=EC=97=90=EC=84=9C=20TokenInValid?= =?UTF-8?q?ateException=20=EC=97=90=20=EB=8C=80=ED=95=9C=20=EC=9D=91?= =?UTF-8?q?=EB=8B=B5=20json=EC=83=9D=EC=84=B1=20=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=B2=98=EB=A6=AC=20(ios-h/airbnb#35)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../team4/airbnb/filter/AuthCheckFilter.java | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/BE/src/main/java/org/team4/airbnb/filter/AuthCheckFilter.java b/BE/src/main/java/org/team4/airbnb/filter/AuthCheckFilter.java index 156d27b44..315901c0e 100644 --- a/BE/src/main/java/org/team4/airbnb/filter/AuthCheckFilter.java +++ b/BE/src/main/java/org/team4/airbnb/filter/AuthCheckFilter.java @@ -1,6 +1,9 @@ package org.team4.airbnb.filter; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; +import java.nio.charset.StandardCharsets; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.annotation.WebFilter; @@ -9,8 +12,10 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; import org.springframework.web.filter.OncePerRequestFilter; import org.team4.airbnb.auth.JwtTokenProvider; +import org.team4.airbnb.exception.ErrorResponse; import org.team4.airbnb.exception.TokenInValidateException; @Slf4j @@ -25,18 +30,32 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse FilterChain filterChain) throws ServletException, IOException { String header = request.getHeader(HttpHeaders.AUTHORIZATION); - validateHeader(header); - String accessToken = header.substring("Bearer ".length()); + try { + validateHeader(header); + String accessToken = header.substring("Bearer ".length()); - jwtTokenProvider.validateJwtToken(accessToken); + jwtTokenProvider.validateJwtToken(accessToken); // Claims claims = jwtTokenProvider.parseJwtToken(accessToken); - filterChain.doFilter(request, response); + filterChain.doFilter(request, response); + } catch (TokenInValidateException e) { + ErrorResponse errorResponse = new ErrorResponse(new String("invalid token".getBytes(),StandardCharsets.UTF_8)); + response.setStatus(HttpStatus.UNAUTHORIZED.value()); + response.getWriter().write(convertObjectToJson(errorResponse)); + } } - private void validateHeader(String header) { + private void validateHeader(String header) throws TokenInValidateException { if (header == null || !header.startsWith("Bearer ")) { throw new TokenInValidateException(); } } + + private String convertObjectToJson(Object object) throws JsonProcessingException { + if (object == null) { + return null; + } + ObjectMapper mapper = new ObjectMapper(); + return mapper.writeValueAsString(object); + } }