转载

Struts2-Upgrade

遇到总结一下项目中有关Struts2升级中遇到的坑。项目大概开始于09年左右,维护近10年,由于Struts2安全漏洞问题决定升级版本,由于版本跨度比较大,一些方法已经弃用或配置变更等

Main Maven Dependency

// old
<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.1.8.1</version>
</dependency>

// new
<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.5.17</version>
</dependency>

Struts2.xml

  • old
<filter>
    <filter-name>ActionContextCleanUp</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>

<filter-mapping>
    <filter-name>ActionContextCleanUp</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
    <filter-name>struts</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    <init-param>
        <param-name>struts.i18n.encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>struts</filter-name>
    <url-pattern>*.action</url-pattern> 
</filter-mapping>
  • new
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    <init-param>
        <param-name>struts.i18n.encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
</filter-mapping>

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/project/*</url-pattern>
</filter-mapping>

在Struts2 2.5.17中 org.apache.struts2.dispatcher.FilterDispatcherorg.apache.struts2.dispatcher.ActionContextCleanUp 被废除,用 org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter 替换

升级过程中遇到的报错:

java.lang.NoSuchMethodError: ognl.SimpleNode.isEvalChain(Lognl/OgnlContext;)Z
  • 问题: ognl 的jar包冲突

需要查询项目中Jar依赖关系,排除无用Jar包。Ognl版本至少在 3.0.6 以上

// 我遇到的是xwork中有低版本的ognl包,故排除
<exclusion>
    <artifactId>ognl</artifactId>
    <groupId>opensymphony</groupId>
</exclusion>
There is no Action mapped for namespace [/] and action name [user!add] associated with context path
  • 问题:由于2.5.17安全机制,过滤器必须指定mapped规则
  1. 粗粒度—动态方法调用
// struts.xml配置添加
 <struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
    ......
    <package name="default" extends="struts-default">
        ......
        <global-allowed-methods>regex:.*</global-allowed-methods>
        ......
    </package>
</struts>
  1. 细粒度-动态方法调用
// action配置

 <action name="login_*" method="{2}"  class="com.wxmimperio.struts.{1}Action"> 
    .......
    <result name="success">/pages/success.jsp</result> 
    <result name="error">/pages/error.jsp</result> 
    ......
    <allowed-methods>regex:.*</allowed-methods>
    ......
</action>

这里在action的name中通配了一个login_*,它对应映射的是method属性。如果在客户端发生一个这样的请 求:login_init.action、login_show.action等,这时服务器就会自动调用这个action中的init()方法或 show()方法。这里的method=”{1}”代表是第一个星号,如果有多个星号,就要根据顺序来通配{1},{2},{3}….

allowed-methods 中可以用逗号分隔指定方法名,也可以用正则匹配。

错误: Struts2 与 Servlet 冲突

表现在当struts.xml如下配置:

<filter-mapping>
    <filter-name>struts</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

struts拦截器会拦截/*下所有路径,所以自定义的servlet无法被mapped到,导致请求根本无法响应

解决方案如下:

  1. 修改servlet的相关配置,统一在servlet后面加上.servlet
<servlet>  
    <servlet-name>jqueryAjaxServlet</servlet-name>  
    <servlet-class>com.clzhang.sample.struts2.servlet.jQueryAjaxServlet</servlet-class>  
</servlet> 

<servlet-mapping>  
    <servlet-name>jqueryAjaxServlet</servlet-name>  
    <url-pattern>/servlet/jqueryAjax.servlet</url-pattern>  
</servlet-mapping>
  1. 修改拦截页面配置,就是将struts的相关拦截配置一下
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/user/*</url-pattern>
</filter-mapping>
  1. 修改struts.xml文件中的后缀映射
<constant name="struts.action.extension" value="action"></constant>

转载请注明出处

原文  http://wxmimperio.tk/2018/08/27/Struts2-Upgrade/
正文到此结束
Loading...