search
HomeJavajavaTutorialHow to implement SpringBoot's HTTPS configuration

How to implement SpringBoot's HTTPS configuration

May 27, 2023 pm 04:29 PM
httpsspringboot

HTTPS configuration

Because HTTPS has good security, it has been increasingly widely used in development. The development of WeChat public accounts, small programs, etc. must be completed using HTTPS. For individual developers, the price of an HTTPS certificate is still a bit expensive. Some domestic cloud server manufacturers provide free HTTPS certificates, and one account can apply for several. However, jdk provides a Java digital certificate management tool keytool. In the \jdkbin directory, you can use this tool to generate a digital certificate yourself. The generation command is as follows:

keytool -genkey -alias tomcathttps -keyalg RSA -keysize 2048 -keystore sang.p12 -validity 365
  • genkey means you want Create a new key.

  • alias represents the alias of the keystore.

  • keyalg indicates that the encryption algorithm used is RSA, an asymmetric encryption algorithm.

  • keysize indicates the length of the key.

  • keystore indicates the storage location of the generated key.

  • validity indicates the validity time of the key, in days.

According to the prompts, you need to provide information such as password or password when executing the command in the cmd window. Enter it to complete the execution process. After the command is executed, a file named sang.p12 will be generated in the current user directory. Copy this file to the root directory of the project, and then make the following configuration in application.properties:

# key-store表示密钥文件名。
server.ssl.key-store=sang.p12 
# key-alias表示密钥别名。
server.ssl.key-alias=tomcathttps
#key-store-password 就是在cmd命令执行过程中输入的密码。
server.ssl.key-store-password=123456

Configuration After success, start the project and enter "https:/localhost:8080" in the browser to view the results. Note that the generated certificate is not authenticated by the browser, so you will need to add trust or move forward. Please refer to the picture below.

How to implement SpringBoots HTTPS configuration

The result of successful operation is as shown in the figure

How to implement SpringBoots HTTPS configuration

At this time, if you access the interface through HTTP, you will access Failed, as shown in the figure

How to implement SpringBoots HTTPS configuration

# Original words rewritten: Spring Boot does not support enabling HTTP and HTTPS at the same time in the configuration. At this time, you can configure request redirection to redirect HTTP requests to HTTPS requests. The configuration method is as follows

@Configuration
public class TomcatConfig {
    @Bean
    TomcatServletWebServerFactory tomcatServletWebServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint constraint = new SecurityConstraint();
                constraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                constraint.addCollection(collection);
                context.addConstraint(constraint);
            }
        };
        factory.addAdditionalTomcatConnectors(createTomcatConnector());
        return factory;
    }

    private Connector createTomcatConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(8080);
        connector.setSecure(false);
        connector.setRedirectPort(8081);
        return connector;
    }
}

Here first configure a TomcatServletWebServerFactory, then add a Connector in Tomcat (listen to port 8080), and forward the request to 8081.
After the configuration is completed, enter "http://localhost:8080/" in the browser and it will automatically redirect to https://localhost:8081/.

The above is the detailed content of How to implement SpringBoot's HTTPS configuration. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor