spring boot 2.X 使用tomcat 9.X版本issue

本文介绍了在Spring Boot 2.x版本中遇到使用Tomcat 9.x启动异常的问题,详细分析了异常的原因,并提出了针对性的解决方案,通过调整Tomcat Bean配置,成功解决了启动问题,最终实现应用无报错顺利启动。

启动异常

The HTTP header line [spas-accesskey0x0d] does not conform to RFC 7230 and has been ignored

异常分析

The way that errors that occur very early during HTTP request processing are reported is the same in Spring Boot 2.3.1 as it was in 2.0.x. What has changed here is that Tomcat is now generating an error where it was not previously. Tomcat 9 more strictly enforces the HTTP specification, rejecting non-compliant header names and values with a 400 response. You can get back to Tomcat 8.5's behaviour where your non-spec-compliant request was still accepted by customising Tomcat's connector so that illegal headers are not rejected

解决方案(tomcat bean 配置)

@Bean
TomcatConnectorCustomizer headerRejectionCustomizer() {
    return (connector) -> 
        ((AbstractHttp11Protocol<?>)connector.getProtocolHandler()).setRejectIllegalHeader(false);
}

结果 :再次启动无报错 有效

java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [org.apache.catalina.webresources.StandardRoot@56c698e3] at java.util.concurrent.FutureTask.report(FutureTask.java:122) at java.util.concurrent.FutureTask.get(FutureTask.java:192) at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:916) at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:841) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134) at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909) at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.core.StandardService.startInternal(StandardService.java:421) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:930) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.startup.Tomcat.start(Tomcat.java:459) at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:107) at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.<init>(TomcatWebServer.java:88) at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getTomcatWebServer(TomcatServletWebServerFactory.java:438) at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:191) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:180) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:153) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) at com.citycloud.ccuap.tc.admin.MainApp.main(MainApp.java:42) 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:50) at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
03-11
Microsoft Windows [版本 10.0.19045.3448] (c) Microsoft Corporation。保留所有权利。 D:\nacos-server-2.0.3\nacos\bin>startup.cmd -m standalone "nacos is starting with standalone" ,--. ,--.&#39;| ,--,: : | Nacos 2.0.3 ,`--.&#39;`| &#39; : ,---. Running in stand alone mode, All function modules | : : | | &#39; ,&#39;\ .--.--. Port: 8848 : | \ | : ,--.--. ,---. / / | / / &#39; Pid: 5576 | : &#39; &#39;; | / \ / \. ; ,. :| : /`./ Console: http://10.30.52.56:8848/nacos/index.html &#39; &#39; ;. ;.--. .-. | / / &#39;&#39; | |: :| : ;_ | | | \ | \__\/: . .. &#39; / &#39; | .; : \ \ `. https://nacos.io &#39; : | ; .&#39; ," .--.; |&#39; ; :__| : | `----. \ | | &#39;`--&#39; / / ,. |&#39; | &#39;.&#39;|\ \ / / /`--&#39; / &#39; : | ; : .&#39; \ : : `----&#39; &#39;--&#39;. / ; |.&#39; | , .-./\ \ / `--&#39;---&#39; &#39;---&#39; `--`---&#39; `----&#39; 2025-09-11 16:01:22,062 INFO Bean &#39;org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@6594402a&#39; of type [org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2025-09-11 16:01:22,068 INFO Bean &#39;methodSecurityMetadataSource&#39; of type [org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2025-09-11 16:01:22,405 WARN Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is java.lang.IllegalStateException: Unable to create the directory [D:\nacos-server-2.0.3\nacos\bin\file:.] to use as the base directory 2025-09-11 16:01:22,408 INFO Nacos Log files: D:\nacos-server-2.0.3\nacos\logs 2025-09-11 16:01:22,409 INFO Nacos Log files: D:\nacos-server-2.0.3\nacos\conf 2025-09-11 16:01:22,409 INFO Nacos Log files: D:\nacos-server-2.0.3\nacos\data 2025-09-11 16:01:22,411 ERROR Startup errors : org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is java.lang.IllegalStateException: Unable to create the directory [D:\nacos-server-2.0.3\nacos\bin\file:.] to use as the base directory at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544) 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.alibaba.nacos.Nacos.main(Nacos.java:35) 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:49) at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) at org.springframework.boot.loader.PropertiesLauncher.main(PropertiesLauncher.java:467) Caused by: java.lang.IllegalStateException: Unable to create the directory [D:\nacos-server-2.0.3\nacos\bin\file:.] to use as the base directory at org.apache.catalina.startup.Tomcat.initBaseDir(Tomcat.java:873) at org.apache.catalina.startup.Tomcat.getServer(Tomcat.java:657) at org.apache.catalina.startup.Tomcat.getService(Tomcat.java:589) at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:171) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:180) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:153) ... 16 common frames omitted 2025-09-11 16:01:22,412 WARN [WatchFileCenter] start close 2025-09-11 16:01:22,413 WARN [WatchFileCenter] start to shutdown this watcher which is watch : D:\nacos-server-2.0.3\nacos\conf 2025-09-11 16:01:22,413 WARN [WatchFileCenter] already closed 2025-09-11 16:01:22,413 WARN [NotifyCenter] Start destroying Publisher 2025-09-11 16:01:22,414 WARN [NotifyCenter] Destruction of the end 2025-09-11 16:01:22,414 ERROR Nacos failed to start, please see D:\nacos-server-2.0.3\nacos\logs\nacos.log for more details. 2025-09-11 16:01:22,423 INFO Error starting ApplicationContext. To display the conditions report re-run your application with &#39;debug&#39; enabled. 2025-09-11 16:01:22,427 ERROR Application run failed org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is java.lang.IllegalStateException: Unable to create the directory [D:\nacos-server-2.0.3\nacos\bin\file:.] to use as the base directory at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544) 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.alibaba.nacos.Nacos.main(Nacos.java:35) 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:49) at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) at org.springframework.boot.loader.PropertiesLauncher.main(PropertiesLauncher.java:467) Caused by: java.lang.IllegalStateException: Unable to create the directory [D:\nacos-server-2.0.3\nacos\bin\file:.] to use as the base directory at org.apache.catalina.startup.Tomcat.initBaseDir(Tomcat.java:873) at org.apache.catalina.startup.Tomcat.getServer(Tomcat.java:657) at org.apache.catalina.startup.Tomcat.getService(Tomcat.java:589) at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:171) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:180) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:153) ... 16 common frames omitted 分析报错信息
最新发布
09-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值