最近應用需要升級Tomcat版本,啟動時遇到的一些問題,記錄一下。
背景:使用的docker容器,專門的運維人員幫忙升級了新的鏡像。
問題:
1. 升級后直接重啟了應用,發現不能訪問。查看日志發現拋異常找不到配置文件,查看發現docker里面配置的配置文件都在原來的tomcat7的conf目錄下,更改后重啟。重啟后仍然不成功,查看日志一步一步來。
2. 修改context.xml,報錯:無法加載資源工廠類dbcp
[ERROR][org.springframework.web.context.ContextLoader] Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xxx' defined in ServletContext resource [/WEB-INF/applicationContext-beans.xml] Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/applicationContext-dataSource.xml : Invocation of init method failed; nested exception is javax.naming.NamingException: 無法加載資源工廠類 [Root exception is java.lang.ClassNotFoundException:org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory]
搜索后發現tomcat7 和 tomcat8的工廠類名稱發生了變化,所以找不到了,解決方法很簡單,將conf目錄下context.xml文件修改,
factory改為:
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
同時 tomcat7中一些參數名在Tomcat8中也發生了變化,比如:maxActive需改寫為maxTotal,maxWait需改寫為maxWaitMillis,還有一些可自行對照了改。
3. 修改server.xml,上述配置文件修改后,日志仍然報錯:找不到JapperListener類,
java.lang.ClassNotFoundException: org.apache.catalina.core.JasperListener
對比tomcat7 與 tomcat 8 的 server.xml文件發現8里面沒有這個類,直接注釋掉。
啟動后又報錯:
The AJP Connector is configured with secretRequired="true" but the secret attribute is either null or "". This combination is not valid.
原來8版本的tomcat有個屬性 secretRequired需要給設置值:
<Connector port=
"8009"
protocol=
"AJP/1.3"
redirectPort=
"8443"
/>
設置成""就可以了,也就是下面的設置結果:
<Connector port=
"8009"
protocol=
"AJP/1.3"
redirectPort=
"8443"
secretRequired=
""
/>
4. 修改web.xml文件
報錯:
org.apache.tomcat.util.descriptor.web.SecurityConstraint.findUncoveredHttpMethods For security constraints with URL pattern [/*]
only the HTTP methods [TRACE HEAD DELETE OPTIONS PUT] are covered. All other methods are uncovered
修改方式類似下面這種改法:
<security-constraint>
<web-resource-collection>
<url-pattern>/bg/c/portal/protected</url-pattern>
<http-method>POST</http-method>
<http-method>GET</http-method>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>...</transport-guarantee>
</user-data-constraint>
</security-constraint>
改成下面這種,再增加一個安全配置,使所有的請求通過:
<security-constraint> <web-resource-collection> <url-pattern>/bg/c/portal/protected</url-pattern> <http-method>POST</http-method> <http-method>GET</http-method> </web-resource-collection> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint> <security-constraint> <web-resource-collection> <url-pattern>/bg/c/portal/protected</url-pattern> </web-resource-collection> <user-data-constraint> <transport-guarantee>NONE</transport-guarantee> </user-data-constraint> </security-constraint>
后來還有登錄頁面出來了,但是登錄不上去,可能使數據源的加密方式改了,需要重新加密用戶名密碼吧。因為是測試環境,直接使用了明文username/password。