Little rookie Learning Spring BOOT--SSL configuration

Source: Internet
Author: User

  Rookie new to the big God do not spray, a little Daigo, grateful. Because I always pretend to be humorous, because I want to make myself happy.

  The SSL protocol is located between the TCP/IP protocol and various application protocols, providing security support for data communication.

The SSL protocol is divided into two tiers:

    • SSL logging protocol, which is based on reliable transmission protocol, provides the basic functions of data encapsulation, compression and encryption for high-level protocols.
    • The SSL handshake protocol, which is based on the SSL record Protocol, is used for authentication, negotiation encryption algorithm, Exchange encryption key and so on before the actual data transmission begins.

Based on the B/S Web application, SSL is implemented via HTTPS. HTTPS is the security version of HTTP, that is, the SSL layer is added under HTTP, the security base of HTTPS is SSL;

We started using SSL settings in spring boot;

    1. Generate certificate  

Each JDK or JRE has a tool called Keytool, which is a certificate management tool that can be used to generate self-signed certificates; open cmd, go to jdk/bin path, enter command

Keytool-genkey-alias Tomcat

  

Generate the. keystore file under the user path, which is the certificate file that we want to use.

  

2.spring Boot Configuration SSL

Copy the. keystore file to the project root and configure the SSL configuration in Application.properties

server.ssl.key-store=.keystoreserver.ssl.key-store-password= Password Server.ssl.keyStoreType = jksserver.ssl.keyalias= Tomcat

Start Project

  

Access Address https://localhost:8080

  

  

3, HTTP to HTTPS

To achieve this, we need to configure tomcatembeddedservletcontainerfactory and add Tomcat's connector to implement it.

  

Package Com.example;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.configurableembeddedservletcontainer;import Org.springframework.boot.context.embedded.embeddedservletcontainercustomizer;import Org.springframework.boot.context.embedded.embeddedservletcontainerfactory;import Org.springframework.boot.context.embedded.tomcat.tomcatembeddedservletcontainerfactory;import Org.springframework.boot.web.servlet.errorpage;import Org.springframework.context.annotation.bean;import Org.springframework.http.httpstatus;import Org.springframework.stereotype.component;import Org.springframework.stereotype.controller;import Org.springframework.ui.model;import Org.springframework. Web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.restcontroller;import Org.springframework.web.servlet.config.annotation.enablewebmvc;import Java.util.arraylist;import java.util.List; Import java.util.concurrent.timeunit;/** * Created by Xingzhuipingye on 2017/5/7. */@Controller @springbootapplicationpublic class Applicationmy {@RequestMapping ("/") public String Index (model        {Person * = new person ("AA", 11);        list<person> list = new arraylist<> ();        person P1 = new Person ("XX", 11);        person P2 = new Person ("yy", 22);        Person P3 = new person ("ZZ", 33);        List.add (p1);        List.add (p2);        List.add (p3);        Model.addattribute ("Singleperson", single);        Model.addattribute ("People", list);    Return "index";    } public static void Main (string[] args) {springapplication.run (applicationmy.class); } @Bean Public Embeddedservletcontainerfactory Servletcontainer () {       Tomcatembeddedservletcontainerfactory tomcat = new Tomcatembeddedservletcontainerfactory () {@Override protected void Postprocesscontext (context context) {Securityconstraint securityconstraint = new S                Ecurityconstraint ();                Securityconstraint.setuserconstraint ("Confidential");                Securitycollection collection = new Securitycollection ();                Collection.addpattern ("/*");                Securityconstraint.addcollection (collection);            Context.addconstraint (Securityconstraint);        }        };        Tomcat.addadditionaltomcatconnectors (HttpConnector ());    return tomcat; } @Bean Public Connector HttpConnector () {Connector Connector = new Connector ("ORG.APACHE.COYOTE.HTTP11.HTTP1        1NioProtocol ");        Connector.setscheme ("http");        Connector.setport (8080);        Connector.setsecure (FALSE);        Connector.setredirectport (8088);    return connector; }}

Note: I modified the port to 8088 in Application.properties

When we visit http://localhost:8080, we will jump to https://localhost:8088.

  

Little rookie Learning Spring BOOT--SSL configuration

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.