บทความนี้แนะนำการกำหนดค่า TOMCAT ของคอนเทนเนอร์แบบฝังใน Spring Boot และแชร์กับคุณดังนี้:
คอนเทนเนอร์เริ่มต้น
โปรแกรมเว็บเริ่มต้นของ Spring Boot ช่วยให้ Tomcat Embedded Container Tomcat ฟังพอร์ต 8080 และ ServletPath เริ่มต้นคือ / ผ่านการปรับเปลี่ยนพอร์ตและเส้นทางบริบท ในสปริงบูตวิธีการดัดแปลงนั้นง่ายมาก
กำหนดค่าในไฟล์ทรัพยากร:
Server.port = 9090 Server.ContextPath =/lkl
เริ่มต้นสปริงบูต
2015-10-04 00: 06: 55.768 ข้อมูล 609 --- [Main] Oswshandler.simpleurlhandlermapping: เส้นทาง URL ที่แมป [/**/favicon.ico] ไปยังตัวจัดการประเภท 00: 06: 55.844 ข้อมูล 609 --- [หลัก] OsJeaAnnotationMbeanExporter: การลงทะเบียนถั่วสำหรับการเปิดรับ JMX เมื่อเริ่มต้น 2015-10-04 00: 06: 55.928 ข้อมูล 609 --- [Main] SBCettomcatembeddedservletContainer 00: 06: 55.930 Info 609 --- [Main] com.lkl.springboot.application: เริ่มต้นแอปพลิเคชันใน 3.906 วินาที (JVM ทำงานสำหรับ 4.184)
จะเห็นได้ว่าพอร์ตการฟัง 9090 สามารถเข้าถึงได้สำเร็จโดยการดำเนินการ http: // localhost: 9090/lkl/springboot/liaokailin
Tomcat ที่กำหนดเอง
ในโครงการจริงการกำหนดค่าอย่างง่ายของพอร์ต Tomcat จะไม่ตรงกับความต้องการของทุกคนดังนั้นคุณต้องปรับแต่งข้อมูลการกำหนดค่า Tomcat เพื่อควบคุม Tomcat อย่างยืดหยุ่น
ใช้ตัวอย่างของการกำหนดการเข้ารหัสเริ่มต้น
แพ็คเกจ com.lkl.springboot.container.tomcat; นำเข้า org.springframework.boot.context.embedded.embeddedservletContainerFactory; นำเข้า org.springframework.boot.context org.springframework.context.annotation.bean; นำเข้า org.springframework.context.annotation.configuration;/** * การกำหนดค่า tomcat * @author liaokailin * @version $ id: tomcatconfig.java, v 01 ตุลาคม 4, 2015 tomcatconfig {@bean public EmbeddedServletContainerFactory servletContainer () {tomcatembeddedservletContainerFactory tomcat = ใหม่ tomcatembeddedservletContainerFactory (); tomcat.seturiencoding ("UTF-8"); กลับ Tomcat; -สร้างถั่วของ EmbeddedServletContainerFactory และหลังจากได้รับอินสแตนซ์ tomcatembeddedservletContainerFactory คุณสามารถตั้งค่า Tomcat ตัวอย่างเช่นการเข้ารหัสถูกตั้งค่าเป็น UTF-8 ที่นี่
การกำหนดค่า SSL
สร้างใบรับรอง
Keytool -Genkey -Alias Springboot -Keyalg RSA -KeyStore/ผู้ใช้/liaokailin/ซอฟต์แวร์/CA1/KeyStore Set Password 123456
ตรวจสอบว่าใบรับรองถูกต้องใน Tomcat
แก้ไขไฟล์ tomcat/conf/server.xml
<connector protocol = "org.apache.coyote.http11.http11nioprotocol" พอร์ต = "8443" maxThreads = "200" scheme = "https" secure = "true" sslenabled = "true" keystorefile = "/ผู้ใช้ 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 =/ผู้ใช้/liaokailin/ซอฟต์แวร์/ca1/keystore
เริ่มต้นสปริงบูต
เยี่ยมชม https: // localhost: 8443/springboot/helloWorld
การกำหนดค่าการตรวจสอบแบบหลายพอร์ต
หลังจากเริ่ม SSL คุณสามารถไปที่ HTTPS เท่านั้นและไม่สามารถเข้าถึงได้ผ่าน HTTP หากคุณต้องการฟังหลายพอร์ตคุณสามารถใช้การเข้ารหัสเพื่อใช้งานได้
1. ออกจากการกำหนดค่า SSL ก่อนหน้าและตั้งค่าเซิร์ฟเวอร์ configuration พอร์ต = 9090
2. แก้ไข tomcatconfig.java
แพ็คเกจ com.lkl.springboot.container.tomcat; นำเข้า java.io.file; นำเข้า org.apache.catalina.connector.connector; นำเข้า org.apache.coyote.http11.http11 org.springframework.boot.context.embedded.tomcat.tomcatembeddedservletContainerFactory; นำเข้า org.springframework.context.annotation.bean; นำเข้า org.springframework.context.annotation.configuration; tomcatconfig.java, v 0.1 4 ตุลาคม 2015 liaokailin exp $ */ @configurationpublic คลาส tomcatconfig {@bean public EmbeddedServletContainerFactory ServletContainer () tomcat.seturiencoding ("UTF-8"); tomcat.addadditionaltomcatconnectors (createSlConnector ()); กลับ Tomcat; } ตัวเชื่อมต่อส่วนตัว CreateSslConnector () {ตัวเชื่อมต่อตัวเชื่อมต่อ = ตัวเชื่อมต่อใหม่ ("org.apache.coyote.http11.http11nioprotocol"); HTTP11NIOPROTOCOL PROTOCOL = (HTTP11NIOPROTOCOL) connector.getProtocolHandler (); ลอง {ไฟล์ TrustStore = ไฟล์ใหม่ ("/ผู้ใช้/liaokailin/ซอฟต์แวร์/ca1/keystore"); connector.setscheme ("https"); Protocol.setsslenabled (จริง); connector.setSecure (จริง); connector.setport (8443); Protocol.setKeyStoreFile (TrustStore.getAbsolutePath ()); Protocol.setKeyStorePass ("123456"); Protocol.setKeyalias ("SpringBoot"); ตัวเชื่อมต่อกลับ; } catch (exception ex) {โยนใหม่ ungelStateException ("ไม่สามารถเข้าถึงคีย์สโตร์: [" + "คีย์สโตร์" + "]", อดีต); - เพิ่มการเชื่อมต่อการฟังหลายครั้งผ่านวิธี AddadditionalTomCatConnectors; ในเวลานี้คุณสามารถใช้พอร์ต HTTP 9090 และพอร์ต HTTPS 8443
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น