Tomcat中使用ipv6地址的示例代碼
在公司的一次項(xiàng)目改造過程中,需要將原來的IPV6替換成IPV4。查詢資料資料之后,你會(huì)遇到如下問題,總結(jié)解決辦法如下:
1、替換老版本Tomcat
tomcat6不支持監(jiān)聽ipv6 tomcat7支持監(jiān)聽ipv6 tomcat8支持監(jiān)聽ipv6
我們只需將tomcat換為7以上的版本即可?。。。。。。。?!
2、項(xiàng)目無法啟動(dòng)
如果使用Tomcat 7以上的版本來替換老版本的Tomcat,那么問題來了你會(huì)無法啟動(dòng)項(xiàng)目。
錯(cuò)誤日志:
26-Aug-2020 16:11:15.558[RMI TCP Connection(3)-127.0.0.1] org.apache.tomcat.util.descriptor.web.WebXmlParser.parseWebXml Parse error in application web.xml file at [file:/F:/Idea-Code/chuangjhx/out/artifacts/emp_std_192_169_1_81_war_exploded/WEB-INF/web.xml]
java.io.FileNotFoundException: Could not resolve XML resource [user] with public ID [null], system ID [userConfig.xml] and base URI [file:/F:/Idea-Code/chuangjhx/out/artifacts/emp_std_192_169_1_81_war_exploded/WEB-INF/web.xml] to a known, local entity.
因?yàn)樾掳姹镜腡omcat無法解析你的web.xml
解決辦法:
打開你的Tomcat的安裝目錄 找到conf目錄下的context.xml
在** **中添加屬性xmlBlockExternal,具體如:
<Context xmlBlockExternal="false">
3、Tomcat7攔截特殊字符
當(dāng)你在服務(wù)期內(nèi)使用ipv6地址進(jìn)行下載或者跳轉(zhuǎn)的時(shí)候,會(huì)出現(xiàn)如下錯(cuò)誤
Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.
java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:479)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:687)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
這是因?yàn)?/p>
- 根據(jù)rfc規(guī)范(RFC 3986規(guī)范定義了Url中只允許包含英文字母(a-zA-Z)、數(shù)字(0-9)、-_.~4個(gè)特殊字符以及所有保留字符(RFC3986中指定了以下字符為保留字符:! * ’ ( ) ; : @ & = + $ , / ? # [ ]))。
- url中不允許有 |,{,}等特殊字符,但在實(shí)際生產(chǎn)中還是有些url有可能攜帶有這些字符,特別是|還是較為常見的。在tomcat升級(jí)到7以后,對(duì)url字符的檢查都變嚴(yán)格了,如果出現(xiàn)這類字符,tomcat將直接返回400狀態(tài)碼。
解決辦法(SpringBoot)
在你的啟動(dòng)類上添加
@Bean public TomcatServletWebServerFactory webServerFactory() { TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(); factory.addConnectorCustomizers(new TomcatConnectorCustomizer() { @Override public void customize(Connector connector) { connector.setProperty("relaxedPathChars", "\"<>[\\]^`{|}"); connector.setProperty("relaxedQueryChars", "\"<>[\\]^`{|}"); } }); return factory; }
解決辦法(Tomcat)
打開你的Tomcat 的安裝目錄 找到conf目錄下的server.xml ,作出如下修改:
<Connector port="8082" protocol="HTTP/1.1" connectionTimeout="20000" relaxedQueryChars="[]|{}^\`"<>" redirectPort="8443" />
4、設(shè)置IPV6的監(jiān)聽
想讓你的tomcat即可以使用IPV4又可以使用IPV6訪問,需要進(jìn)行如下配置
打開打開你的Tomcat的安裝目錄 找到conf目錄下的server.xml,作出如下修改:
<Connector port="8080" protocol="HTTP/1.1" address="0.0.0.0" connectionTimeout="20000" relaxedQueryChars="[]|{}^\`"<>" redirectPort="8443" /> <Connector port="8080" protocol="HTTP/1.1" address="[::]" connectionTimeout="20000" relaxedQueryChars="[]|{}^\`"<>" redirectPort="8443" />
到此這篇關(guān)于Tomcat中使用ipv6地址的示例代碼的文章就介紹到這了,更多相關(guān)Tomcat使用ipv6地址內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。