본문 바로가기
Spring & SpringBoot

Spring Cloud Gateway와 MSA 환경에서 내부 서비스 간 인증 처리 전략

by 창따오 2025. 7. 18.
728x90

Spring Boot MSA 환경에서 Auth-Service와 User-Service를 분리하여 개발할 때, 외부 요청과 내부 서비스 간 요청을 구분하여 인증 처리하는 것이 중요합니다.

이번 글에서는 Gateway, Auth-Service, User-Service 간 통신 흐름과 JWT 인증 처리 전략을 정리합니다.


✅ 구조 흐름 정리

요청 주체대상 서비스JWT 검증 여부

사용자 브라우저 Gateway 필수 ✅
Gateway Auth-Service (/auth) permitAll ✅
Gateway User-Service (/internal) permitAll ✅
Auth-Service User-Service (Feign) Gateway 거치지 않음 → JWT 필요 없음

 


✅ 왜 이렇게 설계하는가?

  • 외부 요청 = 사용자 인증 필수
  • 내부 요청 = 서비스 간 신뢰 기반, 오히려 JWT 검증 없이 처리
  • 복잡한 트래픽 구조를 단순화하고 유지보수를 용이하게 하기 위함

✅ Spring Cloud Gateway Security 설정 예시

@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
    http.csrf().disable()
        .authorizeExchange()
        .pathMatchers("/auth/**").permitAll()
        .pathMatchers("/internal/**").permitAll()  // 내부 서비스 호출 용도
        .anyExchange().authenticated()
        .and()
        .addFilterAt(jwtAuthenticationFilter(), SecurityWebFiltersOrder.AUTHENTICATION);
    return http.build();
}

포인트:

  • /auth/** → 사용자 로그인, 회원가입 등 인증 없이 접근
  • /internal/** → 서비스 간 FeignClient, WebClient 호출 시 사용
  • 그 외 → 모두 JWT 인증 필수

✅ 참고 사항

  • Auth-Service → User-Service 호출은 Feign/WebClient 직접 호출 → Gateway를 거치지 않음.
  • 서비스 간 통신을 Gateway로 우회시킬 수도 있지만, 보통은 직접 붙는 구조가 더 효율적.

✅ 마무리

MSA 구조에서 Gateway를 활용할 때 중요한 점은:

  1. 외부 트래픽과 내부 트래픽을 명확히 구분할 것
  2. 내부 서비스 간 통신은 필요 이상으로 인증 체크하지 말 것
  3. Gateway Security 설정을 통해 필요한 부분만 보호하고, 나머지는 permitAll로 열어 둘 것

실제 운영 환경에서도 이런 구조를 적용하면 개발과 유지보수가 한결 수월해집니다.