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를 활용할 때 중요한 점은:
- 외부 트래픽과 내부 트래픽을 명확히 구분할 것
- 내부 서비스 간 통신은 필요 이상으로 인증 체크하지 말 것
- Gateway Security 설정을 통해 필요한 부분만 보호하고, 나머지는 permitAll로 열어 둘 것
실제 운영 환경에서도 이런 구조를 적용하면 개발과 유지보수가 한결 수월해집니다.
'Spring & SpringBoot' 카테고리의 다른 글
| 💡 Spring Boot에서 @RestControllerAdvice는 어디까지 적용될까? (0) | 2025.07.25 |
|---|---|
| GitHub Actions + Self-hosted Runner로 구성한 MSA 자동 배포 시스템 (2) | 2025.07.22 |
| Spring Boot MSA 구조에서 Auth-Service 개발하며 겪은 실제 문제와 해결 방법 (1) | 2025.07.17 |
| Docker + Spring Boot + Nginx로 나만의 이미지 파일 서버 만들기 (1) | 2025.05.02 |
| Gateway란? Front Desk? Entrance? (0) | 2025.04.15 |