Messages:This method: edit for action XXX is not allowed!-Struts2.5动态方法调用错误解决方法

来源:互联网 发布:无线虚能矩阵 编辑:程序博客网 时间:2024/06/03 22:42

struts.xml部分配置

这里写图片描述

Action部分代码

这里写图片描述

报错信息

这里写图片描述

解决方案

此处查阅了大量文章,还有struts官方文档:

  • struts2:This method: login for action user is not allowed!
  • 在struts2.5版本中使用DMI遇到问题
  • Struts 2.5 动态方法调用(DMI)问题
  • Apache Struts 2 Documentation Action Configuration

解决方法无非是在配置文件中添加

<global-allowed-methods>regex:.*</global-allowed-methods>

例如我的struts.xml中配置:

<package name="yong" extends="struts-default">    <!-- 配置自定义拦截器LoginedCheckInterceptor -->    <interceptors>        <interceptor name="loginedCheck"            class="com.yong.struts.interceptor.LoginedCheckInterceptor" />    </interceptors>    <!--struts2.5之后要添加此属性才能访问action,方法名以逗号隔开,或使用正则匹配-->    <global-allowed-methods>regex:.*</global-allowed-methods></package>

或者,针对action,在 action 块中添加

<allowed-methods>regex:.*</allowed-methods>

同样,也支持在你的 action 上使用 @AllowedMethods 注解

struts-default默认的设置为

<global-allowed-methods>execute,input,back,cancel,browse,save,delete,list,index</global-allowed-methods>

全局设置是增量而不是覆盖的,支持正则和直接匹配方法,
源码在这两段

// com.opensymphony.xwork2.DefaultActionProxy#prepare// com.opensymphony.xwork2.config.entities.ActionConfig#isAllowedMethod

但是经过这样的配置之后总是不生效,经过排查发现在action中使用了@ParentPackage(value = "struts-default") 注解,这样的话只支持struts-default默认的方法通过。将这里改为:

@ParentPackage(value = "yong")

value为你自定义package的名称,继承自struts-default。这样便可使上述的配置生效。

吐槽:struts2真是大坑,每次因为爆出安全漏洞而升级版本都会出现各种问题。

阅读全文
0 0
原创粉丝点击