Java 스프링 프로젝트에서 다중 도메인 구조 구현

반응형

다중 도메인을 사용하는 스프링 프로젝트 구조를 살펴보고, Host 헤더를 통한 경로 분기 처리 방법을 알아보겠습니다.

프로젝트 개요

이 프로젝트는 하나의 스프링 애플리케이션에서 세 가지 다른 도메인을 처리합니다:

  • admin.example.com: 관리자 전용 페이지
  • customer.example.com: 고객용 페이지
  • gtc.example.com: GTC(Global Trading Center) 페이지

개발 환경 접속 주소

로컬에서 개발 시 다음 URL로 각 서비스에 접근할 수 있습니다:

  • domain router 적용 : http://localhost:8888/
  • 관리자: http://localhost:8888/admin/dashboard
  • 대고객: http://localhost:8888/go-customer
  • GTC: http://localhost:8888/go-gtc

프로젝트 구조 및 특징

1. 관리자(admin)

  • Java 백엔드 개발
  • 게시판 등 관리 기능
  • Thymeleaf 템플릿 엔진 사용

2. 대고객(customer)

  • 모든 트랜잭션(TR) 전송 API 개발
  • Vue.js 프론트엔드
  • 빌드된 Vue.js 파일 사용

3. GTC(gtc)

  • Vue.js 프론트엔드 개발
  • 빌드된 Vue.js 파일 사용

핵심 기능: Host 헤더 기반 라우팅

이 프로젝트의 가장 중요한 부분은 URL 경로가 아닌 Host 헤더를 확인하여 요청을 적절한 페이지로 라우팅하는 방식입니다:

private static final Map<String, String> HOST_TO_VIEW = Map.of(
    "gtc.example.com", "forward:/gtc/index.html",
    "customer.example.com", "forward:/customer/index.html",
    "admin.example.com", "admin/dashboard",
    "localhost", "forward:/customer/index.html"
);

@GetMapping("/")
public String index(HttpServletRequest request, HttpServletResponse response) {
    String host = request.getHeader("Host");
    
    if (host == null) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        return "error/404";
    }
    
    return HOST_TO_VIEW.entrySet().stream()
        .filter(entry -> host.contains(entry.getKey()))
        .map(Map.Entry::getValue)
        .findFirst()
        .orElse("error/404");
}

이 코드는 요청의 Host 헤더를 검사하여 적절한 뷰로 라우팅합니다:

  • gtc.example.com → /gtc/index.html로 포워드
  • customer.example.com → /customer/index.html로 포워드
  • admin.example.com → admin/dashboard 뷰 반환
  • localhost → /customer/index.html로 포워드 (개발 환경 기본값)

Vue.js 통합

Vue.js 프로젝트의 빌드 파일을 스프링 애플리케이션에 통합할 때 주의할 점:

static/customer/assets/runtime-dom.esm-bundler.js (Vue 프로젝트 dist/ 디렉토리에 있음)
static/gtc/assets/runtime-dom.esm-bundler.js (Vue 프로젝트 dist/ 디렉토리에 있음)

 

 

반응형