spring boot實戰之內嵌容器tomcat配置,springtomcat

來源:互聯網
上載者:User

spring boot實戰之內嵌容器tomcat配置,springtomcat

本文介紹了spring boot實戰之內嵌容器tomcat配置,分享給大家,具體如下:

預設容器

spring boot預設web程式啟用tomcat內嵌容器tomcat,監聽8080連接埠,servletPath預設為 / 通過需要用到的就是連接埠、上下文路徑的修改,在spring boot中其修改方法及其簡單;

在資源檔中配置:  

server.port=9090 server.contextPath=/lkl

啟動spring boot

2015-10-04 00:06:55.768 INFO 609 --- [      main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2015-10-04 00:06:55.844 INFO 609 --- [      main] o.s.j.e.a.AnnotationMBeanExporter    : Registering beans for JMX exposure on startup2015-10-04 00:06:55.928 INFO 609 --- [      main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 9090 (http)2015-10-04 00:06:55.930 INFO 609 --- [      main] com.lkl.springboot.Application    : Started Application in 3.906 seconds (JVM running for 4.184)

可以看出其監聽連接埠9090,執行 http://localhost:9090/lkl/springboot/liaokailin 成功訪問

自訂tomcat

在實際的項目中簡單的配置tomcat連接埠肯定無法滿足大家的需求,因此需要自訂tomcat配置資訊來靈活的控制tomcat。

以定義預設編碼為例

package com.lkl.springboot.container.tomcat;import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * tomcat 配置 * @author liaokailin * @version $Id: TomcatConfig.java, v 0.1 2015年10月4日 上午12:11:47 liaokailin Exp $ */@Configurationpublic class TomcatConfig {  @Bean  public EmbeddedServletContainerFactory servletContainer() {    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();    tomcat.setUriEncoding("UTF-8");    return tomcat;  }}

構建EmbeddedServletContainerFactory的bean,擷取到TomcatEmbeddedServletContainerFactory執行個體以後可以對tomcat進行設定,例如這裡設定編碼為UTF-8

SSL配置

產生認證

keytool -genkey -alias springboot -keyalg RSA -keystore /Users/liaokailin/software/ca1/keystore設定密碼123456

tomcat中驗證認證是否正確

修改tomcat/conf/server.xml檔案

<Connector       protocol="org.apache.coyote.http11.Http11NioProtocol"       port="8443" maxThreads="200"       scheme="https" secure="true" SSLEnabled="true"       keystoreFile="/Users/liaokailin/software/ca1/keystore" keystorePass="123456"       clientAuth="false" sslProtocol="TLS"/>

啟動tomcat ,訪問 http://localhost:8443

spring boot 內嵌tomcat ssl

配置資源檔

server.port=8443server.ssl.enabled=trueserver.ssl.keyAlias=springbootserver.ssl.keyPassword=123456server.ssl.keyStore=/Users/liaokailin/software/ca1/keystore
  1. server.ssl.enabled 啟動tomcat ssl配置
  2. server.ssl.keyAlias 別名
  3. server.ssl.keyPassword 密碼
  4. server.ssl.keyStore 位置

啟動 spring boot

訪問https://localhost:8443/springboot/helloworld

多連接埠監聽配置

前面啟動ssl後只能走https,不能通過http進行訪問,如果要監聽多連接埠,可採用編碼形式實現。

1.登出前面ssl配置,設定配置 server.port=9090

2.修改TomcatConfig.java

package com.lkl.springboot.container.tomcat;import java.io.File;import org.apache.catalina.connector.Connector;import org.apache.coyote.http11.Http11NioProtocol;import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * tomcat 配置 * @author liaokailin * @version $Id: TomcatConfig.java, v 0.1 2015年10月4日 上午12:11:47 liaokailin Exp $ */@Configurationpublic class TomcatConfig {  @Bean  public EmbeddedServletContainerFactory servletContainer() {    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();    tomcat.setUriEncoding("UTF-8");    tomcat.addAdditionalTomcatConnectors(createSslConnector());    return tomcat;  }  private Connector createSslConnector() {    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");    Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();    try {      File truststore = new File("/Users/liaokailin/software/ca1/keystore");      connector.setScheme("https");      protocol.setSSLEnabled(true);      connector.setSecure(true);      connector.setPort(8443);      protocol.setKeystoreFile(truststore.getAbsolutePath());      protocol.setKeystorePass("123456");      protocol.setKeyAlias("springboot");      return connector;    } catch (Exception ex) {      throw new IllegalStateException("cant access keystore: [" + "keystore" + "] ", ex);    }  }}

通過addAdditionalTomcatConnectors方法添加多個監聽串連;此時可以通過http 9090連接埠,https 8443連接埠。

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.