Springboot Turn on HTTPS (ix)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.