|
| 1 | +package com.example.log4u.common.oauth2.jwt; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | + |
| 5 | +import org.springframework.web.filter.GenericFilterBean; |
| 6 | + |
| 7 | +import com.example.log4u.common.oauth2.repository.RefreshTokenRepository; |
| 8 | + |
| 9 | +import io.jsonwebtoken.ExpiredJwtException; |
| 10 | +import jakarta.servlet.FilterChain; |
| 11 | +import jakarta.servlet.ServletException; |
| 12 | +import jakarta.servlet.ServletRequest; |
| 13 | +import jakarta.servlet.ServletResponse; |
| 14 | +import jakarta.servlet.http.Cookie; |
| 15 | +import jakarta.servlet.http.HttpServletRequest; |
| 16 | +import jakarta.servlet.http.HttpServletResponse; |
| 17 | +import lombok.RequiredArgsConstructor; |
| 18 | + |
| 19 | +@RequiredArgsConstructor |
| 20 | +public class JwtLogoutFilter extends GenericFilterBean { |
| 21 | + |
| 22 | + private final JwtUtil jwtUtil; |
| 23 | + private final RefreshTokenRepository refreshTokenRepository; |
| 24 | + |
| 25 | + @Override |
| 26 | + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws |
| 27 | + IOException, |
| 28 | + ServletException { |
| 29 | + doFilter((HttpServletRequest)request, (HttpServletResponse)response, chain); |
| 30 | + } |
| 31 | + |
| 32 | + private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws |
| 33 | + IOException, |
| 34 | + ServletException { |
| 35 | + |
| 36 | + // 경로 확인 |
| 37 | + String requestUri = request.getRequestURI(); |
| 38 | + if (!requestUri.matches("^\\/logout$")) { |
| 39 | + filterChain.doFilter(request, response); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + String requestMethod = request.getMethod(); |
| 44 | + if (!requestMethod.equals("POST")) { |
| 45 | + filterChain.doFilter(request, response); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + // 리프레시 토큰 추출 |
| 50 | + String refresh = null; |
| 51 | + Cookie[] cookies = request.getCookies(); |
| 52 | + for (Cookie cookie : cookies) { |
| 53 | + if (cookie.getName().equals("refresh")) { |
| 54 | + refresh = cookie.getValue(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // 리프레시 토큰 만료 체크 |
| 59 | + if (refresh == null) { |
| 60 | + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + // 만료 검사 |
| 65 | + try { |
| 66 | + jwtUtil.isExpired(refresh); |
| 67 | + } catch (ExpiredJwtException e) { |
| 68 | + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + // 토큰이 refresh인지 확인 (발급시 페이로드에 명시) |
| 73 | + String tokenType = jwtUtil.getTokenType(refresh); |
| 74 | + if (!tokenType.equals("refresh")) { |
| 75 | + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + //DB에 저장되어 있는지 확인 |
| 80 | + Boolean isExist = refreshTokenRepository.existsByRefreshToken(refresh); |
| 81 | + if (Boolean.FALSE.equals(isExist)) { |
| 82 | + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // 로그아웃 |
| 87 | + logout(response, refresh); |
| 88 | + } |
| 89 | + |
| 90 | + public void logout(HttpServletResponse response, String refresh) { |
| 91 | + // DB 에서 리프레시 토큰 제거 |
| 92 | + refreshTokenRepository.deleteByRefreshToken(refresh); |
| 93 | + // 쿠키 제거 |
| 94 | + deleteCookie(response); |
| 95 | + } |
| 96 | + |
| 97 | + public void deleteCookie(HttpServletResponse response) { |
| 98 | + Cookie access = new Cookie("access", null); |
| 99 | + Cookie refresh = new Cookie("refresh", null); |
| 100 | + |
| 101 | + access.setMaxAge(0); |
| 102 | + access.setPath("/"); |
| 103 | + refresh.setMaxAge(0); |
| 104 | + refresh.setPath("/"); |
| 105 | + |
| 106 | + response.addCookie(access); |
| 107 | + response.addCookie(refresh); |
| 108 | + response.setStatus(HttpServletResponse.SC_OK); |
| 109 | + } |
| 110 | +} |
0 commit comments