Struts2的官网公布了一个远程命令执行漏洞,官方强烈建议升级到2.3.15.1或者以上版本,该版本包含校订过的struts2核心库。html
咱们以前开发项目主要采用的Struts2版本是2.2.1,本文介绍下Struts2从2.2.1升级到2.3.15.1的过程。前端
首先自Struts官方网站下载struts-2.3.15.1-all.zip,里面包含所须要的jar。java
删除项目中以下jar文件:web
将struts-2.3.15.1-all.zip中的以下jar文件加到项目中:express
升级jar以后,测试项目,发现2个问题,第一个问题,控制台会常常输出以下信息:apache
***********************************************************************
* WARNING!!! *
* *
* >>> ActionContextCleanUp<<< is deprecated! Please use the new filters! *
* *
* This can be a source of unpredictable problems! *
* *
* Please refer to the docs for more details! *
* http://struts.apache.org/2.x/docs/webxml.html *
* *
***********************************************************************app
意思是说ActionContextCleanUp过时,不建议使用,查看了测试
http://struts.apache.org/2.x/docs/webxml.html,没有看出因此然,后来在论坛中有人说Struts的过滤器org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter包含了ActionContextCleanUp功能,因此注释掉web.XML文件中的ActionContextCleanUp过滤器便可。网站
注意若是struts2过滤器不是this
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter的话,要修改为这个。
固然上述问题是个警告,若是说警告不予理睬的话,第二个问题是控制台抛出错误,这个就要解决了。
抛出错误的状况是:代码在执行某些数据保存的时候,会抛出异常,可是数据依然能保存,从前台功能上看没有任何问题。
后台异常堆栈信息:
16-04-01 02:33:49.017 ERROR [CommonsLogger.java:38] Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'formName' on 'class net.jqsoft.XXXX.manager.XXXX.QuestionAnswer: Error setting expression 'formName' with value ['dataListForm', ]
Error setting expression 'formName' with value ['dataListForm', ] - [unknown location]
at com.opensymphony.xwork2.ognl.OgnlValueStack.handleRuntimeException(OgnlValueStack.java:197)
at com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:174)
at com.opensymphony.xwork2.ognl.OgnlValueStack.setParameter(OgnlValueStack.java:148)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.setParameters(ParametersInterceptor.java:318)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(Parameters
异常信息说的很清楚,只要关闭struts.devMode(开发模式)就不会抛出这些信息,尝试设置struts.devMode=false,果真再也不抛出异常。
可是仍是不放心,想知道到底为何?参考论坛上的说法,Action中的全部属性都要加上seter、geter方法,检查下,都加上了,仍是不行。
而后在action中添加private string formName,并生成seter、geter,测试发现抛出下面的问题:
Unexpected Exception caught setting ' showNum ' on .........
而后在action中添加private string showNum,并生成seter、geter,依次下去,最终问题解决。
总结下,原来Struts2.3.15.1检查的比较严格,凡在前端界面form中全部的name=’xxx’,action中都要有对应的属性,哪怕action端不须要使用这个name,也要加上。
搞明白了,能够放心的把struts.devMode=false,或者把代码写的严谨点,不须要的<input name=’xxx’>不要写。
最后总结下升级过程:
一、 替换jar
二、 注释web.xml文件中的
<filter>
<filter-name>struts2CleanUpFilter</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2CleanUpFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
三、 修改struts.properties文件中的struts.devMode = false