spring boot实战之内嵌容器tomcat配置
本文介绍了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
- server.ssl.enabled 启动tomcat ssl配置
- server.ssl.keyalias 别名
- server.ssl.keypassword 密码
- 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端口。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。