自定义springboot的tomcat

返回
Author Avatar
钢翼
2022-11-25
编程
85

package org.example.config;

import org.apache.coyote.http11.Http11NioProtocol; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;

@Configuration public class HttpsConfig {

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

@Bean
public TomcatServletWebServerFactory servletContainer() {

    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.addConnectorCustomizers((connector -> {
        Http11NioProtocol proto = (Http11NioProtocol) connector.getProtocolHandler();
        proto.setSSLEnabled(true);
        connector.setPort(port);
        connector.setScheme("https");
        connector.setSecure(true);
        //证书文件路径
        proto.setKeystoreFile("classpath:www.abc.com.pfx");
        //证书类型
        proto.setKeystoreType("pkcs12");
        //证书密钥密码
        proto.setKeystorePass("aaaaaaaaaa");
        //证书别名,可通过指令查询 keytool -list -keystore 密钥路径 -storepass 密码   -storetype pkcs12
        proto.setKeyAlias("www.abc.com");
    }));
    return tomcat;

}

}