Spring Boot 웹 응용 프로그램은 내장 웹 서버를 포함한다. 여기서 임베디드 서버 구성 및 변경하는 방법에 대해서 설명한다.
내장 웹 서버 이해 Spring Boot에 spring-boot-autoconfigure
자동 설정 확인
spring.factories
에 org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\
자동 설정이 되어 있는걸 확인 할 수 있다.
spring.factories 1 2 3 ... org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\ ...
ServletWebServerFactoryAutoConfiguration.java
에서 EmbeddedTomcat
, EmbeddedJetty
, EmbeddedUndertow
의 자동 설정부분이 Import 되어 있어서 내장 웹 서버가 자동으로 구동 된다.
ServletWebServerFactoryAutoConfiguration.java 1 2 3 4 5 6 7 8 9 10 11 12 @Configuration @AutoConfigureOrder (Ordered.HIGHEST_PRECEDENCE)@ConditionalOnClass (ServletRequest.class ) @ConditionalOnWebApplication (type = Type.SERVLET)@EnableConfigurationProperties (ServerProperties.class ) @Import ( { ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class , ServletWebServerFactoryConfiguration .EmbeddedTomcat .class , ServletWebServerFactoryConfiguration .EmbeddedJetty .class , ServletWebServerFactoryConfiguration .EmbeddedUndertow .class }) public class ServletWebServerFactoryAutoConfiguration { ... }
다른 서블릿 컨테이너 사용하기 Spring Boot는 기본 서블릿 컨테이너가 Tomcat
이다. 이를 변경하기 위해서는 Tomcat
의 종속성을 제거하고, 원하는 서블릿 컨테이너(Netty
, Undertow
) 추가해주면 된다.
Maven Dependency pom.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <dependencies > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > <exclusions > <exclusion > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-tomcat</artifactId > </exclusion > </exclusions > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-undertow</artifactId > </dependency > </dependencies >
Undertow
의존성 확인
Unsertow
동작 확인
웹 서버 비활성화 Spring Boot 서블릿 컨테이너가 의존성 주입이 되어 있는데 동작을 하지 않게 하는 방법이다.
application.yaml 1 2 3 spring: main: web-application-type: none
포트 변경 application.yaml
랜덤 포트 변경 사용하지 않는 포트를 찾아 포트 변경
application.yaml
웹 서버 기동시 port 포트 확인 웹 서버가 생성이 되면 ApplicationListener<ServletWebServerInitializedEvent>
가 호출이 된다. getApplicationContext
인스턴스를 생성 후 포트 정보를 확인하면 된다.
ApplicationListener.java 1 2 3 4 5 6 7 8 9 @Component public class PortListener implements ApplicationListener <ServletWebServerInitializedEvent > { @Override public void onApplicationEvent (ServletWebServerInitializedEvent servletWebServerInitializedEvent) { ServletWebServerApplicationContext applicationContext = servletWebServerInitializedEvent.getApplicationContext(); System.out.println(applicationContext.getWebServer().getPort()); } }
동작 확인
ssl 적용 ssl 적용하기 위해서는 keystore를 생성해야 된다.
keystore 생성 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 $ keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 4000 Enter keystore password: Re-enter new password: What is your first and last name? [Unknown]: jaehyun What is the name of your organizational unit? [Unknown]: it What is the name of your organization? [Unknown]: it What is the name of your City or Locality? [Unknown]: seoul What is the name of your State or Province? [Unknown]: sangbong What is the two-letter country code for this unit? [Unknown]: 100 Is CN=jaehyun, OU=it, O=it, L=seoul, ST=sangbong, C=100 correct? [no]: y
keystore 생성 확인
application.yaml을 이용해서 SSL 등록 정보를 설정 application.yaml application.yaml 1 2 3 4 5 6 7 8 server: port: 8443 ssl: key-store: web-server/keystore.p12 key-store-type: PKCS12 key-store-password: 123456 key-alias: spring
ssl 확인
소스코드
참조