Nach Springboot 2.0 unterscheidet sich der Weg, um das HTTPS -Protokoll zu aktivieren, ein bisschen von dann, wenn 1.*den Code veröffentlichen.
Mein Code kann bestimmen, ob das HTTPS -Protokoll basierend auf der Bedingung in den Konfigurationsparametern aktiviert werden soll. Wenn das HTTPS -Protokoll aktiviert ist, werden alle Zugriffe auf das HTTPS -Protokoll automatisch auf das HTTPS -Protokoll übertragen.
1. Starten Sie das Programm
Paket com.wallimn.iteye.sp.asset; import org.apache.catalina.context; import org.apache.catalina.connector.connector; import org.apache.tomcat.util.descriptor.web.securityCollection; import org.apache.tomcat.util.descriptor.web.securityconstraint; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.springApplication; import org.springframework.boot.autoconfigure.springbootApplication; import org.springframework.boot.autoconfigure.condition.ConditionalonProperty; import org.springframework.boot.web.embeded.tomcat.tomcatservletweBserverfactory; import org.springframework.context.annotation.bean; /** * SpringBoot2.0 Starter * @author Wallimn, http://wallimn.iteye.com * */@springbootApplication öffentliche Klasse AssetApplication {public static void main (String [] args) {SpringApplication.run (assetApplication.class, args, args, args, args, args, args, args, args, args, args, args); } // Wenn der Standardwert nicht verwendet wird 80 @Value ("$ {http.port: 80}") Integer httpport; // Der normalerweise aktivierte HTTPS -Port ist wie 443 @Value ("$ {Server.port}") Integer httpsport; // springboot2 writing method @Bean @ConditionalOnProperty(name="condition.http2https", havingValue="true", matchIfMissing=false) public TomcatServletWebServerFactory servletContainer() { TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() { @Override protected void postProcesscontext (Kontextkontext) {SecurityConstraint Constraint = new SecurityConstraint (); Einschränkung.SetUserConstraint ("vertraulich"); SecurityCollection Collection = new SecurityCollection (); Collection.addPattern ("/*"); Connect.AddCollection (Sammlung); context.addconstraint (Einschränkung); }}; tomcat.addadditionaltomcatConnectors (httpconnector ()); Tomcat zurückgeben; } @Bean @ConditionalonProperty (name = "condition.http2https", hat Value = "true", matchiFming = false) public Connector httpconnector () {System.out.println ("Enable HttpTP -Protocol, Htttp -Port:"+This.httpPr. "+this.httpsport); Connector Connector = New Connector ("org.apache.coyote.http11.http11nioprotocol"); Connector.SetScheme ("http"); // Die Portnummer von HTTP, die von Connector.setPort (httpport) angehört werden; Connector.SetSecure (Falsch); // Die Portnummer der HTTPs, an die Sie sich nach dem Anhören der Portnummer von HTTPS CONNECTECT.SetRectPort (httpsport) zuwenden; Rückgabeanschluss; }}2. Konfigurationsdatei
1. Konfiguration bei Verwendung des HTTP -Protokolls
Server.port = 80
2. Konfiguration bei Verwendung von HTTPS- und HTTP -Protokollen
server.port = 443 server.ssl.key-store = classPath: keystore.p12 server.ssl.key-store-password = your-password server.ssl.keyStoretype = pkcs12 server.ssl.keyalias = your-cert-alias condition.htp2htps = true http.port = 80 80 80 80 80 80 80 80 80 80.
Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.