Home > Java > javaTutorial > body text

How to configure springboot to jump from http to https

WBOY
Release: 2023-05-27 15:11:42
forward
1519 people have browsed it

SSL is a security protocol used to ensure the security and data integrity of network communications. It encrypts network connections at the network transport layer.

Example: cas's single sign-in uses SSL

1. Generation of security certificate

1. You can use the certificate generation tool that comes with jdk, which comes with jdk A certificate management tool called keytool can be used to implement signed certificates.

2. First configure the basic java environment, ctrl r input cmd, enter the java directory

3. Example: Generate a certificate with the alias tomcat. First use the command to enter the jdk bin here. The password is 123456

keytool -genkey -alias tomcat -keypass 123456 -keyalg RSA -keysize 1024 -validity 365 -keystore D:/keys/tomcat.keystore -storepass 123456

How to configure springboot to jump from http to https

4. Get a tomcat.keystore file and put this file in the project directory

How to configure springboot to jump from http to https

2. Configure SSL

1. Edit the file application.properties

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.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringBootHttpsApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootHttpsApplication.class, args);
    }
    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
            @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);
            }
        };
        tomcat.addAdditionalTomcatConnectors(httpConnector());
        return tomcat;
    }

    @Bean
    public Connector httpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        //Connector监听的http的端口号
        connector.setPort(8080);
        connector.setSecure(false);
        //监听到http的端口号后转向到的https的端口号
        connector.setRedirectPort(8443);
        return connector;
    }

}
Copy after login

3. Test usage

1. Check the startup information

How to configure springboot to jump from http to https

2. Access address localhost :8080/AmazeUI-2.7.2/login.html I customized an html web page, which has been redirected to port 8443

How to configure springboot to jump from http to https

3. It is displayed in the address bar of the browser Unsafe: Because this certificate is not trusted, traditional enterprises generally need to purchase this certificate

The above is the detailed content of How to configure springboot to jump from http to https. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template