亲宝软件园·资讯

展开

springboot内嵌tomcat spring boot实战之内嵌容器tomcat配置

liaokailin 人气:0
想了解spring boot实战之内嵌容器tomcat配置的相关内容吗,liaokailin在本文为您仔细讲解springboot内嵌tomcat的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:springboot内嵌tomcat,spring,boot,内嵌容器,下面大家一起来学习吧。

本文介绍了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 startup
2015-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 $
 */
@Configuration
public 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=8443
server.ssl.enabled=true
server.ssl.keyAlias=springboot
server.ssl.keyPassword=123456
server.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 $
 */
@Configuration
public 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端口。

加载全部内容

相关教程
猜你喜欢
用户评论