SpringBoot2 开启https

  • 生成证书
keytool -genkey -alias abc -keypass k123456 -keyalg RSA -keysize 2048 -validity 3650 -keystore D:/keys/keystore.keystore -storepass s123456
  • 修改yml
server:
  session-timeout: 3600
  tomcat:
    max-threads: 1000
    min-spare-threads: 30
  port: 8443
  ssl:
    key-store: classpath:keystore.keystore
    key-store-password: 123456
    key-store-type: PKCS12
    key-alias: abc
  • 修改http重定向https,添加到SpringBootApplication中
 @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addAdditionalTomcatConnectors(createHTTPConnector());
        return tomcat;
    }

    private Connector createHTTPConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        //同时启用http(8080)、https(8443)两个端口
        connector.setScheme("http");
        connector.setSecure(false);
        connector.setPort(8080);
        connector.setRedirectPort(8443);
        return connector;
    }

测试

 

异常信息

org.apache.catalina.LifecycleException: Failed to start component [Connector[HTTP/1.1-8443]]
  at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)
  at org.apache.catalina.core.StandardService.addConnector(StandardService.java:225)
  at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.addPreviouslyRemovedConnectors(TomcatWebServer.java:256)
  at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:198)
  at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.startWebServer(ServletWebServerApplicationContext.java:300)
  at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:162)
  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553)
  at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
  at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)
  at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:327)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243)
  at com.sdyy.SpringbootApplication.main(SpringbootApplication.java:30)
Caused by: org.apache.catalina.LifecycleException: Protocol handler start failed
  at org.apache.catalina.connector.Connector.startInternal(Connector.java:1020)
  at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
  ... 13 common frames omitted
Caused by: java.lang.IllegalArgumentException: DerInputStream.getLength(): lengthTag=109, too big.
  at org.apache.tomcat.util.net.AbstractJsseEndpoint.createSSLContext(AbstractJsseEndpoint.java:116)
  at org.apache.tomcat.util.net.AbstractJsseEndpoint.initialiseSsl(AbstractJsseEndpoint.java:87)
  at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:225)
  at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:1150)
  at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:591)
  at org.apache.catalina.connector.Connector.startInternal(Connector.java:1018)
  ... 14 common frames omitted
Caused by: java.io.IOException: DerInputStream.getLength(): lengthTag=109, too big.
  at sun.security.util.DerInputStream.getLength(DerInputStream.java:599)
  at sun.security.util.DerValue.init(DerValue.java:391)
  at sun.security.util.DerValue.(DerValue.java:332)
  at sun.security.util.DerValue.(DerValue.java:345)
  at sun.security.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:1938)
  at java.security.KeyStore.load(KeyStore.java:1445)
  at org.apache.tomcat.util.net.SSLUtilBase.getStore(SSLUtilBase.java:139)
  at org.apache.tomcat.util.net.SSLHostConfigCertificate.getCertificateKeystore(SSLHostConfigCertificate.java:204)
  at org.apache.tomcat.util.net.jsse.JSSEUtil.getKeyManagers(JSSEUtil.java:184)
  at org.apache.tomcat.util.net.openssl.OpenSSLUtil.getKeyManagers(OpenSSLUtil.java:79)
  at org.apache.tomcat.util.net.AbstractJsseEndpoint.createSSLContext(AbstractJsseEndpoint.java:114)
  ... 19 common frames omitted
19:24:56 [main] INFO  o.a.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8080"]
19:24:56 [main] INFO  o.a.coyote.http11.Http11NioProtocol - Pausing ProtocolHandler ["https-openssl-nio-8443"]
19:24:56 [main] INFO  o.a.coyote.http11.Http11NioProtocol - Pausing ProtocolHandler ["http-nio-8080"]
19:24:57 [main] INFO  o.a.catalina.core.StandardService - Stopping service [Tomcat]

修改生成证书

keytool -genkey -alias llhl  -storetype PKCS12 -keyalg RSA -keysize 2048  -keystore keystore.p12 -validity 3650

正常访问

SpringBoot2 开启https_第1张图片

你可能感兴趣的:(技术,工具)