Home  >  Article  >  Java  >  How to configure SSL in SpringBoot to support http and https access at the same time

How to configure SSL in SpringBoot to support http and https access at the same time

王林
王林forward
2023-05-18 14:25:061059browse

Transport Layer Security (English: Transport Layer Security, abbreviated as TLS), and its predecessor Secure Sockets Layer (Abbreviated as SSL) are a Security protocols are designed to provide security and data integrity protection for Internet communications.

SSL includes the record layer (Record Layer) and the transport layer. The record layer protocol determines the encapsulation format of the transport layer data.

The transport layer security protocol uses X.509 authentication, and then uses asymmetric encryption algorithms to authenticate the communicating parties, and then exchanges symmetric keys as session keys.

This session key is used to encrypt the data exchanged by the two communicating parties to ensure the confidentiality and reliability of the communication between the two applications, so that the communication between the client and server applications will not be eavesdropped by attackers. .

Step one: Generate certificate

Before configuring TLS/SSL, you need to obtain the corresponding signing certificate and configure the JAVA development environment. You can use Java Use the following Keytool to generate the certificate, open the console and enter:

keytool -genkey -alias michaelSpica -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore E: \Temp\michaelSpica.p12 -validity 3650

  • -alias Alias ​​(can be chosen as desired)

  • -storetype Specify the key store type

  • -keyalg The name of the algorithm for generating certificates. RSA is an asymmetric encryption algorithm

  • -keysize Certificate size

  • -keystore Storage path of the generated certificate file (relative path or Absolute path)

  • -validity The validity period of the certificate

As shown in the figure:

How to configure SSL in SpringBoot to support http and https access at the same time

Note: Please fill in the standard value in the formal environment

Step 2: Obtain the certificate

Find the generated certificate according to the path and copy the certificate Go to the project, as shown in the figure:

How to configure SSL in SpringBoot to support http and https access at the same time

Step 3: Add SSL configuration

Add the following configuration in application.yml, As shown in the picture:

How to configure SSL in SpringBoot to support http and https access at the same time

Step 4: Configure https access

When adding Spring Boot, read the configuration information, such as

(Note: Please add the necessary jars)

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
package com.michael.protocol.config;
import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * Created by michael on 2019/2/23.
 */
@Configuration
public class TomcatConfig {
    @Value("${server.http.port}")
    private int httpPort;
    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addAdditionalTomcatConnectors(createStandardConnector()); // 添加http
        return tomcat;
    }
    private Connector createStandardConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setPort(httpPort);
        return connector;
    }
}

Note: This is the spring boot 2.0.X version

At this point, all the work has been completed , start the project, such as:

How to configure SSL in SpringBoot to support http and https access at the same time

You can see two ports, indicating that it has been successful, and you can access it in the following two ways:

https://localhost:443

http://localhost:80

The above is the detailed content of How to configure SSL in SpringBoot to support http and https access at the same time. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete