Springboot only HTTP requests are turned on by default, and if you want to turn on HTTPS, you need to generate certificates and configuration
1. First open the CMD command-line tool and generate the certificate in the Jdk/bin directory. KeyStore
123456 1024x768 365 123456
Related parameters:
--123456-1024x768365-keystore f:/ 123456(password to get keystore information)
Springboot Configuring SSL (the certificate is a. keystore file)
1. Copy the Tomcat.keystore to the project root directory (with the Pom file sibling directory)
2. Configure Application.properties
#端口号server. Port=8443#你生成的证书名字server. Ssl.key-store=tomcat.keystore# KeyStore password Server.ssl.key-store-password=123456server.ssl.keyStoreType= JKSserver.ssl.keyAlias:tomcat
3. Modify Springboot Run Main class: Implement HTTP auto-jump HTTPS
Package Com.mingtong.demo_client;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.boot.SpringApplication; Import Org.springframework.boot.autoconfigure.springbootapplication;import Org.springframework.boot.context.embedded.embeddedservletcontainerfactory;import Org.springframework.boot.context.embedded.tomcat.tomcatembeddedservletcontainerfactory;import Org.springframework.context.annotation.bean;import Org.springframework.web.bind.annotation.getmapping;import Org.springframework.web.bind.annotation.RestController, @SpringBootApplication @restcontroller Public classdemoclientapplication { Public Static voidMain (string[] args) {Springapplication.run (democlientapplication.class, args); } @GetMapping ("/login") PublicString Login () {return "Login"; } /** * it ' s for set HTTP URL auto change to HTTPS*/@Bean Publicembeddedservletcontainerfactory Servletcontainer () {tomcatembeddedservletcontainerfactory tomcat=Newtomcatembeddedservletcontainerfactory () {@Overrideprotected voidPostprocesscontext (Context context) {Securityconstraint Securityconstraint=NewSecurityconstraint (); Securityconstraint.setuserconstraint ("Confidential");//ConfidentialSecuritycollection collection =Newsecuritycollection (); Collection.addpattern ("/*"); Securityconstraint.addcollection (collection); Context.addconstraint (Securityconstraint); } }; Tomcat.addadditionaltomcatconnectors (HttpConnector ()); returnTomcat; } @Bean PublicConnector HttpConnector () {Connector Connector=NewConnector ("Org.apache.coyote.http11.Http11NioProtocol"); Connector.setscheme ("http"); Connector.setport (8080); Connector.setsecure (false); Connector.setredirectport (8443); returnconnector; }}
Test: Access Http://localhost:8080/login, will automatically jump to Https://localhost:8443/login
Springboot Turn on HTTPS (ix)