Springboot1.x, 2.x配置https证书 ssl

1,进入jdk 目录生成key

cmd进入 jdk bin 目录下

我的是E:\Program Files\Java\jdk1.8.0_2.12\bin

输入命令

keytool -genkey -alias tomcat -keyalg RSA  -keystore "E:\Program Files\Java\jdk1.8.0_2.12\bin\ssl.key"

alias 别名

keyalg加密算法

2.properties配置

Springboot1.x, 2.x配置https证书 ssl_第1张图片

Springboot1.x, 2.x配置https证书 ssl_第2张图片

server.ssl.key-store=ssl.key
server.ssl.key-store-type=JKS
server.ssl.key-alias=ssl
server.ssl.key-store-password=123456

3.启动类配置

2.x配置

Springboot1.x, 2.x配置https证书 ssl_第3张图片

@Bean
@Qualifier(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public DispatcherServlet dispatcherServlet() {
    return new com.ibeetl.admin.core.conf.MyDispatcherServlet();
}

/**
 * it's for set http url auto change to https
 */
@Bean
public TomcatServletWebServerFactory servletContainer() { //springboot2 新变化
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
        @Override
        protected void postProcessContext(Context context) {
            SecurityConstraint securityConstraint = new SecurityConstraint();
            securityConstraint.setUserConstraint("CONFIDENTIAL");
            SecurityCollection collection = new SecurityCollection();
            collection.addPattern("/*");
            securityConstraint.addCollection(collection);
            context.addConstraint(securityConstraint);
        }
    };
    tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());
    return tomcat;
}

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

 

 

1.x配置

package com.art.boot;

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

/**
 * @author chenye
 */
@Configuration
public class HttpsConfig {

    @Value("${server.custom.httpPort}")
    private Integer httpPort;
    @Value("${server.port}")
    private Integer httpsPort;

    @Bean
    public EmbeddedServletContainerFactory servletContainerFactory() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };

        tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());
        return tomcat;
    }

    private Connector initiateHttpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(this.httpPort);
        connector.setSecure(false);
        connector.setRedirectPort(this.httpsPort);

        return connector;
    }
}

   
 

你可能感兴趣的:(springboot,ssl)