10 Temmuz 2018 Salı

SpringBoot TomcatServletWebServerFactory Sınıfı - Tomcat Sunucusunun Çalışma Ayarlarını Değiştiririr

Giriş
Şu satırı dahil ederiz
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
Bu sınıf ConfigurableServletWebServerFactory  arayüzünü gerçekleştirir. Tomcat sunucusunun çalışma ayarlarını değiştirmek için kullanılır.

Bu sınıfın deprecate edilen kardeşi TomcatEmbeddedServletContainerFactory sınıfı

Bu sınıfı kodlama yerine application.properties ile bazı ayarlar değiştirilebilir.

constructor
Örnek
Şöyle yaparız.
@Bean 
ServletWebServerFactory servletWebServerFactory(){
  return new TomcatServletWebServerFactory();
}
Örnek
Şöyle yaparız.
import org.apache.catalina.Context;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.descriptor.web.ContextResource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class MySpringBootApplication {

  public static void main(String[] args) {
    SpringApplication.run(MySpringBootApplication.class, args);
  }

  @Bean
  public ServletWebServerFactory tomcatFactory() {
    return new TomcatServletWebServerFactory() {...};
  }
  ...
}
addAdditionalTomcatConnectors metodu
Tomcat tarafından dinlenecek (liste) portu belirtir.
Örnek - http redirect
Şöyle yaparız.
tomcat.addAdditionalTomcatConnectors(redirectConnector());

private Connector redirectConnector() {
  Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
  connector.setScheme("http");
  connector.setPort(8080);
  connector.setSecure(false);
  connector.setRedirectPort(8443);
  return connector;
}
Örnek - http redirect
Şöyle yaparız
@Bean
public ServletWebServerFactory servletContainer() {
  TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
    @Override
    protected void postProcessContext(Context context) {
      SecurityConstraint securityConstraint = new SecurityConstraint();
      securityConstraint.setUserConstraint("CONFIDENTIAL");
      SecurityCollection collection = new SecurityCollection();
      collection.addPattern("/*");
      securityConstraint.addCollection(collection);
      context.addConstraint(securityConstraint);
    }
  };
  tomcat.addAdditionalTomcatConnectors(redirectConnector());
  return tomcat;
}

private Connector redirectConnector() {
  Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
  connector.setScheme("http");
  connector.setPort(80);
  connector.setSecure(false);
  connector.setRedirectPort(443);
  return connector;
}
Örnek - https
application.properties şöyle olsun. Böylece 8080 ile http 8079 ile https hizmeti verilir
server:
  port: 8080
  ssl:
    enabled: true
    keyStoreType: PKCS12
    key-store: /path/to/keystore.p12
    key-store-password: password
  http:
    port: 8079

Şöyle yaparız
@Configuration
public class TomcatConfig implements 
  WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

  @Value("${server.http.port}")
  private int httpPort;

  @Override
  public void customize(TomcatServletWebServerFactory factory) {
    Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
    connector.setPort(httpPort);
    factory.addAdditionalTomcatConnectors(connector);
  }
}
Örnek - https
Şöyle yaparız
@Bean
public ServletWebServerFactory servletContainer() {
  TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
    @Override
    protected void postProcessContext(Context context) {
      SecurityConstraint securityConstraint = new SecurityConstraint();
      securityConstraint.setUserConstraint("CONFIDENTIAL");
      SecurityCollection collection = new SecurityCollection();
      collection.addPattern("/*");
      securityConstraint.addCollection(collection);
      context.addConstraint(securityConstraint);
    }
  };
  tomcat.addAdditionalTomcatConnectors(extraHttpsConnector());
  return tomcat;
}

private Connector extraHttpsConnector() {
  Connector connector = new Connector();
  connector.setScheme("https");
  connector.setPort(443);
  connector.setSecure(true);
  connector.setProperty("SSLEnabled", "true");
  //Add SSL configuration to your extra connector
  SSLHostConfig sslConfig = new SSLHostConfig();
  SSLHostConfigCertificate certConfig = new SSLHostConfigCertificate(sslConfig, Type.RSA);
  certConfig.setCertificateKeystoreFile("YOUR_KEYSTORE");
  certConfig.setCertificateKeystorePassword("YOUR_KEYSTORE_PASSWORD");
  certConfig.setCertificateKeyAlias("YOUR_KEYSTORE_ALIAS");
  sslConfig.addCertificate(certConfig);
  //Link the configuration to the connector
  connector.addSslHostConfig(sslConfig);
  return connector;
}
addConnectorCustomizers metodu
Şöyle yaparız.

getTomcatWebServer metodu
Şöyle yaparız.
@Override
protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
  tomcat.enableNaming();
  return new TomcatWebServer(tomcat, getPort() >= 0);
}
postProcessContext metodu
Örnek
Şöyle yaparız. SecurityConstraint tomcat sınıfı.
@Override
protected void postProcessContext(Context context) {
  SecurityConstraint securityConstraint = new SecurityConstraint();
  securityConstraint.setUserConstraint("CONFIDENTIAL");
  SecurityCollection collection = new SecurityCollection();
  collection.addPattern("/*");
  securityConstraint.addCollection(collection);
  context.addConstraint(securityConstraint);
}
Örnek
Şöyle yaparız. ContextResource  tomcat sınıfı
@Override
protected void postProcessContext(Context context) {
  ContextResource resource = new ContextResource();
  resource.setName("java:comp/env/jdbc/OracleDSNonXA");
  resource.setType(DataSource.class.getName());
  resource.setProperty("driverClassName", "oracle.jdbc.driver.OracleDriver");
  resource.setProperty("url", "DBURL");
  resource.setProperty("username", "DBUSER");
  resource.setProperty("password", "PASSWORD");
  context.getNamingResources().addResource(resource);
}

Hiç yorum yok:

Yorum Gönder