Struts-2.5 Action动态调用与通配调用无效解决办法

本文主要探讨在Struts2.5中遇到的Action动态调用和通配调用无效的问题,并提供了解决这些问题的两种方法:一是通过配置启用动态方法调用和允许的通配符;二是针对特定Action开放安全的动态调用。这些解决方案旨在在保证安全性的同时,应对Struts2.5版本对DMI和通配符调用的严格限制。

Struts-2.5 Action动态调用与通配调用无效解决办法

标签(空格分隔): 后端技术学习


一、问题引入

1. 动态方法调用的使用

在实际开发中,随着应用程序的不断扩大,不得不管理数量庞大的Action,为了减少Action,通常在一个Action中编写不同方法(必须遵从execute()方法相同的格式)出来不同的请求,如编写LoginAction,其中login()方法处理登录,register()方法处理注册请求,此时可以采用动态方法调用来处理
如:

class LoginAction{
  public String login(){}
  public String register(){}
}

调用方式则可以<form action="LoginAction!login">或通过<form action="LoginAction!register">

2. 通配符的使用

在Strust2 的使用中,可能同一类型的多个Action类有各自的处理逻辑,如UserAction中有login()、registe()方法,RecordAction中有create()、querry()、delete()方法。为了不使Strust.xml配置文件看起来肥胖,所以经常需要使用到通配符

<action name="*_*Action" class="example.{1}Action" method="{2}">
    <result>/{2}.jsp</result>
</action>

其中{1}、{2}分别对应的是*_*action中的第一个’*’和第二个’*’,如调用user_loginAction则<Action>元素中的class就被设置成了example.userAction。

二、出现在Strust2-2.3的限制

以上内容在Strust2-2.3以下的版本都能够很好的使用,但是由于安全性的原因Strust2-2.3以上对此功能进行了如下限制

  1. Strust2-2.3版本中添加了严格限制动态方法调用DMI的选项。设置strict-method-invocation="true"这个属性在你的<package>元素上面,这将告诉Strust2 拒绝所有没有被预期允许的方法调用,也就是不再允许调用没有写在 <action>method<allowed-methods>中的方法,否则调用浏览器会提示你

HTTP Status 404 - There is no Action mapped for namespace [/] and action name [regist_Action] associated with context path [/wildcard1]

启用SMI严格方法调用模式,所以你配置需要写成这样:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

  <constant name="struts.enable.DynamicMethodInvocation" value="true"/>

  <package name="default" extends="struts-default" strict-method-invocation="true">

    <action name="userAction" class="examples.actions.UserAction">
        <result name="success">/WEB-INF/content/index.jsp</result>
        <allowed-methods>login,registe</allowed-methods>
    </action>

  </package>
</struts>

取消SMI严格方法调用模式,所以你配置需要写成这样:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

  <constant name="struts.enable.DynamicMethodInvocation" value="false"/>

  <package name="default" extends="struts-default" strict-method-invocation="false">

    <action name="userAction" class="examples.actions.UserAction">
        <result name="success">/WEB-INF/content/index.jsp</result>
    </action>

  </package>
</struts>

然后通过<form action="LoginAction!login">的方式去调用


三、出现在Strust2-2.5 的限制

在Strust2-2.5版本中严格的DMI被SMI( Strict Method Invocation)所继承,同时SMI被默认启用(所有继承 struts-default 的包,strict-method-invocation 的属性被默认设置为了true )。同时你可以对单个包禁用SMI,但是没有一种全局的方式去禁用整个Strust项目的SMI限制

如果使用的是Strust2-2.5版本你的DTD的正确的格式应该是这样,否则会提示一些属性找不到的错误,以及未知的错误。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
...
</struts>

这种情况下如果你按照原先在Strust2-2.3以下的配置,那么之前的动态调用以及通配调用将被默认的禁用,action请求将提示如下错误

HTTP Status 404 - There is no Action mapped for namespace [/] and action name [regist_Action] associated with context path [/wildcard1]


四、解决办法

(一)、比较粗鲁的解决方式

  1. 启用DMI动态方法调用 <constant name="struts.enable.DynamicMethodInvocation" value="true" />
  2. 添加SMI严格方法调用中的可用通配符 <constant name="struts.strictMethodInvocation.methodRegex" value="([A-Za-z0-9_$]*)" />
  3. 禁用SMI严格方法调用 <package name="default" namespace="/" extends="struts-default" strict-method-invocation="false">

    然后你就可以开启通配符匹配,和动态调用。如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.strictMethodInvocation.methodRegex"
        value="([A-Za-z0-9_$]*)" />
    <package name="default" namespace="/" extends="struts-default"
        strict-method-invocation="false">
        <action name="student*_*" class="com.hwl.action.Student{1}Action"
            method="{2}">
            <result name="success">jsp/index.jsp</result>
            <result name="scan">jsp/edit.jsp</result>
            <result name="full">jsp/add.jsp</result>
        </action>
    </package>
</struts>

(二)、安全的方式

直接让全局禁止严格动态调用,会导致一些不安全的影响。可以采取如下方式进行较对指定的action开放,从而实现较安全的配置

  1. 为每个<action>配置<allowed-methods/>,使得可以动态的访问被allowed-methods下的方法
<package ...>
  ...
  <global-allowed-methods>execute,input,back,cancel,browse</global-allowed-methods>
  ...

  <action name="Bar">
    <allowed-methods>foo,bar</allowed-methods>
  </action>

  ...
</package>
  1. 为每个需要使用动态调用的方法添加@AllowedMethods,被@AllowedMethods包含的方法可以被动态调用
@AllowedMethods("end")
public class ClassLevelAllowedMethodsAction {
    public String execute() { 
        return ...
    }
}

  1. 或者是向整个<package>添加<global-allowed-methods/>
<package ...>
  ...
  <global-allowed-methods>execute,input,back,cancel,browse</global-allowed-methods>
  ...

  <action name="Bar">
    <allowed-methods>foo,bar</allowed-methods>
  </action>

  ...
</package>
  1. 为package 添加注解 @org.apache.struts2.convention.annotation.AllowedMethods()
@org.apache.struts2.convention.annotation.AllowedMethods({"home", "start"})
package org.apache.struts2.convention.actions.allowedmethods;

总结

每一个版本都在不停地为安全、效率进行升级,每一个版本相对于以前的版本都有修改,很多书籍使用的是老版本的框架进行知识点的讲解,所以会出现一些低版本在高版本下方法不可使用的情况。对此我们可以去官网上面查看相应的修改

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值