当前位置: 首页 > 编程笔记 >

Springboot配置阿里SSL证书异常处理

万阳嘉
2023-05-05

application.yml

http:
  port: 8083
server:
  port: 443
  ssl:
    key-store: classpath:ssl/2788888__xxxxx.com.pfx
    key-store-password: GJH789J
    key-store-type: PKCS12

Application启动类

// 拦截所有请求
    @Bean
    public ServletWebServerFactory servletContainer() {
        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(httpConnector());
        return tomcat;
    }
    // 配置http转https
    @Bean
    public Connector httpConnector() {
        Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
        connector.setScheme("http");
        // Connector监听的http的端口号
        connector.setPort(8083);
        connector.setSecure(false);
        // 监听到http的端口号后转向到的https的端口号
        connector.setRedirectPort(443);
        return connector;
    }

pom.xml

Linux运行Springboot.jar应用程序,出现以下错误:

nohup: ignoring input
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.9.RELEASE)
2020-03-06 22:35:50.376  INFO 1124 --- [           main] com.api.ApiApplication            : Starting ApiApplication v1.0.0 on iZ8vb4ugz9ame9vzmq1t5iZ with PID 1124 (/usr/local/xxx/paymentapi-1.0.0.jar started by root in /usr/local/xxx)
2020-03-06 22:35:50.378  INFO 1124 --- [           main] com.api.ApiApplication            : No active profile set, falling back to default profiles: default
2020-03-06 22:35:51.522  INFO 1124 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8443 (https) 8083 (http)
2020-03-06 22:35:51.541  INFO 1124 --- [           main] o.a.coyote.http11.Http11NioProtocol      : Initializing ProtocolHandler ["https-jsse-nio-8443"]
2020-03-06 22:35:51.556  INFO 1124 --- [           main] o.a.coyote.http11.Http11NioProtocol      : Initializing ProtocolHandler ["http-nio-8083"]
2020-03-06 22:35:51.565  INFO 1124 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-03-06 22:35:51.565  INFO 1124 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.26]
2020-03-06 22:35:51.648  INFO 1124 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-03-06 22:35:51.648  INFO 1124 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1185 ms
2020-03-06 22:35:52.231  INFO 1124 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-06 22:35:52.442  INFO 1124 --- [           main] o.a.coyote.http11.Http11NioProtocol      : Starting ProtocolHandler ["https-jsse-nio-8443"]
2020-03-06 22:35:52.613 ERROR 1124 --- [           main] org.apache.catalina.util.LifecycleBase   : Failed to start component [Connector[HTTP/1.1-8443]]
org.apache.catalina.LifecycleException: Protocol handler start failed
	at org.apache.catalina.connector.Connector.startInternal(Connector.java:1008)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
	at org.apache.catalina.core.StandardService.addConnector(StandardService.java:227)
	at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.addPreviouslyRemovedConnectors(TomcatWebServer.java:263)
	at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:195)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.startWebServer(ServletWebServerApplicationContext.java:297)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:163)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:552)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204)
	at com.api.PaymentapiApplication.main(PaymentapiApplication.java:22)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:51)
	at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52)
Caused by: java.lang.IllegalArgumentException: Stream closed
	at org.apache.tomcat.util.net.AbstractJsseEndpoint.createSSLContext(AbstractJsseEndpoint.java:99)
	at org.apache.tomcat.util.net.AbstractJsseEndpoint.initialiseSsl(AbstractJsseEndpoint.java:71)
	at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:218)
	at org.apache.tomcat.util.net.AbstractEndpoint.bindWithCleanup(AbstractEndpoint.java:1124)
	at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:1210)
	at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:585)
	at org.apache.catalina.connector.Connector.startInternal(Connector.java:1005)
	... 22 common frames omitted
Caused by: java.io.IOException: Stream closed
	at java.util.zip.InflaterInputStream.ensureOpen(InflaterInputStream.java:67)
	at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:142)
	at org.springframework.boot.loader.jar.ZipInflaterInputStream.read(ZipInflaterInputStream.java:52)
	at java.io.FilterInputStream.read(FilterInputStream.java:107)
	at org.apache.tomcat.util.security.KeyStoreUtil.load(KeyStoreUtil.java:59)
	at org.apache.tomcat.util.net.SSLUtilBase.getStore(SSLUtilBase.java:217)
	at org.apache.tomcat.util.net.SSLHostConfigCertificate.getCertificateKeystore(SSLHostConfigCertificate.java:206)
	at org.apache.tomcat.util.net.SSLUtilBase.getKeyManagers(SSLUtilBase.java:283)
	at org.apache.tomcat.util.net.SSLUtilBase.createSSLContext(SSLUtilBase.java:247)
	at org.apache.tomcat.util.net.AbstractJsseEndpoint.createSSLContext(AbstractJsseEndpoint.java:97)
	... 28 common frames omitted
2020-03-06 22:35:52.613  INFO 1124 --- [           main] o.a.coyote.http11.Http11NioProtocol      : Starting ProtocolHandler ["http-nio-8083"]
2020-03-06 22:35:52.628  INFO 1124 --- [           main] o.a.coyote.http11.Http11NioProtocol      : Pausing ProtocolHandler ["https-jsse-nio-8443"]
2020-03-06 22:35:52.632  INFO 1124 --- [           main] o.a.coyote.http11.Http11NioProtocol      : Pausing ProtocolHandler ["http-nio-8083"]
2020-03-06 22:35:52.640  INFO 1124 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-03-06 22:35:52.644  INFO 1124 --- [           main] o.a.coyote.http11.Http11NioProtocol      : Stopping ProtocolHandler ["http-nio-8083"]
2020-03-06 22:35:52.647  INFO 1124 --- [           main] org.apache.catalina.util.LifecycleBase   : The stop() method was called on component [StandardServer[-1]] after stop() had already been called. The second call will be ignored.
2020-03-06 22:35:52.648  INFO 1124 --- [           main] o.a.coyote.http11.Http11NioProtocol      : Stopping ProtocolHandler ["https-jsse-nio-8443"]
2020-03-06 22:35:52.648  INFO 1124 --- [           main] o.a.coyote.http11.Http11NioProtocol      : Destroying ProtocolHandler ["https-jsse-nio-8443"]
2020-03-06 22:35:52.648  INFO 1124 --- [           main] o.a.coyote.http11.Http11NioProtocol      : Destroying ProtocolHandler ["http-nio-8083"]
2020-03-06 22:35:52.651  INFO 1124 --- [           main] ConditionEvaluationReportLoggingListener : 
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-03-06 22:35:52.652 ERROR 1124 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 
***************************
APPLICATION FAILED TO START
***************************
Description:
The Tomcat connector configured to listen on port 8443 failed to start. The port may already be in use or the connector may be misconfigured.
Action:
Verify the connector's configuration, identify and stop any process that's listening on port 8443, or configure this application to listen on another port.
2020-03-06 22:35:52.655  INFO 1124 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

注意:

1.换任意空闲端口都会出现此问题

2.将SSL配置注释后,运行正常

 

错误原因:

在Spring 2.1.9.RELEASE中,Tomcat 9.0.26似乎是一个问题 。

降低Tomcat版本

Springboot修改内置Tomcat版本

 

此问题真的是让我几乎崩溃啊,疯掉的感觉。

 

 类似资料:
  • 进入阿里云服务器,搜索ssl证书,免费申请,1.证书申请(自行选购) 2.下载证书 3.解压下载压缩包 4.将.pfx文件放至springboot项目resources目录下    applicatio.yml中添加配置   5.设置http自动重定向https(8082端口->443端口): 在SpringApplication启动类中加入以下代码:(注意网上有些代码中的EmbeddedServletContainerFactory找不到

  • 主要内容:1 Tomcat生成SSL证书,2 使用SSL证书1 Tomcat生成SSL证书 运行以下命令以生成证书: 在Windows上: 在Linux上: 该工具将询问一些问题以提供证书。证书将位于文件夹中,证书名称为"tomcat"。您可以使用以下方法检查证书: 在Windows上: 在Linux上: 2 使用SSL证书 编辑文件: 在Windows上: 在Linux上: 并添加一个SSL连接器。 重新启动Tomcat,您就完成了。现在,您可以在Tom

  • 下面是我的代码: 这里有个例外: 对象“users”中字段“salary”上的字段错误:拒绝值[null];代码[NotNull.Users.Salary,NotNull.Salary,NotNull.java.lang.Integer,NotNull];参数[org.springframework.context.support.defaultmessageSourceResolvable:代码

  • 问题内容: 我正在创建一个Go TCP服务器(不是http / s),并且试图将其配置为使用SSL。我有一个StartCom免费SSL证书,正试图用来完成此任务。我的服务器代码如下: 我尝试切换证书的顺序,但不包括某些证书等,但是的输出基本上保持不变。有关完整输出,请参见此处。我对中间证书似乎做错了,但是我不知道该怎么办。我已经为此工作了几天,进行了大量的谷歌搜索和搜索,但似乎没有什么适合我的情况

  • 谈到SSL,我是个新手。所以我可以在这里使用一些关于我的情况的专业知识。 我有一个基于Java的web应用程序,可以进行SSL API调用。API和web应用程序都托管在同一台服务器上(