|
| 1 | +package com.sampoom.factory.common.config.jwt; |
| 2 | + |
| 3 | +import com.sampoom.factory.common.config.security.CustomAuthEntryPoint; |
| 4 | +import com.sampoom.factory.common.entity.Role; |
| 5 | +import com.sampoom.factory.common.entity.Workspace; |
| 6 | +import com.sampoom.factory.common.exception.CustomAuthenticationException; |
| 7 | +import com.sampoom.factory.common.response.ErrorStatus; |
| 8 | +import io.jsonwebtoken.Claims; |
| 9 | +import jakarta.servlet.FilterChain; |
| 10 | +import jakarta.servlet.ServletException; |
| 11 | +import jakarta.servlet.http.HttpServletRequest; |
| 12 | +import jakarta.servlet.http.HttpServletResponse; |
| 13 | +import lombok.RequiredArgsConstructor; |
| 14 | +import lombok.extern.slf4j.Slf4j; |
| 15 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 16 | +import org.springframework.security.core.GrantedAuthority; |
| 17 | +import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| 18 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 19 | +import org.springframework.stereotype.Component; |
| 20 | +import org.springframework.web.filter.OncePerRequestFilter; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +@Slf4j |
| 27 | +@Component |
| 28 | +@RequiredArgsConstructor |
| 29 | +public class JwtAuthFilter extends OncePerRequestFilter { |
| 30 | + |
| 31 | + private final JwtProvider jwtProvider; |
| 32 | + private final CustomAuthEntryPoint customAuthEntryPoint; |
| 33 | + |
| 34 | + @Override |
| 35 | + protected void doFilterInternal(HttpServletRequest request, |
| 36 | + HttpServletResponse response, |
| 37 | + FilterChain filterChain) |
| 38 | + throws ServletException, IOException { |
| 39 | + String path = request.getRequestURI(); |
| 40 | + if (path.startsWith("/swagger-ui") || path.startsWith("/v3/api-docs") || path.equals("/swagger-ui.html")) { |
| 41 | + filterChain.doFilter(request, response); |
| 42 | + return; |
| 43 | + } |
| 44 | + try { |
| 45 | + String accessToken = jwtProvider.resolveAccessToken(request); |
| 46 | + if (accessToken == null || accessToken.isBlank()) { |
| 47 | + filterChain.doFilter(request, response); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + Claims claims = jwtProvider.parse(accessToken); |
| 52 | + |
| 53 | + // 토큰 타입 검증 |
| 54 | + String type = claims.get("type", String.class); |
| 55 | + |
| 56 | + // service 토큰 검증 |
| 57 | + if ("service".equals(type)) { |
| 58 | + String role = claims.get("role", String.class); |
| 59 | + String subject = claims.getSubject(); // 토큰 발급자 정보 (auth-service) |
| 60 | + if (role == null) { |
| 61 | + throw new CustomAuthenticationException(ErrorStatus.NULL_TOKEN_ROLE); |
| 62 | + } |
| 63 | + if (role.isBlank()) { |
| 64 | + throw new CustomAuthenticationException(ErrorStatus.BLANK_TOKEN_ROLE); |
| 65 | + } |
| 66 | + if (!role.startsWith("SVC_")) { |
| 67 | + throw new CustomAuthenticationException(ErrorStatus.NOT_SERVICE_TOKEN); |
| 68 | + } |
| 69 | + |
| 70 | + // Feign 내부 호출용 권한 통과 |
| 71 | + UsernamePasswordAuthenticationToken auth = |
| 72 | + new UsernamePasswordAuthenticationToken( |
| 73 | + subject, |
| 74 | + null, |
| 75 | + List.of(new SimpleGrantedAuthority(role)) |
| 76 | + ); |
| 77 | + SecurityContextHolder.getContext().setAuthentication(auth); |
| 78 | + // service 토큰은 더 이상 검증할 필요 없음 |
| 79 | + filterChain.doFilter(request, response); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + // 그 외 토큰 (refresh) 예외 처리 |
| 84 | + if ("refresh".equals(type)) { |
| 85 | + SecurityContextHolder.clearContext(); // 인증 정보 제거 |
| 86 | + throw new CustomAuthenticationException(ErrorStatus.NOT_ACCESS_TOKEN); |
| 87 | + } |
| 88 | + |
| 89 | + // 토큰에서 userId, role 가져오기 |
| 90 | + String userId = claims.getSubject(); |
| 91 | + String roleStr = claims.get("role", String.class); |
| 92 | + String workspaceStr = claims.get("workspace", String.class); |
| 93 | + if (userId == null |
| 94 | + || userId.isBlank() |
| 95 | + || roleStr == null |
| 96 | + || roleStr.isBlank() |
| 97 | + || workspaceStr == null |
| 98 | + || workspaceStr.isBlank() |
| 99 | + ) { |
| 100 | + throw new CustomAuthenticationException(ErrorStatus.INVALID_TOKEN); |
| 101 | + } |
| 102 | + |
| 103 | + Role role; |
| 104 | + Workspace workspace; |
| 105 | + try { |
| 106 | + role = Role.valueOf(roleStr); |
| 107 | + workspace = Workspace.valueOf(workspaceStr); |
| 108 | + } catch (IllegalArgumentException ex) { |
| 109 | + throw new CustomAuthenticationException(ErrorStatus.INVALID_TOKEN); |
| 110 | + } |
| 111 | + |
| 112 | + // 권한 매핑 (Enum Role → Security 권한명) |
| 113 | + String roleAuthority = "ROLE_" + role.name(); |
| 114 | + String workspaceAuthority = "ROLE_" + workspace.name(); |
| 115 | + |
| 116 | + // GrantedAuthority 리스트 생성 |
| 117 | + List<GrantedAuthority> authorities = new ArrayList<>(); |
| 118 | + authorities.add(new SimpleGrantedAuthority(roleAuthority)); |
| 119 | + authorities.add(new SimpleGrantedAuthority(workspaceAuthority)); |
| 120 | + |
| 121 | + UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken( |
| 122 | + userId, null, authorities |
| 123 | + ); |
| 124 | + SecurityContextHolder.getContext().setAuthentication(authentication); |
| 125 | + filterChain.doFilter(request, response); |
| 126 | + } catch (CustomAuthenticationException ex) { |
| 127 | + SecurityContextHolder.clearContext(); |
| 128 | + customAuthEntryPoint.commence(request, response, ex); |
| 129 | + } catch (Exception ex) { |
| 130 | + SecurityContextHolder.clearContext(); |
| 131 | + customAuthEntryPoint.commence(request, response, |
| 132 | + new CustomAuthenticationException(ErrorStatus.INVALID_TOKEN)); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments